Saturday, August 29, 2020

Convert .ts file to MP4 (also MP4 to MP3)

This is a post about how to convert .TS files to MP4 files using ffmpeg. It is a note to self since I probably will need this some time.
 
This will stream copy (re-mux) all streams:

ffmpeg -i input -map 0 -c copy output.mp4
 
Note: If your input's format is not compatible with MP4 you will get an error. Also, your player/device may not support all arbitrary, less common, or legacy formats even if they are supported by MP4. If in doubt, re-encode to H.264 + AAC as shown below.

This will re-encode the video to H.264 and stream copy the audio:

ffmpeg -i input.ts -c:v libx264 -c:a copy output.mp4

This example will re-encode video to H.264 and audio to AAC:

ffmpeg -i input.ts -c:v libx264 -c:a aac output.mp4

Lossless H.264 conversion (lossless files will be huge):

ffmpeg -i input.ts -c:v libx264 -crf 0 -c:a copy output.mp4

Source: Ubuntu – How to convert .ts file into a mainstream format losslessly
 
BTW, to join two separate .ts files, one with audio and one with video:
 
ffmpeg -i input_video.ts -i input_audio.ts -c copy output.mp4

will join these two files to form a single output file, assuming the video and audio files are the same length.

And to convert MP4 to MP3:
mkdir outputs
for f in *.mp4; do ffmpeg -i "$f" -c:a libmp3lame "outputs/${f%.mp4}.mp3"; done

This is work with spaces in the filename too.

No comments: