1.前言

Python+ffmpeg 多个ts文件合成MP4文件

当我们在在看腾讯视频或者其他网页上的视频时,浏览器会先缓冲ts文件到你本地.如果你想把这些ts转成mp4文件保存在本地,可以使用ffmpeg进行格式转换。

准备需要合成mp4而且是序号是排好序的ts文件。在合成前,在自行安装ffmpeg,Fmpeg 是领先的多媒体,能够解码、编码、转码、混合、解密、流媒体、过滤和播放人类和机器创造的几乎所有东西。它支持最晦涩的古老格式,直到最尖端的格式。

2开始编写/ target=_blank class=infotextkey>Python脚本

# -*- coding: utf-8 -*-
# @Time    : 2021/4/18 11:25
# @Author  : linwl

import os

shell_str =r'copy /b D:\work\convermp4\ts\*.ts C:\work\convermp4\new.ts'

file_name = "new.ts"

out_file_name ='E:\work\convermp4\test.mp4'

def staert():
    print('step1-----------------------')
    work_path = os.path.join(os.getcwd(), file_name)
    print('ts路径:'+work_path)
    cmd = "ffmpeg -i {} -c copy {}".format(work_path,out_file_name)
    os.system(cmd)
    print('done')
    print('---------------end--------------------')


if __name__ == '__main__':
    staert()

主要是使用win10 自带的命令copy /b 把多个ts文件合成一个新的ts文件,再执行ffmpeg命令把ts格式文件转换成mp4.这样就可以愉快地看电影了。

胜象大百科