12

I am trying to create a low latency CMAF video stream using FFMPEG. To do so, I would like to enable the lhls option in FFMPEG in order to have the #EXT-X-PREFETCH tag written in the HLS manifest.

From the FFMPEG doc :

https://www.ffmpeg.org/ffmpeg-all.html

Enable Low-latency HLS(LHLS). Adds #EXT-X-PREFETCH tag with current >segment’s URI. Apple doesn’t have an official spec for LHLS. Meanwhile >hls.js player folks are trying to standardize a open LHLS spec. The >draft spec is available in https://github.com/video-dev/hlsjs->rfcs/blob/lhls-spec/proposals/0001-lhls.md This option will also try >to comply with the above open spec, till Apple’s spec officially >supports it. Applicable only when streaming and hls_playlist options >are enabled. This is an experimental feature.

I am using the following command with FFMPEG 4.1 :

ffmpeg -re -i ~/Documents/videos/BigBuckBunny.mp4 \
    -map 0 -map 0 -map 0 -c:a aac -c:v libx264 -tune zerolatency \
    -b:v:0 2000k -s:v:0 1280x720 -profile:v:0 high \
    -b:v:1 1500k -s:v:1 640x340  -profile:v:1 main \
    -b:v:2 500k -s:v:2 320x170  -profile:v:2 baseline \
    -bf 1 \
    -keyint_min 24 -g 24 -sc_threshold 0 -b_strategy 0 -ar:a:1 22050 -use_timeline 1 -use_template 1 \
    -window_size 5 -adaptation_sets "id=0,streams=v id=1,streams=a" \
    -hls_playlist 1 -seg_duration 1 -streaming 1  -strict experimental -lhls 1 -remove_at_exit 1 \
    -f dash manifest.mpd

The kind of HLS manifest I obtained for a specific resolution :

#EXTM3U
#EXT-X-VERSION:6
#EXT-X-TARGETDURATION:1
#EXT-X-MEDIA-SEQUENCE:8
#EXT-X-MAP:URI="init-stream0.mp4"
#EXTINF:0.998458,
#EXT-X-PROGRAM-DATE-TIME:2019-06-21T18:13:56.966+0900
chunk-stream0-00008.mp4
#EXTINF:0.998458,
#EXT-X-PROGRAM-DATE-TIME:2019-06-21T18:13:57.964+0900
chunk-stream0-00009.mp4
#EXTINF:0.998458,
#EXT-X-PROGRAM-DATE-TIME:2019-06-21T18:13:58.963+0900
chunk-stream0-00010.mp4
#EXTINF:0.998458,
#EXT-X-PROGRAM-DATE-TIME:2019-06-21T18:13:59.961+0900
chunk-stream0-00011.mp4
#EXTINF:1.021678,
#EXT-X-PROGRAM-DATE-TIME:2019-06-21T18:14:00.960+0900
chunk-stream0-00012.mp4
...

As you can see the #EXT-X-PREFETCH tag is missing.

Any help would be highly appreciated.

Edit

I also compiled FFmpeg from its master branch by doing the following :

nasm

sudo apt-get install nasm mingw-w64

Codecs

sudo apt-get install libx265-dev libnuma-dev libx264-dev libvpx-dev libfdk-aac-dev libmp3lame-dev libopus-dev

FFmpeg

mkdir lhls
cd lhls 
git init 
git clone https://github.com/FFmpeg/FFmpeg.git
cd FFmpeg 
git checkout master

AOM (inside FFmpeg dir)

git -C aom pull 2> /dev/null || git clone --depth 1 https://aomedia.googlesource.com/aom && \
mkdir -p aom_build && \
cd aom_build && \
PATH="$HOME/bin:$PATH" cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -DENABLE_SHARED=off -DENABLE_NASM=on ../aom && \
PATH="$HOME/bin:$PATH" make && \
make install
cd..

Compiling

PATH="$HOME/bin:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure \
  --prefix="$HOME/ffmpeg_build" \
  --pkg-config-flags="--static" \
  --extra-cflags="-I$HOME/ffmpeg_build/include" \
  --extra-ldflags="-L$HOME/ffmpeg_build/lib" \
  --extra-libs="-lpthread -lm" \
  --bindir="$HOME/bin" \
  --enable-gpl \
  --enable-libaom \
  --enable-libass \
  --enable-libfdk-aac \
  --enable-libfreetype \
  --enable-libmp3lame \
  --enable-libopus \
  --enable-libvorbis \
  --enable-libvpx \
  --enable-libx264 \
  --enable-libx265 \
  --enable-nonfree && \
PATH="$HOME/bin:$PATH" make 

Unfortunately, the #EXT-X-PREFETCH is still missing in the HLS Manifest.

I also tried nightly builds from https://ffmpeg.zeranoe.com/builds/ , same result.

Any help would be highly appreciated.

EDIT 2 :resolved

Thanks to @aergistal and @Gyan , the #EXT-X-PREFETCH tag is now present in my HLS manifest.

Here the FFMPEG command I am using:

./ffmpeg -re -i ~/videos/BigBuckBunny.mp4 -loglevel debug \
  -map 0 -map 0 -map 0 -c:a aac -c:v libx264 -tune zerolatency \
  -b:v:0 2000k -s:v:0 1280x720 -profile:v:0 high -b:v:1 1500k -s:v:1 640x340  -profile:v:1 main -b:v:2 500k -s:v:2 320x170  -profile:v:2 baseline -bf 1 \
 -keyint_min 24 -g 24 -sc_threshold 0 -b_strategy 0 -ar:a:1 22050 -use_timeline 1 -use_template 1 -window_size 5 \
 -adaptation_sets "id=0,streams=v id=1,streams=a" -hls_playlist 1 -seg_duration 3 -streaming 1 \
 -strict experimental -lhls 1 -remove_at_exit 0 -master_m3u8_publish_rate 3 \
 -f dash -method PUT -http_persistent 1  https://example.com/manifest.mpd

Apparently the mime types are not passed to the server & FFmpeg seems to ignore the -headers option.

mehdi.r
  • 386
  • 1
  • 4
  • 20

2 Answers2

9

In the current implementation the lhls option doesn't work with file output. It'll work if you use another protocol like HTTP:

-f dash -method PUT http://example.com/live/manifest.mpd

See dash_write_packet in dashenc.c:

int use_rename = proto && !strcmp(proto, "file");
...
if (c->lhls) {
    char *prefetch_url = use_rename ? NULL : os->filename;
    write_hls_media_playlist(os, s, pkt->stream_index, 0, prefetch_url);
}
aergistal
  • 29,947
  • 5
  • 70
  • 92
  • Thanks a lot. I will post my results by tomorrow. I am still trying to find the correct options for http upload. I was not able to see the `#EXT-X-PREFETCH` yet. – mehdi.r Jun 25 '19 at 09:27
  • 1
    @mehdi.r You could also add another format option to supply the server URL and path for file output. It's just a few lines of code. – aergistal Jun 25 '19 at 09:40
2

I know this thread is 18 months old, but I have been using it to experiment with -lhls support in ffmpeg. I have set up a test harness using @medhi.r post as a roadmap, but I am not seeing the EXT-X-PREFETCH in my manifest (as he mentions he resolved above).

I have tried to duplicate as many as the applicable settings, but still not seeing the extra entry.

I don't really have an answer, but thought others might be investigating this and wanted to post on one of the only threads that helped me get as far as I have.

Here is my argument list to ffmpeg (which I built from a fresh pull from git this morning):

ffmpeg -r 25 -re -i pipe:0 -y -an -vcodec copy \
-seg_duration 3 -window_size 5  -init_seg_name init.m4s \
-media_seg_name '$Time$-$Number$.m4s'  \
-strict experimental -streaming 1 -hls_playlist 1 -tune zerolatency -lhls 1 -master_m3u8_publish_rate 3 \
-f dash -method PUT -http_persistent 1  http://192.168.xx.xx:yyyy/camera/manifest.mpd

I was hoping to use lhls to drive the latency down to below 2 seconds or so, but I am not really seeing any reduction in latency, which is consistently around 12-15 seconds. I am using this to pipe the video from the system camera.

JohnL
  • 21
  • 2
  • 1
    Hello, as I understand `EXT-X-PREFETCH` was a proposal from the video-dev team, it was not from Apple. Recently Apple updated its doc : https://developer.apple.com/documentation/http_live_streaming/enabling_low-latency_hls . *Hls.js* is implementing it already : https://github.com/video-dev/hls.js/search?q=EXT-X-PRELOAD-HINT . But sorry I don't how to output the new `EXT-X-PRELOAD-HINT` TAG with ffmpeg. – mehdi.r Dec 28 '20 at 01:42
  • Did you ever find a solution for this? - we have a similar problem and also seeing ~15 second delays despite having 1 second chunks. – Roman Gaufman Aug 10 '21 at 09:54
  • This is a great post and I am very interested in publishing low-latency streaming. I am attempting to implement but can't get http/s PUT to work. The terminal says it PUTs the files fine but that isn't really happening. If I change to `-f dash manifest.mpd` the files are created but there is no `EXT-X-PREFETCH` in the HLS manifest... – Sflagg Aug 16 '21 at 18:49
  • @mehdi.r Would is be easy to support EXT-X-PRELOAD-HINT? I'm looking to use Apple's LL-HLS with FFMPEG – Alexander Dec 12 '21 at 21:29
  • @Alexander As far as I understand, currently, the ffmpeg's lhls is different from Apple's ll-hls. Please look for the term 'lhls' in the following page: https://ffmpeg.org/ffmpeg-formats.html – mehdi.r Dec 15 '21 at 08:25