1

需求:将十几个MP4视频文件合并为1个视频

思路:

  • 先将MP4文件转化为同样编码形式的ts流(ts流支持concate)
  • 第二步,连接(concate)ts流
  • 最后,把连接好的ts流转化为MP4.


脚本1


ffmpeg -i 0.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 0.ts;
ffmpeg -i 1.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 1.ts;
ffmpeg -i 2.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 2.ts;
ffmpeg -i 3.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 3.ts;
ffmpeg -i 4.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 4.ts;
ffmpeg -i 5.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 5.ts;
ffmpeg -i 6.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 6.ts;
ffmpeg -i 7.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 7.ts;
ffmpeg -i 8.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 8.ts;
ffmpeg -i 9.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 9.ts;
ffmpeg -i 10.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 10.ts;
ffmpeg -i 11.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 11.ts;
ffmpeg -i 12.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 12.ts;
ffmpeg -i 13.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 13.ts;
ffmpeg -i 14.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 14.ts;
ffmpeg -i 15.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 15.ts;
ffmpeg -i 16.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 16.ts;

ffmpeg -i "concat:0.ts|1.ts|2.ts|3.ts|4.ts|5.ts|6.ts|7.ts|8.ts|9.ts|10.ts|11.ts|12.ts|13.ts|14.ts|15.ts" -acodec copy -vcodec copy -absf aac_adtstoasc FileName.mp4;


脚本2: Python

生成脚本1:

# 设置起始和结束的数字
start = 0
end = 16

# 输出的文件名
output_file = "output_commands.sh"


def ff_cmd():
    with open(output_file, "a") as file:
        for i in range(start, end + 1):
            command = f"ffmpeg -i {i}.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb {i}.ts;\n"
            file.write(command)


def ff_cmd2():
    # 定义输入文件列表并构建命令
    input_files = [f"{i}.ts" for i in range(end)]
    input_files_str = "|".join(input_files)
    command = f'ffmpeg -i "concat:{input_files_str}" -acodec copy -vcodec copy -absf aac_adtstoasc FileName.mp4;'

    # 将命令写入到文件中
    with open(output_file, "a") as file:
        file.write(command)


ff_cmd()
ff_cmd2()



DEFAULT
19 声望2 粉丝