Creating HLS variants with FFMPEG

2020-07-14 12:49发布

问题:

I am starting with a high res video file and I would like to create 3 variants, low quality, mid quality, and high quality for mobile streaming. I want these mid/low/high variants to be segmented into ts pieces that the m3u8 file will be pointing that. Is there a way to do this in one line in ffmpeg?

I have successfully generated an m3u8 file and ts segments with ffmpeg, do I need to do this 3x and set specs for low/mid/high? If so, how do I get the singular m3u8 file to point to all variants as opposed to one for each variant?

This is the command I used to generate the m3u8 file along with the ts segments.

ffmpeg -i C:\Users\george\Desktop\video\hos.mp4 -strict -2 -acodec aac -vcodec libx264 -crf 25 C:\Users\user\Desktop\video\hos_Phone.m3u8

回答1:

Yes, you need to encode all variants and generate the media playlists first (the playlists containing the segments).

If you want you can do it in one command since ffmepg supports multiple inputs/outputs. Eg:

ffmpeg -i input \
    ... [encoding parameters 1] ... output1 \
    ... [encoding parameters 2] ... output2 \
    ....[encoding parameters 3] ... output3

You must provide the variants in multiple qualities/bitrates but the aspect ratio should remain the same. Keeping the aspect ratio was initially mandatory but in the latest HLS authoring guide it's downgraded to a recommendation.

All variant streams must be keyframe aligned so set a GOP size using the -g option, disable scene-cut detection and use a segment duration hls_time which is a multiple of your keyframe interval.

Once you have all 3x m3u8 media playlist you can manually create the master playlist which points to each media playlist.

Example from the Apple HLS documentation, you must change the bandwidth, codecs, resolution and playlist filenames according to your own encoding options:

#EXTM3U
#EXT-X-VERSION:6
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2855600,CODECS="avc1.4d001f,mp4a.40.2",RESOLUTION=960x540
medium.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=5605600,CODECS="avc1.640028,mp4a.40.2",RESOLUTION=1280x720
high.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1755600,CODECS="avc1.42001f,mp4a.40.2",RESOLUTION=640x360
low.m3u8


回答2:

  • The Aspect ratio Does not have to be the same, that makes no sense. How could you know what the client can play?

    Aspect ratios are 4:3 for non-HD, 16:9 for a HD variants.

  • You don't want to do all your variants in one ffmpeg command if you need segment times to be consistent.
  • Also watch transcoding downward, if you go from 1080 to 360, there might be issues. One that I often get is that the audio degrades and sounds weird. I try to go down no more than half, if I want high quality.
  • @DavidC That hex is the codec version number.