使用python利用ffmpeg合并mp4文件

    科技2024-08-01  29

    talk is cheap, show you the code.

    import subprocess from pathlib import Path def merge_mp4(mp4_dir: Path, output_mp4=None): '''使用ffmpeg拼接文件夹中的mp4视频 :param mp4_dir: 存放mp4的文件夹 :param output_mp4: 输出的mp4文件,若没有指定则使用文件夹的名字 :return: None ''' mp4_list = [str(m) for m in mp4_dir.glob("*.mp4")] if output_mp4 is None: output_mp4 = mp4_dir / ("%s.mp4" % mp4_dir.stem) txt = ("file \'{}\'\n"*len(mp4_list)).format(*mp4_list) txt_file = mp4_dir / "video.txt" txt_file.write_text(txt) cmd = "ffmpeg -f concat -safe 0 -i %s -c copy %s" % (txt_file, output_mp4) print(cmd) subprocess.run(cmd) if __name__ == '__main__': mp4_dir = Path("D:\\video\\test") merge_mp4(mp4_dir)
    Processed: 0.011, SQL: 8