Creating HLS variants with FFMPEG

13,132

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
Share:
13,132
user2491463
Author by

user2491463

Updated on July 12, 2022

Comments

  • user2491463
    user2491463 almost 2 years

    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

  • David42
    David42 almost 8 years
    Neither your answer nor the Apple HLS documentation explain the meaning of the CODECS parameter. The hexadecimal number needs explanation. Without this, your answer is not usable.
  • aergistal
    aergistal almost 8 years
    @DavidC CODECS is not always required. The format is explained in RFC 6381 and CODECS usage is specified in the HLS draft.
  • Max Jacobson
    Max Jacobson over 6 years
    How is the BANDWIDTH value calculated? (It's not present in the input.) According to the spec it is supposed to be the peak bandwidth of the stream.
  • aergistal
    aergistal over 6 years
    @mjs it's the peak bitrate of the resulting transport stream, that is including the TS overhead. For VBR you can compute it based on the PCR deltas. You could also do constrained VBR and use the muxing overhead information displayed by ffmpeg for an educated guess.