Environmental preparation
- Install Python: Make sure that Python is already installed on your system. You can download and install the latest version of Python from the official Python website.
- Install FFmpeg:FFmpeg is an open source multimedia framework that can be used to record, convert and stream audio and video. You can download and install FFmpeg from the official FFmpeg website.
Install the necessary Python libraries
To simplify operations, we can usesubprocess
Module to call the FFmpeg command. In addition, we can also useos
module to process file paths. If you have not installed these libraries, you can install them through the following command:
pip install subprocess os
Convert TS file to MP4
Here is a simple Python script for converting TS files to MP4 files:
import subprocess import os def convert_ts_to_mp4(input_file, output_file): """ Use FFmpeg to convert TS files to MP4 files. :param input_file: The input TS file path :param output_file: The output MP4 file path """ if not (input_file): print(f"Enter a file {input_file} Does not exist") return # Build the FFmpeg command command = [ 'ffmpeg', '-i', input_file, # Enter a file '-c:v', 'copy', # Video encoder is set to copy '-c:a', 'copy', # Audio encoder is set to copy '-movflags', '+faststart', # Optimize MP4 files for easy network playback output_file # Output file ] try: # Execute the FFmpeg command (command, check=True) print(f"Successfully {input_file} Convert to {output_file}") except as e: print(f"Conversion failed: {e}") # Example usageinput_file = '' output_file = 'example.mp4' convert_ts_to_mp4(input_file, output_file)
Code explanation
-
Import module:
-
subprocess
: Used to call external commands. -
os
: Used to check whether the file exists.
-
-
Define functions
convert_ts_to_mp4
:- parameter
input_file
: Entered TS file path. - parameter
output_file
: The output MP4 file path. - Check whether the input file exists.
- Build the FFmpeg command, use
-c:v copy
and-c:a copy
Option to copy video and audio streams without reencoding. - use
-movflags +faststart
Options optimize MP4 files to load faster when playing on the web. - use
Execute the FFmpeg command and catch possible errors.
- parameter
-
Example usage:
- Defines the path to the input file and the output file.
- Call
convert_ts_to_mp4
Functions are converted.
Things to note
-
FFmpeg path: Make sure FFmpeg has been added to the system's PATH environment variable, so that the Python script can be called directly
ffmpeg
Order. - File permissions: Make sure that the input file is readable and the output file path is writable.
- Error handling: In actual applications, it is recommended to add more error handling logic, such as checking whether FFmpeg is installed, processing special characters in the file path, etc.
in conclusion
With the above steps, you can easily convert TS files to MP4 files using Python and FFmpeg. This method is not only simple and efficient, but also suitable for various scenarios, such as video processing, streaming media conversion, etc. Hope this article is helpful to you!
This is the article about how to convert TS files to MP4 using Python. For more related content to convert Python TS files to MP4, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!