Generate hls stream using h265 codec

6,243

Solution 1

I tested this with ffmpeg 4.1.1 today on macOS 10.14.3, and it seems to work beautifully. I do have to transcode to an MP4 initially (can't just go direct to HLS manifest during the transcode stage as the resulting m3u8 is unplayable in Safari – seems like a ffmpeg bug), but the packaging function appears to work perfectly well, and I am able to play the resulting HEVC HLS m3u8 (in UHDp60 HDR10 even – albeit with quite a bit of buffering on my iPhone XS Max and a non-tone-mapped display in macOS) in Safari on both macOS 10.14.3 and iOS 12.1.2.

As stated above, I first need to transcode to MP4 (the following example takes a UHDp60 HEVC HDR10 input and transcodes to 1080p60 HEVC HDR10):

ffmpeg -y -i source.mp4 -c:v libx265              \
    -tag:v hvc1 -pix_fmt yuv420p10le -s 1920x1080 \
    -x265-params "colorprim=bt2020:transfer=smpte2084:colormatrix=bt2020nc:bitrate=4000:keyint=120:strict-cbr" \
    -c:a copy ~/Sites/HLS/1080p/HDR10.mp4

From the resulting file, I can then do the HLS packaging:

ffmpeg -y -i ~/Sites/HLS/1080p/HDR10.mp4 -c copy \
    -hls_segment_type fmp4 -hls_time 6 -hls_list_size 10 \
    -hls_flags delete_segments+append_list+split_by_time \
    -hls_playlist_type vod ~/Sites/HLS/1080p/HDR10.m3u8

The output from these steps produces all the necessary files to be able to logon to my web server and automatically play the HLS playlist in Safari. Safari can even play streamed HEVC HDR10 MP4 files from a web server now. So I would suggest attempting to do that first – if your original MP4 doesn't play in Safari via the web server, then it is unlikely that an HLS packaging of it will either.

Solution 2

I've also been having problems with getting ffmpeg to generate a valid HLS playlist with HEVC/h265 files with version 4.3.2 that will play in Safari. VLC plays them just fine though. Upgrading to ffmpeg v4.4 also seems to completely break HLS generation for h264 files as well.

I ended up just letting ffmpeg transcode to hevc for me, and then used bento4 to fragment it (using bento's mp4fragment) and then generate the HLS playlist for me (using bento's mp4dash with the --hls option enabled)

Share:
6,243

Related videos on Youtube

formatkaka
Author by

formatkaka

Updated on September 18, 2022

Comments

  • formatkaka
    formatkaka almost 2 years

    I am trying to convert h264 video to an hls stream which uses h265 codec. As this bitmovin article suggests, we need to use fragmented mp4 for the hls/h265 stream to work on safari.

    1. I am able to convert h264 to h265 properly using this command

      ffmpeg -i input.mp4 -c:v libx265 -tag:v hvc1 out.mp4
      
    2. I am able to convert an input video (h264) to hls (fragmented mp4) using this command

      ffmpeg -y -i input.mp4 \
          -c copy -hls_segment_type fmp4 -hls_time 6 -hls_list_size 10 \
          -hls_flags delete_segments+append_list+split_by_time \ 
          -hls_playlist_type vod manifest.m3u8
      

    Now when I use the above command by specifying h265, the output hls stream does not work in Safari. It throws this error

    Plugin Handled Load
    

    Command

    ffmpeg -y -i input.mp4 \
        -vf scale=640:360 -c:v libx265 -tag:v hvc1 -c:a copy \
        -hls_segment_type fmp4 -hls_time 6 -hls_list_size 10 \
        -hls_flags delete_segments+append_list+split_by_time \
        -hls_playlist_type vod manifest.m3u8
    

    What might be the issue here?

    • Bryan Scott
      Bryan Scott over 5 years
      I've run into this as well. I've narrowed it down to an issue Safari has with the libx265 hevc encoder. If you use hevc_videotoolbox (with -allow_sw 0 or 1), it will properly encode and Safari will play it. It's a shame, because the quality of hevc_videotoolbox is pretty poor for bitrates under 5000k.
    • Bryan Scott
      Bryan Scott over 5 years
      Further searching has brought me to at least one problem Apple apps have with libx265: it puts an "hev1" tag instead of an "hvc1" tag in the mp4 container (see discussion in discussions.apple.com/thread/8091782). I tried overriding the tag (-tag:v hvc1) but that didn't fix the problem for Safari.
  • dravit
    dravit almost 3 years
    this doesn't seems to be working. The above command gives out a few files which are not supported on macOS.
  • dravit
    dravit almost 3 years
  • Chris
    Chris over 2 years
    Some notes: This flow works for me to get a Safari/Quicktime compatible HEVC HLS stream but it requires that I use ffmpeg 4.1.1 as noted. The latest ffmpeg from HEAD (2021-10-14) doesn't work nor does ffmpeg 4.4. Also I have to follow these steps exactly and not try to do transcoding and HLS generation in one step. If I change the final ffmeg HLS command to do -c:v libx265 instead of copy, the HLS doesn't work in Safari! Very weird and mysterious. Of course the HLS stream still works fine in mpv/IINA/VLC though. ¯_(ツ)_/¯