当前位置: 首页 > news >正文

python 批量ts合并成一个mp4

首先,确保你已经安装了ffmpeg

然后再次保证所有ts文件放在同一个文件夹中,并且依次命名为 1.ts 、 2.ts 、 3.ts 、 4.ts 、 4.ts 。。。

Python完整代码如下:(ffmpeg_batch_merge_ts.py文件)

#!/usr/bin/python3
# -*- coding: UTF-8 -*-import os# # 设置包含.ts文件的目录。
# ts_directory = '/path/to/ts/files';# # 遍历目录中的所有.ts文件。
# for filename in os.listdir(ts_directory):
#     if filename.endswith('.ts'):
#         # 构建源文件和目标文件的路径。
#         source_path = os.path.join(ts_directory, filename);
#         target_path = os.path.join(ts_directory, os.path.splitext(filename)[0] + '.mp4');
#
#         # 构建ffmpeg命令并运行。
#         ffmpeg_command = f'ffmpeg -i "{source_path}" "{target_path}"';
#         os.system(ffmpeg_command);def is_no_ignore(ignore_id_tuple, the_id):for x in ignore_id_tuple:if x == the_id: return False;return True;# 定义函数。
def file_copy(source_path, target_path):print('file_copy');with open(source_path, "rb") as source_file:with open(target_path, "wb") as target_file:target_file.write(source_file.read());# 设置包含.ts文件的目录。
# ts_directory = '/path/to/ts/files';
ts_directory = '';
while len(ts_directory) == 0:# 在Python中, 你可以使用input()函数来获取键盘输入。ts_directory = input('请输入ts文件所在的目录: ');
os.system(f'cd "{ts_directory}" && pwd');
# 由于ffmpeg一次合并的ts数量不能过大, 否则合并失败, 因此需要限制。
ts_number_per_group = 100;
ts_count = 0;
while ts_count <= 0:try:ts_count = int(input('请输入ts的数量: '));except ValueError:print('[Error] 您输入的数量不是整数!')
# ts_ignore_id_list = [120, 121, 122, 123, 124, 455, 456, 457, 458, 459];
# ts_ignore_id_tuple = (120, 121, 122, 123, 124, 455, 456, 457, 458, 459);
ts_ignore_id_tuple = ();
if ts_count > ts_number_per_group:ts_group_count = (ts_count + ts_number_per_group - 1) // ts_number_per_group;print('分组组数:', ts_group_count);final_ffmpeg_input = '';for group_index in range(ts_group_count):group_id = group_index + 1;print('第', group_id, '组开始');ts_start_id = ts_number_per_group * group_index + 1;ts_end_id = ts_number_per_group * (group_index + 1);if ts_end_id > ts_count: ts_end_id = ts_count;ffmpeg_input = '';for ts_id in range(ts_start_id, ts_end_id):# print(group_id, ts_id);if is_no_ignore(ts_ignore_id_tuple, ts_id):ffmpeg_input += f'{ts_id}.ts|';else:print('Ignore_id', ts_id);else:if is_no_ignore(ts_ignore_id_tuple, ts_id):ffmpeg_input += f'{ts_end_id}.ts';else:ffmpeg_input = ffmpeg_input[0:len(ffmpeg_input) - 1];print(ffmpeg_input);if ffmpeg_input.find('|') != -1:# 一个组内ts合并。os.system(f'cd "{ts_directory}" && ffmpeg -i "concat:{ffmpeg_input}" -c copy temp_{group_id}.mp4');# 把 一个组内ts合并的mp4 转成 temp.ts 。os.system(f'cd "{ts_directory}" && ffmpeg -i temp_{group_id}.mp4 -codec copy -vbsf h264_mp4toannexb temp_{group_id}.ts');else:file_copy(f'{ts_directory}/{ffmpeg_input}', f'{ts_directory}/temp_{group_id}.ts');final_ffmpeg_input += f'temp_{group_id}.ts|';print('第', group_id, '组结束');final_ffmpeg_input = final_ffmpeg_input[0:len(final_ffmpeg_input) - 1];print(final_ffmpeg_input);# 把 合成的组temp.ts 再次合并。os.system(f'cd "{ts_directory}" && ffmpeg -i "concat:{final_ffmpeg_input}" -c copy target.mp4');
else:print('only one group');ffmpeg_input = '';for ts_id in range(1, ts_count):# print(group_id, ts_id);if is_no_ignore(ts_ignore_id_tuple, ts_id):ffmpeg_input += f'{ts_id}.ts|';else:print('Ignore_id', ts_id);ffmpeg_input += f'{ts_count}.ts';print(ffmpeg_input);# 一个组内ts合并。os.system(f'cd "{ts_directory}" && ffmpeg -i "concat:{ffmpeg_input}" -c copy target.mp4');print('track_main_exit');

相关文章:

  • Unity3D 基于YooAssets的资源管理详解
  • Web安全渗透攻防技术
  • 深度解读:Apache Kafka如何超越消息引擎的界限
  • echaerts图例自动滚动并隐藏翻页按钮
  • Android基础-性能优化
  • Iphone自动化指令每隔固定天数打开闹钟关闭闹钟(二)
  • Vue 跨平台性能优化十法
  • HLA高层体系结构1.0.0版本
  • 父组件调用子组件方法(组合式 API版)
  • 浅谈申请小程序地理位置权限的正确打开方式
  • (代码示例)使用setTimeout来延迟加载JS脚本文件
  • yolov5-ros模型结合zed2相机部署在 Ubuntu系统
  • MoE-LLaVA:为大型视觉-语言模型引入专家混合
  • Freeswitch-soundtouch-变声开发
  • 网络编程(八)
  • .pyc 想到的一些问题
  • Docker下部署自己的LNMP工作环境
  • RedisSerializer之JdkSerializationRedisSerializer分析
  • SpringCloud(第 039 篇)链接Mysql数据库,通过JpaRepository编写数据库访问
  • tweak 支持第三方库
  • 二维平面内的碰撞检测【一】
  • 浮动相关
  • 如何将自己的网站分享到QQ空间,微信,微博等等
  • 入门级的git使用指北
  • 手机app有了短信验证码还有没必要有图片验证码?
  • 微信公众号开发小记——5.python微信红包
  • 函数计算新功能-----支持C#函数
  • 没有任何编程基础可以直接学习python语言吗?学会后能够做什么? ...
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • ​软考-高级-系统架构设计师教程(清华第2版)【第20章 系统架构设计师论文写作要点(P717~728)-思维导图】​
  • #android不同版本废弃api,新api。
  • (6)【Python/机器学习/深度学习】Machine-Learning模型与算法应用—使用Adaboost建模及工作环境下的数据分析整理
  • (ZT)一个美国文科博士的YardLife
  • (翻译)Quartz官方教程——第一课:Quartz入门
  • (分类)KNN算法- 参数调优
  • (附源码)php投票系统 毕业设计 121500
  • (汇总)os模块以及shutil模块对文件的操作
  • (一)搭建springboot+vue前后端分离项目--前端vue搭建
  • (转)LINQ之路
  • . Flume面试题
  • .apk 成为历史!
  • .NET / MSBuild 扩展编译时什么时候用 BeforeTargets / AfterTargets 什么时候用 DependsOnTargets?
  • .net core使用EPPlus设置Excel的页眉和页脚
  • .NET Micro Framework 4.2 beta 源码探析
  • .NET 动态调用WebService + WSE + UsernameToken
  • .Net 中Partitioner static与dynamic的性能对比
  • .Net 转战 Android 4.4 日常笔记(4)--按钮事件和国际化
  • .net最好用的JSON类Newtonsoft.Json获取多级数据SelectToken
  • @基于大模型的旅游路线推荐方案
  • [ C++ ] STL---仿函数与priority_queue
  • []指针
  • [20140403]查询是否产生日志
  • [AIGC] SQL中的数据添加和操作:数据类型介绍
  • [Android学习笔记]ScrollView的使用
  • [Angular] 笔记 21:@ViewChild