Loading... 通过脚本把视频的音频和视频分离存储到指定目录文件夹下 加入多线程提高处理速度 ``` import os import subprocess from concurrent.futures import ThreadPoolExecutor from tqdm import tqdm # 定义输入和输出目录 input_directory = r'G:\day' output_directory = r'G:\day\mp4_no_audio' audio_output_directory = r'G:\day\audio' # 确保输出目录存在 os.makedirs(output_directory, exist_ok=True) os.makedirs(audio_output_directory, exist_ok=True) # 获取所有MP4文件 files = [f for f in os.listdir(input_directory) if f.endswith('.mp4')] def process_file(filename, pbar): input_file = os.path.join(input_directory, filename) output_video_file = os.path.join(output_directory, filename) output_audio_file = os.path.join(audio_output_directory, f"{os.path.splitext(filename)[0]}.mp3") # 构建FFmpeg命令分离音频 command_audio = ['ffmpeg', '-i', input_file, '-q:a', '0', '-map', 'a', output_audio_file] subprocess.run(command_audio, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) # 构建FFmpeg命令分离视频 command_video = ['ffmpeg', '-i', input_file, '-an', output_video_file] subprocess.run(command_video, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) # 更新进度条 pbar.update(1) # 使用多线程处理文件并显示进度条 with ThreadPoolExecutor(max_workers=4) as executor: with tqdm(total=len(files)) as pbar: futures = [executor.submit(process_file, filename, pbar) for filename in files] for future in futures: future.result() # 确保捕获所有异常 print("批量处理完成!") ``` 最后修改:2024 年 10 月 07 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏