Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable LHLS in FFMPEG 4.1?

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.

like image 987
mehdi.r Avatar asked Jun 21 '19 09:06

mehdi.r


2 Answers

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);
}
like image 105
aergistal Avatar answered Oct 29 '22 10:10

aergistal


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.

like image 36
JohnL Avatar answered Oct 29 '22 08:10

JohnL