SoFunction
Updated on 2025-04-17

How to convert TS files to MP4 using Python

Environmental preparation

  1. 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.
  2. 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 usesubprocessModule to call the FFmpeg command. In addition, we can also useosmodule 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

  1. Import module
    • subprocess: Used to call external commands.
    • os: Used to check whether the file exists.
  2. Define functionsconvert_ts_to_mp4
    • parameterinput_file: Entered TS file path.
    • parameteroutput_file: The output MP4 file path.
    • Check whether the input file exists.
    • Build the FFmpeg command, use-c:v copyand-c:a copyOption to copy video and audio streams without reencoding.
    • use-movflags +faststartOptions optimize MP4 files to load faster when playing on the web.
    • useExecute the FFmpeg command and catch possible errors.
  3. Example usage
    • Defines the path to the input file and the output file.
    • Callconvert_ts_to_mp4Functions are converted.

Things to note

  1. FFmpeg path: Make sure FFmpeg has been added to the system's PATH environment variable, so that the Python script can be called directlyffmpegOrder.
  2. File permissions: Make sure that the input file is readable and the output file path is writable.
  3. 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!