FFmpeg command for mpeg-ts encoding

36,617

As always with FFmpeg there are a number of choices to make, and I have narrowed this down a little further to create a clear answer:

1. Your input file's codecs are supported in TS container:

Examine your input file with FFmpeg as follows:

ffmpeg -i input.mp4

If the file contains codecs that are well supported in a TS container, for example H.264 video and AAC sound, you can simply copy the streams across:

ffmpeg -i input.mp4 -c copy output.ts

This will give great results although you may need to vary this command line depending on your actual use of the output file (streaming, a certain playback device etc).

2. Your input file's codecs are not supported in a TS container:

If your input file contains codecs that are not well supported in a TS container you will need to re-encode either video or audio streams or both. The default codecs for FFmpeg and the TS container are mpeg2video and mp2 sound. If you are happy to go with these default codecs the following will give great results:

ffmpeg -i input.mp4 \
       -c:v mpeg2video -qscale:v 2 \
       -c:a mp2 -b:a 192k \
       output.ts

This certainly gave quite reasonable results on my system and should on yours as well...

3. You wish to segment your file for HTTP Live Streaming (HLS):

And finally you may wish to produce a segmented TS file with playlist for use with HTTP Live Streaming (HLS). There are as always several ways to accomplish this but the sample command line below will work well for input files whose codecs are supported in a TS container:

ffmpeg -re -i input.mp4 \
       -codec copy -map 0 \
       -f segment -segment_list playlist.m3u8 \
       -segment_list_flags +live -segment_time 10 \
       out%03d.ts

If you wish to alter the codecs of the input file simply add the required settings in place of -codec copy, I believe that H.264 and AAC are popular codecs for this type of streaming. A lot of room for experimentation with the segment options which are described here...

Notes:

Share:
36,617

Related videos on Youtube

Fayas B
Author by

Fayas B

Updated on September 18, 2022

Comments

  • Fayas B
    Fayas B over 1 year

    I have just started using FFmpeg encoder. I have a command to encode video files to mp4 containers:

    ffmpeg -i input.mp4 \
           -vcodec libx264 -s 426x240 -pix_fmt yuv420p \
           -crf 22 -minrate 200k -bufsize 400k -maxrate 400k -preset medium \
           -b:v 500000 -profile:v baseline -level 3.1 \
           -c:a aac -strict -2 -b:a 64k -ac 2 \
           output_240.mp4
    

    I need the corresponding command for mpeg-ts encoding. I searched for this online, but couldn't get exactly what I was looking for as I am new to this.

  • Fayas B
    Fayas B over 6 years
    Is there a command that is similar to the ffmpeg command in the question to create segmented ts files along with the playlist file?
  • andrew.46
    andrew.46 over 6 years
    @FayasB Sure is but I will need to experiment with that a little. I see some thoughts here: hlsbook.net/segmenting-video-with-ffmpeg that I will work on and add to the answer...
  • andrew.46
    andrew.46 over 6 years
    @FayasB Done! Hardest I have worked on a question on AskUbuntu for a while :)
  • user0103
    user0103 almost 5 years
    Just letting you know that your answer is extremely helpful. I refer to it while developing custom HTML5 video player, all good and thanks.
  • andrew.46
    andrew.46 almost 5 years
    @user0103 Great news :)
  • user1182988
    user1182988 over 4 years
    For those wondering, this copied the over-the-antenna captions as well.