import subprocess import os from pathlib import Path def batch_convert(): # 创建输出目录 output_dir = Path("new") output_dir.mkdir(exist_ok=True) # 获取当前目录所有MP4文件 for file in Path(".").glob("*.mp4"): # 跳过已在输出目录的文件 if file.parent == output_dir: continue # 构建输出文件名 output_file = output_dir / f"{file.stem}-new.mp4" # 构建ffmpeg命令 cmd = [ "ffmpeg", "-i", str(file), "-s", "1280x720", "-r", "10", "-qmin", "30", "-qmax", "40", "-ab", "28k", "-ac", "1", "-y", str(output_file) ] try: subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print(f"转换完成:{output_file}") except subprocess.CalledProcessError as e: print(f"转换失败 {file}:{e.stderr.decode()}") if __name__ == "__main__": batch_convert()
转载请注明:运维博客 » python 使用 ffmpeg 批量压缩mp4的脚本