python ts video to mp4

First explain the Ts format

TS (TransportStream, transport stream) is an encapsulated format, its full name is MPEG2-TS. MPEG2-TS is a standard data container format that transmits and stores audio and video, program and system information protocol data. It is mainly used in digital broadcast systems, such as DVB, ATSC and IPTV. Transport streams were originally designed for broadcasting. Later, this packet was made into a 192-byte packet by adding 4 bytes of time code (TC) to the standard 188-byte packet, making it suitable for digital cameras, recorders, and players.

Handle the scene

Crawling the videos of a certain fish and some websites will get a lot of video files ending in ts format. At this time, they need to be synthesized into a Ts video file or MP4 format video. My own computer cannot play the Ts suffix format video. So I converted the Ts suffix format video to MP4.

Directly use the os built-in module. The following is the code:

import os


def merge_ts_video(ts_path, ts_path_):
    all_ts = os.listdir(ts_path)
    # 最好是对all_ts 进行排序处理一下
    # 我这里获取到后直接对ts视频文件进行了排序处理,所以没有加排序操作
    for file in all_ts:
        with open(ts_path + file, 'rb') as f1:  # 读取视频以二进制进行处理
            with open(ts_path_ + "VideoName.mp4", 'ab') as f2:  # 存储到指定位置,VideoName为变量值
                f2.write(f1.read())
        os.remove(os.path.join(ts_path, file))  # 将每次处理后的ts视频文件进行删除


merge_ts_video(r"./VideoPreliminaryStorage/", r"./VideoFinalStorage/")
# 函数调用:merge_ts_video
# 参数值:
#       参数1 存放 ts 的路径 VideoPreliminaryStorage
#       参数2 存放 mp4 的路径 VideoFinalStorage

Although the above content can convert Ts format videos to MP4 format, some players cannot recognize it. For example: a certain audio file
is MP4 after the conversion, but the format of the video has changed to: media file (.mp4) (.mp4),
so it is still Using ffmpeg

"""
mp4 -->  ts  : ffmpeg -i 1.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 1.ts
ts  -->  mp4 : ffmpeg -i 2.ts -acodec copy -vcodec copy -absf aac_adtstoasc output.mp4
"""

First merge all Ts format videos into one Ts format file, and then convert to mp4 after the merger is completed.

def merge_ts_video(ts_path, ts_path_, ffmpeg_path, video_name):
    all_ts = os.listdir(ts_path)
    all_ts_mp4 = [ts_path + file for file in all_ts]
    for all_ in all_ts_mp4:
        with open(all_, 'rb') as f1:
            with open(ts_path + f"{
      
      video_name}.ts", 'ab') as f2:
                f2.write(f1.read())
        # 删除多余ts文件
        os.remove(os.path.join(all_))

    # Ts视频转MP4
    new_ts = os.listdir(ts_path)
    cmd = ffmpeg_path + " -i " + f"{
      
      ts_path + new_ts[0]}" + " -acodec copy -vcodec copy -absf aac_adtstoasc " + ts_path_ + "\\" + f"{
      
      video_name}.mp4"
    os.popen(cmd)

    # 删除多余ts文件,清空文件夹(VideoPreliminaryStorage)
    os.remove(video_name + '.ts')
    for i in range(0, len(all_ts_mp4)):
        ts_name = os.path.basename(all_ts_mp4[i])  # 去掉文件名前面的文件路径
        mp4_name = os.path.splitext(ts_name)[0]  # 去掉文件名的后缀
        """
        mp4 -->  ts  : ffmpeg -i 1.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 1.ts
        ts  -->  mp4 : ffmpeg -i 2.ts -acodec copy -vcodec copy -absf aac_adtstoasc output.mp4
        """
        # # 拼接好运行ffmpeg的命令行语句
        cmd = ffmpeg_path + " -i " + all_ts_mp4[
            i] + " -acodec copy -vcodec copy -absf aac_adtstoasc " + ts_path_ + "\\" + mp4_name + ".mp4"
        os.popen(cmd)
# 函数调用:merge_ts_video
# 参数值:
#       参数1 存放 ts 的路径 ts_path
#       参数2 存放 mp4 的路径 ts_path_
#       参数3 ffpmpeg 的路径 ffmpeg_path
#       参数4 视频存储 的名称 video_name

Guess you like

Origin blog.csdn.net/weixin_43603846/article/details/128903008