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

血的教训:采用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.")
相关推荐
八月的雨季 最後的冰吻4 小时前
音视频会议梳理
音视频
合尘猫1 天前
用 6GB 显卡搭短视频产线:MiniMax H3 + ffmpeg 全流程实录(55 秒片 / 40 分钟)
人工智能·音视频
the3clipse3 天前
Windows 11 26H2 声音与音频问题排查指南:没声音、麦克风不收音与蓝牙音频优化
音视频·le audio·windows 11 26h2·win11没声音·麦克风没声音·蓝牙耳机音质·音频服务修复
开开心心就好3 天前
PDF水印怎么去掉?勾选对象再点匹配
java·前端·mysql·支持向量机·pdf·音视频·模拟退火算法
RobinDevNotes4 天前
Claude Code 剪视频,不上传、不要 API Key
人工智能·python·音视频·开发工具·ipython
伯奥克客服4 天前
2026年视频综合一体机架构选型:从接口协议到流媒体底层的深度技术解析
架构·音视频
开开心心就好4 天前
两张表格数据对比工具,不用写函数也能查
算法·spring·智能手机·pdf·电脑·intellij-idea·音视频
FansAI_art4 天前
多角色 AIGC 短片的双模型分工:静态生图与图生视频怎么接
aigc·音视频
山顶夕景5 天前
【ECCV2026】流式视频理解Thinking in Streaming Video
音视频·vlm·视频理解·多模态理解·流式视频
zhanlang02176 天前
HTML 零基础入门到企业实战【第五章 5.4】内嵌网页 iframe 标签:嵌入网页、视频、地图实战
前端·html·音视频