python 批量ts合并成一个mp4

打印 上一主题 下一主题

主题 525|帖子 525|积分 1579

首先,确保你已经安装了ffmpeg。
然后再次保证所有ts文件放在同一个文件夹中,而且依次命名为 1.ts 、 2.ts 、 3.ts 、 4.ts 、 4.ts 。。。

Python完备代码如下:(ffmpeg_batch_merge_ts.py文件)
  1. #!/usr/bin/python3
  2. # -*- coding: UTF-8 -*-
  3. import os
  4. # # 设置包含.ts文件的目录。
  5. # ts_directory = '/path/to/ts/files';
  6. # # 遍历目录中的所有.ts文件。
  7. # for filename in os.listdir(ts_directory):
  8. #     if filename.endswith('.ts'):
  9. #         # 构建源文件和目标文件的路径。
  10. #         source_path = os.path.join(ts_directory, filename);
  11. #         target_path = os.path.join(ts_directory, os.path.splitext(filename)[0] + '.mp4');
  12. #
  13. #         # 构建ffmpeg命令并运行。
  14. #         ffmpeg_command = f'ffmpeg -i "{source_path}" "{target_path}"';
  15. #         os.system(ffmpeg_command);
  16. def is_no_ignore(ignore_id_tuple, the_id):
  17.     for x in ignore_id_tuple:
  18.         if x == the_id: return False;
  19.     return True;
  20. # 定义函数。
  21. def file_copy(source_path, target_path):
  22.     print('file_copy');
  23.     with open(source_path, "rb") as source_file:
  24.         with open(target_path, "wb") as target_file:
  25.             target_file.write(source_file.read());
  26. # 设置包含.ts文件的目录。
  27. # ts_directory = '/path/to/ts/files';
  28. ts_directory = '';
  29. while len(ts_directory) == 0:
  30.     # 在Python中, 你可以使用input()函数来获取键盘输入。
  31.     ts_directory = input('请输入ts文件所在的目录: ');
  32. os.system(f'cd "{ts_directory}" && pwd');
  33. # 由于ffmpeg一次合并的ts数量不能过大, 否则合并失败, 因此需要限制。
  34. ts_number_per_group = 100;
  35. ts_count = 0;
  36. while ts_count <= 0:
  37.     try:
  38.         ts_count = int(input('请输入ts的数量: '));
  39.     except ValueError:
  40.         print('[Error] 您输入的数量不是整数!')
  41. # ts_ignore_id_list = [120, 121, 122, 123, 124, 455, 456, 457, 458, 459];
  42. # ts_ignore_id_tuple = (120, 121, 122, 123, 124, 455, 456, 457, 458, 459);
  43. ts_ignore_id_tuple = ();
  44. if ts_count > ts_number_per_group:
  45.     ts_group_count = (ts_count + ts_number_per_group - 1) // ts_number_per_group;
  46.     print('分组组数:', ts_group_count);
  47.     final_ffmpeg_input = '';
  48.     for group_index in range(ts_group_count):
  49.         group_id = group_index + 1;
  50.         print('第', group_id, '组开始');
  51.         ts_start_id = ts_number_per_group * group_index + 1;
  52.         ts_end_id = ts_number_per_group * (group_index + 1);
  53.         if ts_end_id > ts_count: ts_end_id = ts_count;
  54.         ffmpeg_input = '';
  55.         for ts_id in range(ts_start_id, ts_end_id):
  56.             # print(group_id, ts_id);
  57.             if is_no_ignore(ts_ignore_id_tuple, ts_id):
  58.                 ffmpeg_input += f'{ts_id}.ts|';
  59.             else:
  60.                 print('Ignore_id', ts_id);
  61.         else:
  62.             if is_no_ignore(ts_ignore_id_tuple, ts_id):
  63.                 ffmpeg_input += f'{ts_end_id}.ts';
  64.             else:
  65.                 ffmpeg_input = ffmpeg_input[0:len(ffmpeg_input) - 1];
  66.         print(ffmpeg_input);
  67.         if ffmpeg_input.find('|') != -1:
  68.             # 一个组内ts合并。
  69.             os.system(f'cd "{ts_directory}" && ffmpeg -i "concat:{ffmpeg_input}" -c copy temp_{group_id}.mp4');
  70.             # 把 一个组内ts合并的mp4 转成 temp.ts 。
  71.             os.system(
  72.                 f'cd "{ts_directory}" && ffmpeg -i temp_{group_id}.mp4 -codec copy -vbsf h264_mp4toannexb temp_{group_id}.ts');
  73.         else:
  74.             file_copy(f'{ts_directory}/{ffmpeg_input}', f'{ts_directory}/temp_{group_id}.ts');
  75.         final_ffmpeg_input += f'temp_{group_id}.ts|';
  76.         print('第', group_id, '组结束');
  77.     final_ffmpeg_input = final_ffmpeg_input[0:len(final_ffmpeg_input) - 1];
  78.     print(final_ffmpeg_input);
  79.     # 把 合成的组temp.ts 再次合并。
  80.     os.system(f'cd "{ts_directory}" && ffmpeg -i "concat:{final_ffmpeg_input}" -c copy target.mp4');
  81. else:
  82.     print('only one group');
  83.     ffmpeg_input = '';
  84.     for ts_id in range(1, ts_count):
  85.         # print(group_id, ts_id);
  86.         if is_no_ignore(ts_ignore_id_tuple, ts_id):
  87.             ffmpeg_input += f'{ts_id}.ts|';
  88.         else:
  89.             print('Ignore_id', ts_id);
  90.     ffmpeg_input += f'{ts_count}.ts';
  91.     print(ffmpeg_input);
  92.     # 一个组内ts合并。
  93.     os.system(f'cd "{ts_directory}" && ffmpeg -i "concat:{ffmpeg_input}" -c copy target.mp4');
  94. print('track_main_exit');
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

篮之新喜

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表