Converting .ts to .mp4 or .mkv using ffmpeg in Linux



Introduction

If you are working with video files, you may come across the .ts (Transport Stream) format, which is commonly used for digital television broadcasting. However, this format is not widely supported by media players and devices, so you may need to convert it to a more widely recognized format such as .mp4 or .mkv. In this article, we will show you how to use ffmpeg in Linux to convert .ts files to .mp4 or .mkv.



Step 1: Install ffmpeg First, you need to install ffmpeg on your Linux machine. You can do this by running the following command:

sudo apt-get install ffmpeg

on Arch

sudo pacman -S ffmpeg

    FFmepg Github: https://github.com/FFmpeg/FFmpeg


Step 2: Convert a single .ts file to .mp4 or .mkv To convert a single .ts file to .mp4 or .mkv, you can use the following command:


ffmpeg -i input_file.ts -c:v libx264 -preset medium -crf 23 -c:a copy output_file.mp4


This command uses the libx264 video codec to encode the video with a medium preset and a CRF (Constant Rate Factor) value of 23, which determines the video quality. The audio stream is copied without re-encoding using the -c:a copy option.

To convert to .mkv instead of .mp4, simply change the output file extension to .mkv.

Step 3: Convert multiple .ts files using a for loop If you have multiple .ts files that you want to convert, you can use a for loop to automate the process. Here's an example:


for file in *.ts; do ffmpeg -i "$file" -c:v libx264 -preset medium -crf 23 -c:a copy "${file%.*}.mp4" done

This for loop iterates through all the .ts files in the current directory and converts each one to .mp4 using the same ffmpeg command as in Step 2. The "${file%.*}.mp4" syntax is used to generate the output file name based on the input file name.


Quickly Convert:


for file in *.ts; do
ffmpeg -i "$file" -c copy "${file%.ts}.mp4"
done

Conclusion

Converting .ts files to .mp4 or .mkv is a simple process using ffmpeg in Linux. With the commands provided in this article, you can convert a single file or multiple files using a for loop. Whether you are working with digital television broadcasting or just have some .ts files that you want to watch on your media player, ffmpeg makes it easy to convert to a more widely recognized format.

Share:

0 Comments:

Post a Comment