批量合并视频/图像,合并前重命名避免覆盖

血的教训:采用md5哈希算法+时间戳重命名,避免覆盖

合并后就能安安心心排序了

python 复制代码
import os
import hashlib
import shutil

# 指定的源文件夹列表
source_dirs = ["video/vox2/8-12/10s", "video/vox2/12-20/10s", "video/vox2/20-30/10s", "video/vox2/30-60/10s", "video/vox2/60-inf"]  # 添加更多文件夹

# 目标文件夹
target_dir = "video/vox2/10s"

# 创建目标文件夹,如果它不存在
if not os.path.exists(target_dir):
    os.makedirs(target_dir)

# 遍历每个源文件夹
for source_dir in source_dirs:
    if not os.path.exists(source_dir):
        print(f"Warning: Directory does not exist: {source_dir}")
        continue

    print(f"Processing directory: {source_dir}")

    # 使用 os.walk() 递归遍历文件夹
    for root, dirs, files in os.walk(source_dir):
        for filename in files:
            print(f"Checking file: {os.path.join(root, filename)}")

            # 检查文件是否为.jpg或.png格式
            if filename.lower().endswith('.mp4'):
                # 分离文件名和扩展名
                base_name, ext = os.path.splitext(filename)

                # 生成文件内容的哈希值
                with open(os.path.join(root, filename), 'rb') as f:
                    file_hash = hashlib.md5(f.read()).hexdigest()

                # 获取文件的修改时间戳
                modification_time = os.path.getmtime(os.path.join(root, filename))

                # 构建目标文件路径
                dst_file_path = os.path.join(target_dir, f"{file_hash}_{modification_time}{ext}")

                # 如果文件已存在,递增后缀直到找到唯一文件名
                while os.path.exists(dst_file_path):
                    modification_time += 0.001  # 微小增加时间戳
                    dst_file_path = os.path.join(target_dir, f"{file_hash}_{modification_time}{ext}")

                # 构建源文件的完整路径
                src_file_path = os.path.join(root, filename)

                # 复制文件到目标文件夹
                shutil.copy2(src_file_path, dst_file_path)

                # 输出信息
                print(f'Copied "{filename}" from "{root}" to "{dst_file_path}"')
            else:
                print(f"Ignored file: {os.path.join(root, filename)}")

print("All images have been merged successfully.")
相关推荐
给大佬递杯卡布奇诺13 小时前
FFmpeg 基本API avcodec_alloc_context3函数内部调用流程分析
c++·ffmpeg·音视频
给大佬递杯卡布奇诺16 小时前
FFmpeg 基本API avio_open函数内部调用流程分析
c++·ffmpeg·音视频
Damon小智18 小时前
RedPlayer 视频播放器在 HarmonyOS 应用中的实践
音视频·harmonyos·鸿蒙·小红书·三方库·redplayer
云雾J视界19 小时前
Linux企业级解决方案架构:字节跳动短视频推荐系统全链路实践
linux·云原生·架构·kubernetes·音视频·glusterfs·elk stack
Likeadust1 天前
新版视频直播点播平台EasyDSS用视频破局,获客转化双提升
大数据·音视频
涛涛讲AI1 天前
一段音频多段字幕,让音频能够流畅自然对应字幕 AI生成视频,扣子生成剪映视频草稿
人工智能·音视频·语音识别
lzptouch1 天前
数据预处理(音频/图像/视频/文字)及多模态统一大模型输入方案
人工智能·音视频
casdfxx2 天前
捡到h3开发板,做了个视频小车(二),御游追风plus做遥控器
音视频
给大佬递杯卡布奇诺2 天前
FFmpeg 基本API avcodec_send_packet函数内部调用流程分析
c++·ffmpeg·音视频
酌量2 天前
从 ROS 订阅视频话题到本地可视化与 RTMP 推流全流程实战
经验分享·笔记·ffmpeg·音视频·ros