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:
ffmpy3is a Python wrapper for FFmpegffmpy3complies 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_fileBatch 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:
- Use single quotes, not double quotes - the latter will cause errors!
- 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