Streaming from Icecast to Facebook Live with ffmpeg on Ubuntu 16.04

3.6k Views Asked by At

I have a webradio streamed by Liquidsoap+Icecast on a DigitalOcean droplet (Ubuntu 16.04), and I want to combine this audio stream with a simple jpeg image with ffmpeg, transform it to a video stream and send it to Facebook live.

Facebook Live specifications :

Video Format :

We accept video in maximum 720p (1280 x 720) resolution, at 30 frames per second. (or 1 key frame every 2 seconds). You must send an I-frame (keyframe) at least once every two seconds throughout the stream.. Recommended max bit rate is 4000 Kbps. Titles must be less than 255 characters otherwise the stream will fail. The Live API accepts H264 encoded video and AAC encoded audio only.

Video Length :

240 minute maximum length, with the exception of continuous live (see above). 240 minute maximum length for preview streams (either through Live dialog or publisher tools). After 240 minutes, a new stream key must be generated.

Advanced Settings :

Pixel Aspect Ratio: Square. Frame Types: Progressive Scan. Audio Sample Rate: 44.1 KHz. Audio Bitrate: 128 Kbps stereo. Bitrate Encoding: CBR.

And the ffmpeg command I tried :

ffmpeg -loop 1 -i radio-background.jpg -thread_queue_size 20480 -i http://localhost:8000/radio -framerate 30 -r 30 -acodec aac -strict -2 -c:v libx264 -strict experimental -b:a 128k -pix_fmt yuvj444p -x264-params keyint=60 -b:v 256k -minrate 128k -maxrate 512k -bufsize 768k -f flv 'rtmp://rtmp-api.facebook.com:80/rtmp/<fb-streaming-key>'

This is actually working, as Facebook receives the live video and allows me to publish it. But I can't figured out why there is a lag almost every 2 or 3 seconds. I asked different people to watch the test video, and everyone gets the same problem : every 2 or 3 seconds the playing "freezes" for half a second and seems to load the video, I even can see the loading icon spinning on the screen.

I tried different combinations of values for the following options : -thread_queue_size / -b:v / -minrate / -maxrate / -bufsize. Nothing seems to produce any change.

Video streaming is new for me, I'm not really confortable with the options listed before, so I think I'm missing something here...

Also, note that the icecast audio stream perfectly works, and according to DigitalOcean graphs, the server is not overloaded. So I think my ffmpeg command is wrong.

What ffmpeg parameters would be working for that case?

1

There are 1 best solutions below

0
On

specify a frame rate for the image. this would go before the input item.

-r 30 -loop 1 -i radio-background.jpg

if your radio stream is is already aac you can just stream copy, there is no need to re-encode the audio. you can use -c:a copy.

-c:a copy

if you still want to use aac you should switch to using libfdk_aac. ffmpeg by default uses 128k bitrate for audio so there is no need to specify -b:a

-c:a libfdk_aac

ffmpeg will use the input framerate of the first item for the output by default so you dont need to specify anymore frame rates. (you have the output frame rate specified twice. -framerate 30 and -r 30 are the same)

ultrafast preset for better CPU performance, tune, and pixel format. you can also use -g for the keyent.

-c:v h264 -preset ultrafast -tune stillimage -pix_fmt yuvj444p -g 60

set the profile and profile level, bframes

-profile:v high444 -level 4.2

use either -b:v or -minrate -maxrate -bufsize but not both.

-b:v 768k

and out we go

-f flv rtmp://rtmp-api.facebook.com:80/rtmp/streamkey

now to put it all together

    ffmpeg -r 30 -loop 1 -i radio-background.jpg \
-i http://localhost:port/mount -c:a libfdk_aac -c:v h264 -b:v 768k \
-preset ultrafast -tune stillimage -pix_fmt yuvj444p -g 60 \
-profile:v high444 -level 4.2 -f flv rtmp://rtmp-api.facebook.com:80/rtmp/streamkey