How can we transcode live rtmp stream to live hls stream using ffmpeg?

90,614

Solution 1

This is a short guide for HLS streaming with any input file or stream:

I am following user1390208's approach, so I use FFMPEG only to produce the rtmp stream which my server then receives to provide HLS. Instead of Unreal/Wowza/Adobe, I use the free server nginx with the rtmp module, which is quite easy to setup. This is how I do it in short: Any input file or stream -> ffmpeg -> rtmp -> nginx server -> HLS -> Client or more detailed:

input video file or stream (http, rtmp, whatever) --> ffmpeg transcodes live to x.264 + aac, outputs to rtmp --> nginx takes the rtmp and serves a HLS to the user (client). So on the client side you can use VLC or whatever and connect to the .m3u8 file which is provided by nginx.

  • I followed this setup guide for nginx.
  • This is my nginx config file.
  • This is how I use ffmpeg to transcode my input file to rtmp:

    ffmpeg -re -i mydirectory/myfile.mkv -c:v libx264 -b:v 5M -pix_fmt yuv420p -c:a:0 libfdk_aac -b:a:0 480k -f flv rtmp://localhost:12345/hls/mystream;
    

    (the .mkv is 1080p with 5.1 sound, depending on your input, you should use lower bitrates!)

Where do you get the rtmp stream from?

  • A file? Then you can use exactly my approach.
  • Any server X with a stream Y? Then you have to change the ffmpeg command to:

    ffmpeg -re -i rtmp://theServerX/yourStreamY -c:v libx264 -b:v 5M -pix_fmt yuv420p -c:a:0 libfdk_aac -b:a:0 480k -f flv rtmp://localhost:12345/hls/mystream;
    

    or if your rtmp stream is already h.264/aac encoded, you could try to use the copy option in ffmpeg to stream the content directly to nginx.

As you see in my nginx config file:

  • My rtmp server has an "application" called "hls". That's the part that describes where nginx listens to ffmpeg's rtmp stream and that's why ffmpeg streams to rtmp://localhost:12345/hls/mystream;
  • My http server has the location /hls. This means in VLC I can connect to http://myServer:80/hls/mystream.m3u8 to access the HLS stream.

Is everything clear? Happy streaming!

Solution 2

Try this RTMP to HLS command line settings:

ffmpeg -v verbose -i rtmp://<host>:<port>/<stream> -c:v libx264 -c:a aac -ac 1 -strict -2 -crf 18 -profile:v baseline -maxrate 400k -bufsize 1835k -pix_fmt yuv420p -flags -global_header -hls_time 10 -hls_list_size 6 -hls_wrap 10 -start_number 1 <pathToFolderYouWantTo>/<streamName>.m3u8

There might be some delay in the HLS feed. However, it'll work.

Solution 3

As an update to this question, I've managed to complete the live transcoding from RTMP to HLS without the use of ffmpeg, how?

Well just by using the exact same nginx config file shared by user3069376 and being very careful about the paths that you are generating the .m3uh manifesto, the hls option within the RTMP module should take care of it.

As for video player the Video.Js worked like a charm o

Share:
90,614
Kiran
Author by

Kiran

cloud enthusiast

Updated on August 31, 2020

Comments

  • Kiran
    Kiran almost 4 years

    I am trying to convert a live rtmp stream to hls stream on real time.

    I got some idea after reading

    http://sonnati.wordpress.com/2011/08/30/ffmpeg-%E2%80%93-the-swiss-army-knife-of-internet-streaming-%E2%80%93-part-iv/

    i am able to convert the live rtmp stream to hls but not at run time. when i run the command and test for any hsl files (.m3u8 and .ts) i am not able to see but when i interrupt the command and check there i get the hls files as required.

    I searched on google for solution but not able to get proper answer.

    Can any body help me?

    Thanks in Advance...

  • scaryguy
    scaryguy over 9 years
    This is a very helpful answer.
  • Mr MT
    Mr MT about 9 years
    Great answer ! Just what I needed.
  • Dr.jacky
    Dr.jacky almost 9 years
    Can I use RTSP/RTP for input stream in nginx?!
  • JorgeGarza
    JorgeGarza about 8 years
    can you please elaborate more or share a conf file ?
  • dCSeven
    dCSeven about 5 years
    I don't understand how you connect the location /hls to the application hls and get the stream out of /hls/mystream.m3u8 (at least it doesn't work for me that easy).
  • user3069376
    user3069376 about 5 years
    @dCSeven what doesn't work for you? Have you tried with my exact nginx config file and connect to it in VLC via URL mentioned above?
  • ROODAY
    ROODAY about 4 years
    Does this method make the HLS stream adapt bitrate/quality depending on the client/viewer's connection? Or would I need to have multiple ffmpeg calls and hls applications to serve that?
  • user3069376
    user3069376 about 4 years
    @ROODAY no, but you can make it work using hls_variant tags, see licson.net/post/setting-up-adaptive-streaming-with-nginx
  • Cristian Sepulveda
    Cristian Sepulveda about 4 years
    as I know, nginx use FFMPEG behind the scenes to transcode.
  • Alejandro Cotilla
    Alejandro Cotilla almost 4 years
    Great simple answer! Any way to reduce the latency?