Creating HLS variants with FFMPEG

早过忘川 提交于 2019-12-04 11:05:57

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
  • 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.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!