Cytrogen 的个人博客

万圣节恶魔的领地

马上订阅 Cytrogen 的个人博客 RSS 更新: https://cytrogen.icu/atom.xml

Batch Merging TS Files Using ffmpy3

2025年2月21日 09:37

How to use Python's ffmpy3 package to batch merge TS files into a single MP4 file.

Before reading, you need to know:

  • ffmpy3 is a Python wrapper for FFmpeg
  • ffmpy3 complies FFmpeg command line based on provided parameters and options

Using ffmpy3

Installing ffmpy3 Package

Install using pip:

pip install ffmpy3

Simple ffmpy3 Example

import ffmpy3ff = ffmpy3.FFmpeg(    inputs={'input_file': 'parameter1'},    outputs={'output_file': 'parameter2'})

The final result is equivalent to entering in terminal:

FFmpeg parameter1 -i input_file parameter2 output_file

Batch Merging TS Files

Directory Structure

├───folder│    python_file.py│    file.txt│    fileA.ts│    fileB.ts│    fileC.ts│    fileD.ts...

file.txt

Write the TS filenames in file.txt:

file 'fileA.ts'file 'fileB.ts'file 'fileC.ts'file 'fileD.ts'

Note:

  1. Use single quotes, not double quotes - the latter will cause errors!
  2. Use relative paths within the quotes

Python File

Use this code to batch merge TS files:

ff = ffmpy3.FFmpeg(        inputs={f'file.txt': '-f concat'},        outputs={f'filename.mp4': '-c copy'})ff.run()

The above code is equivalent to entering in terminal:

FFmpeg -f concat -i file.txt -c copy filename.mp4