How to set up your own private RTMP server using nginx

All Versions How to set up your own private RTMP server using nginx

Daica said:
Does anyone know how to install NGINX + RTMP Server when Apache is already installed on the VPS?
I don't want to make any changes to Apache because its currently hosting 1 website on it.
The reply after yours was the correct answer. Set up NGINX to use a port other than 80 and it'll work fine along side Apache, that's how I'm using it.

Took me a while to get nginx to start automatically when my VPS boots, but I found a init.d script for nginx that worked great :D

I couldn't figure out why my VPS kept rebooting all the time... it was because I was killing apache (to save on ram), lol. As long as I leave it running, everything is stable.

-Jamie M.
 

sneaky4oe

Member
sneaky4oe said:
paladzin said:
Thanks.
Using this vm for transcoding 1080>720p, and restreaming.
Install -> ffmpeg/x264 ppa
Code:
sudo add-apt-repository ppa:jon-severinsson/ffmpeg
sudo apt-get update && apt-get install ffmpeg x264

Transcoding example:
Code:
                     application live_orig {
                        live on;
                        record off;
                        push rtmp://sreaming.url/paladzin;
                        exec_static ffmpeg -re -i rtmp://localhost/live_orig -vcodec libx264 -vprofile baseline -preset veryfast -g 10 -s 1280x720 -acodec copy -f flv rtmp://streaming.url/paladzin~720p;
                                           }
Works great.

I didn't manage to apply this on a virtual machine from http://www.xedocproject.com/forum/index.php?/topic/16-мультистрим-без-нагрузки-на-процессор/ thread.
I am able to multistream now, but I need to transcode it to 480p. Could somebody please help?


7578bb4954b8.png

In a viewtopic.php?f=18&t=5873&start=30 thread I asked a question, but nobody answered. Could you tell what's wrong here? Can't start ffmpeg transcoding. If I try to do it via console, it goes (framerate 2000 is bigger than 24, blah-blah). Tried to limit it, but didn't work, as well as changing preset.
 

Jack0r

The Helping Squad
OK: In OBS you setup rtmp://localhost/live in the server field and a streamkey in the Playpath/Streamkey field.
Now for ffmpeg to find this stream you have to give it the full info:
-i rtmp://localhost/live/streamkey
without the streamkey info the server does not know which stream to send to ffmpeg as far as I know :) (even if there is only one stream)
 

sneaky4oe

Member
Checked it out with default string from the example, but got messages about quality. Changed "-preset" to "-vpre", didn't work - started complaining about framerate and missing preset file. Added a "-framerate 24" parameter and changed "480p" to "720" and even "1080". Still don't work.

Removing "-vpre" will give "unknown encoder 'libx264'" error. Maybe there's something with it? Performed "sudo apt-get install x264", but same story.

"-vpre faster" gives me framerate error and "Uncategorised option 'preset' " error...

T_T


http://s001.radikal.ru/i195/1312/66/ef15c41386ff.png
ef15c41386ff.png
 

Jack0r

The Helping Squad
I would first of all recommend you to either manually update your ffmpeg version or switch to avconv on ubuntu (as that is hopefully updated and not deprecated, see the yellow message you get each time). Then I normally sat up my ffmpeg like this:
ffmpeg -i rtmp://localhost/live1/streamkey -vcodec libx264 -preset veryfast -x264opts nal-hrd=cbr:force-cfr=1:keyint=60 -b:v 2000k -maxrate 2000k -bufsize 2000k -s 1280x720 -sws_flags spline -r 30 -acodec copy -f flv rtmp://localhost/live2/streamkey;

But this will only work in an updated version of ffmpeg.
 

sneaky4oe

Member
Jack0r said:
I would first of all recommend you to either manually update your ffmpeg version or switch to avconv on ubuntu (as that is hopefully updated and not deprecated, see the yellow message you get each time). Then I normally sat up my ffmpeg like this:
ffmpeg -i rtmp://localhost/live1/streamkey -vcodec libx264 -preset veryfast -x264opts nal-hrd=cbr:force-cfr=1:keyint=60 -b:v 2000k -maxrate 2000k -bufsize 2000k -s 1280x720 -sws_flags spline -r 30 -acodec copy -f flv rtmp://localhost/live2/streamkey;

But this will only work in an updated version of ffmpeg.


Thanx a lot for a working string! I updated my ffmpeg version step by step from this manual: https://trac.ffmpeg.org/wiki/UbuntuCompilationGuide
After I did it (1 hour of hand written commands, dammit!) it all worked out! I only changed the bitrate and resolution to 854x480.

Thank you. But for some reason, transcoding doesn't start through nginx after I do "exec_static" to that string. Do you know why this might be happening?
 

sneaky4oe

Member
For now I've made a small walkaround with ./start file, in which there are 2 commands:
Code:
sudo service nginx restart
ffmpeg -i rtmp://localhost/live/streamkey -vcodec libx264 -preset veryfast -x264opts nal-hrd=cbr:force-cfr=1:keyint=60 -b:v 800k -maxrate 1200k -bufsize 1600k -s 854x480 -sws_flags spline -r 30 -acodec copy -f flv rtmp://remotehost/live/streamkey

I'm still in need of a way to keep the second string in the config file.
 

Jack0r

The Helping Squad
exec_static = Similar to exec but runs specified command at nginx start. Does not support substitutions since has no session context.

So I would probably try to use the normal exec in the config, which also supports substitutions if needed:
exec ffmpeg -i rtmp://localhost/$app/$name ....

This will be executed as soon as you send a stream to the application which you put the exec into and for each stream you send to this application.
 

sneaky4oe

Member
It won't start even without static parameter. I have no idea why. I even tried to add "-re" parameter from xedox's string, but didn't work.
I can stream to my main channel and simultaniously transcode to another, but transcoding will begin only after I run a string manually. It still doesn't work in /nginx/conf/nginx.conf file. :-(
 

Jack0r

The Helping Squad
Make sure you have the error logging configured at the start of your conf file, I like to use a different name for the logfile:
error_log logs/rtmp_error.log debug;
is the line I use. Then just start a single stream that should trigger the exec and see the rtmp_error.log afterwards
 

Jack0r

The Helping Squad
I start streaming:
Code:
2013/12/26 18:14:06 [info] 1004#0: *1 client connected '192.168.0.2'
2013/12/26 18:14:06 [info] 1004#0: *1 connect: app='live1' args='' flashver='FMLE/3.0 (compatible; FMSc/1.0)' swf_url='rtmp://192.168.0.3/live1' tc_url='rtmp://192.168.0.3/live1' page_url='' acodecs=0 vcodecs=0 object_encoding=0, client: 192.168.0.2, server: 0.0.0.0:1935
2013/12/26 18:14:06 [info] 1004#0: *1 createStream, client: 192.168.0.2, server: 0.0.0.0:1935
2013/12/26 18:14:06 [info] 1004#0: *1 publish: name='1080' args='' type=live silent=0, client: 192.168.0.2, server: 0.0.0.0:1935

The exec gets called...
Code:
2013/12/26 18:14:06 [info] 1004#0: *1 exec: starting managed child 'ffmpeg', client: 192.168.0.2, server: 0.0.0.0:1935

...and connects as a new client to receive the stream...
Code:
2013/12/26 18:14:06 [info] 1004#0: *2 client connected '192.168.0.3'
2013/12/26 18:14:06 [info] 1004#0: *2 connect: app='live1' args='' flashver='LNX 9,0,124,2' swf_url='' tc_url='rtmp://192.168.0.3:1935/live1' page_url='' acodecs=4071 vcodecs=252 object_encoding=0, client: 192.168.0.3, server: 0.0.0.0:1935
2013/12/26 18:14:06 [info] 1004#0: *2 createStream, client: 192.168.0.3, server: 0.0.0.0:1935
2013/12/26 18:14:06 [info] 1004#0: *2 play: name='1080' args='' start=-2000 duration=0 reset=0 silent=0, client: 192.168.0.3, server: 0.0.0.0:1935

and then starts sending the stream with a second connection (Client3):
Code:
2013/12/26 18:14:08 [info] 1004#0: *3 client connected '192.168.0.3'
2013/12/26 18:14:08 [info] 1004#0: *3 connect: app='live2' args='' flashver='FMLE/3.0 (compatible; Lavf55.22' swf_url='' tc_url='rtmp://192.168.0.3:1935/live2' page_url='' acodecs=0 vcodecs=0 object_encoding=0, client: 192.168.0.3, server: 0.0.0.0:1935
2013/12/26 18:14:08 [info] 1004#0: *3 createStream, client: 192.168.0.3, server: 0.0.0.0:1935
2013/12/26 18:14:08 [info] 1004#0: *3 publish: name='720' args='' type=live silent=0, client: 192.168.0.3, server: 0.0.0.0:1935

both streams are running for around 3 minutes before I stop streaming with OBS:
Stream *1 gets deleted, exec is terminating ffmpeg, client is disconnected and deletestream send a second time, the notice and alert show ffmpeg closed
Code:
2013/12/26 18:17:05 [info] 1004#0: *1 deleteStream, client: 192.168.0.2, server: 0.0.0.0:1935
2013/12/26 18:17:05 [info] 1004#0: *1 exec: terminating child 2380, client: 192.168.0.2, server: 0.0.0.0:1935
2013/12/26 18:17:05 [info] 1004#0: *1 disconnect, client: 192.168.0.2, server: 0.0.0.0:1935
2013/12/26 18:17:05 [info] 1004#0: *1 deleteStream, client: 192.168.0.2, server: 0.0.0.0:1935
2013/12/26 18:17:05 [notice] 1004#0: signal 17 (SIGCHLD) received
2013/12/26 18:17:05 [alert] 1004#0: unknown process 2380 exited on signal 9
Now there is no client more watching on *2:
2013/12/26 18:17:05 [info] 1004#0: *2 recv() failed (104: Connection reset by peer), client: 192.168.0.3, server: 0.0.0.0:1935
And no client *3 streaming anymore, so both get disconnected and the streams deleted:
Code:
2013/12/26 18:17:05 [info] 1004#0: *3 disconnect, client: 192.168.0.3, server: 0.0.0.0:1935
2013/12/26 18:17:05 [info] 1004#0: *3 deleteStream, client: 192.168.0.3, server: 0.0.0.0:1935
2013/12/26 18:17:05 [info] 1004#0: *2 disconnect, client: 192.168.0.3, server: 0.0.0.0:1935
2013/12/26 18:17:05 [info] 1004#0: *2 deleteStream, client: 192.168.0.3, server: 0.0.0.0:1935
In your log I couldnt find the exec being called, although you have a second client connecting as a viewer, which should be your ffmpeg. It stops receiving after 3-4 seconds and a process exits, but then a client connects?! but its missing the play or publish info line.

I am not really sure whats wrong atm, you could upload us your whole config once (of course replace the streamkeys) and maybe a new rtmp_error.log in text form (the screenshots are a bit hard to read)
 

sneaky4oe

Member
Well... Since my virtual machine is performed in console only mode, I have no idea how to upload you my config. I could make a snapshot with different streamkeys of it and upload it somewhere, if you would like to test it yourself... Because I have no idea how to mount network catalogues and stuff. :-/
 

Jack0r

The Helping Squad
Hmm: http://answers.oreilly.com/topic/79-how ... ine-tools/
if you like to learn how to do it over the console :D

But I would probably recommend to start up a desktop manager to just copy the files to your main system that runs the vm, I personally just used light-dm until everything was setup correctly and worked, then told linux to boot in text-mode to free up all ressources. The conf file in textform is the most important part, so I get your exact version.

From afar I can only say that if the ffmpeg line works fine on your ./start file it should work the same if nginx calls it. You might have to compile nginx with the debug option to get more exact error messages or talk to arut directly on his google group.
 

mamouth

New Member
Hi !

Great tutorial !

but when you said :
Code:
push rtmp://<other streaming service rtmp url>/<stream key>

I forgot to add the ";" after so it always gave me errors, maybe you should say it too for beginners like me.

And when you say this :
Code:
Streaming Service: Custom
Server: rtmp://<your server ip>/live
Play Path/Stream Key: test

If you run nginx on any other port, you should NOT write it when it ask the server ip.

This is two mistake I made as a beginners. I hope it will help others if they had some errors.
 

Daica

New Member
Hey quick question :
I have a ubuntu server I rented and I setup nginx rtmp on it and was able to stream to it pretty smoothly. The server has a 1gigabjt shared connection. Then I thought if I setup ubuntu on my own computer and stream directly to that it should have the same speed right? But instead it lagged like crazy.

So my question is, why can't I stream smoothly from/to my own computer?

Is it because my upload speed is too low? (4MB)
Why is my stream so much better if I stream to the ubuntu server?
Thanks for your guide, it helped me a lot
 

dodgepong

Administrator
Forum Admin
When you say that it lagged like crazy, what do you mean? Did OBS drop a lot of frames? Or was playback choppy?
 
Top