Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perfect FFMPEG configuration to publish on RTMP Nginx #758

Closed
antoniogiordano opened this issue Feb 1, 2016 · 16 comments
Closed

Perfect FFMPEG configuration to publish on RTMP Nginx #758

antoniogiordano opened this issue Feb 1, 2016 · 16 comments

Comments

@antoniogiordano
Copy link

Hi everybody,
and thanks arut for your wonderful library.
I correctly configured RTMP Nginx, with this configuration file:

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
worker_rlimit_nofile 400000;

error_log logs/error.log debug;

events {
    worker_connections 8192;
    use epoll;
    multi_accept on;
}

rtmp {

    server {

        listen 1935;

        chunk_size 4000;

        # TV mode: one publisher, many subscribers
        application mytv {

            # enable live streaming
            live on;

            # record first 1K of stream
            record all;
            record_path /tmp/av;
            #record_max_size 1000K;

            # append current timestamp to each flv
            record_unique on;

            # publish only from localhost
            allow publish all;
            #deny publish all;

            allow play all;
        }
    }
}

Now i m using this FFMpeg library for Android https://github.com/bytedeco/javacpp to publish the content from my android camera to my rtmp server.

recorder = new FFmpegFrameRecorder(ffmpeg_link, imageWidth, imageHeight, 1);

        // H264
        recorder.setVideoCodec(28);
        recorder.setFormat("flv");
        recorder.setFrameRate(30);
        recorder.setSampleRate(sampleAudioRateInHz);

I correctly start the streaming to my :1935/mytv/ok channel, and the rtmp correctly writes the stream in the /tmp/av direcotry.

I download the .flv file (after ~2 minutes of recording) and i play it with VLC.
BUT, the video stream, after ~15 seconds, brokes.
For "brokes" i mean the video freezes.

Generally, the audio is in slow motion.

So, my question is, do you have a ffmpeg command line configuration that works very good with RTMP Nginx? The lower the resolution, the better (i want to test it with very low bandwith stress)

Thanks

@sceptic30
Copy link

try this

rtmp {

    server {

        listen 1935;

        chunk_size 4000;

        # TV mode: one publisher, many subscribers
        application mytv {

            # enable live streaming
            live on;
            interleave on;
            wait_key on;

            # record first 1K of stream
            record all;
            record_path /tmp/av;
            #record_max_size 1000K;

            # append current timestamp to each flv
            record_unique on;

            # publish only from localhost
            allow publish all;
            #deny publish all;

            allow play all;
            exec /opt/ffmpeg/bin/ffmpeg -threads 0 -i rtmp://server_ip/mytv/$name -vcodec libx264 -vprofile main -tune zerolatency -g 10 -s 1366x768 -pix_fmt yuv420p -movflags +faststart -preset ultrafast -acodec libfdk_aac -ar 44100 -ac 1 -f flv rtmp://server_ip/live/$name;
        }
        application live {
            live on;
            interleave on;
            wait_key on;
        }
    }
}

Replace /opt/ffmpeg/bin/ffmpeg with the actual absolute path to your ffmpeg executable.
what this does is that allow ffmpeg to take the rtmp stream from the nginx from application mytv , convert it , then send it back to nginx to application live (transcoded) for transmission to connected clients.
This config is optimized for minimal cpu usage, with no decrementation in visual quality.
Report back when you test it

@antoniogiordano
Copy link
Author

Hi sceptic, thanks for your hint.

I configured it like that:

rtmp {

    server {

        listen 1935;

        chunk_size 4000;

        # TV mode: one publisher, many subscribers
        application mytv {

            # enable live streaming
            live on;
            interleave on;
            wait_key on;

            # record first 1K of stream
            record all;
            record_path /tmp/av;
            #record_max_size 1000K;

            # append current timestamp to each flv
            record_unique on;

            # publish only from localhost
            allow publish all;
            #deny publish all;

            allow play all;
            exec /usr/bin/ffmpeg -threads 0 -i rtmp://*.*.net/mytv/$name -vcodec libx264 -vprofile main -tune zerolatency -g 10 -s 1366x768 -pix_fmt yuv420p -movflags +faststart -preset ultrafast -acodec libfdk_aac -ar 44100 -ac 1 -f flv rtmp://*.*.net:1935/live/$name;
        }
        application live {
            live on;
            interleave on;
            wait_key on;
        }
    }
}

I just hide my real domain for security reason.

Now, if i download the result flv file from /tmp/av, i can correctly play it (i also fixed audio problem from my Android client) with no freezeing, UNLESS i try to move the VLC cursor and change video position!

Instead, if i open the RTMP stream directly, VLC buffer bar seems to continuously try to buffer something, but never play it.

My bandwith sould be good enough to publish and play from the same connection, (20 Mbit, 8 Mbit down/up), but nothing.

Could it be a keyframe problem? Does VLC search for keyframes to start the stream? Maybe i still have problems on input stream?

@sceptic30
Copy link

Well from what i have tested so far, vlc screws everything up when it comes to speed. My streams play really really slow in vlc and some times i see flickering, but my steam runs perfectly on browser. Try setting up a viewer page with jwplayer, or flash.swf from the test folder , so you will have a more "real life world" testing environment.

@antoniogiordano
Copy link
Author

Neither with JW player online, the same problem, infinite buffering loop. :-(

@antoniogiordano
Copy link
Author

Update, in vod the stream works, i added this:

        # video on demand for mp4 files
        application vod2 {
            play /tmp/av;
        }

and i can correctly stream from VLC the file previously published with my android.

So, the last piece is the live connection, any hints? :-(

@ibizawold
Copy link

What version of vlc are you trying to play rtmp stream?

On Wednesday, 3 February 2016, Antonio notifications@github.com wrote:

Update, in vod the stream works, i added this:

    # video on demand for mp4 files
    application vod2 {
        play /var/mp4s;
    }

and i can correctly stream from VLC the file previously published with my
android.

So, the last piece is the live connection, any hints? :-(


Reply to this email directly or view it on GitHub
#758 (comment)
.

@antoniogiordano
Copy link
Author

Version 2.2.1 Terry Pratchett (Weatherwax) (Intel 64bit).

Neither JW player online worked, anyway

@ibizawold
Copy link

Windows O/S?

On Wednesday, 3 February 2016, Antonio notifications@github.com wrote:

Version 2.2.1 Terry Pratchett (Weatherwax) (Intel 64bit).

Neither JW player online worked, anyway


Reply to this email directly or view it on GitHub
#758 (comment)
.

@antoniogiordano
Copy link
Author

OSX Yosemite 10.10

@ibizawold
Copy link

Ok I know in Windows VLC 2.1.5 32bit on x64 system plays rtmp fine but
later versions have stutter etc. Also if playing streams from rtmp server
in hls VLC 2.0.8 32bit plays flawless but later versions are erratic and
for hls the ffmpeg transcode needs to be baseline too. Never tried your O/S
but maybe try earlier version of VLC.

On Wednesday, 3 February 2016, Antonio notifications@github.com wrote:

OSX Yosemite 10.10


Reply to this email directly or view it on GitHub
#758 (comment)
.

@antoniogiordano
Copy link
Author

Previous versions are even worst, VLC returns an error when i try to stream the recorder video. This is the log:

main debug: Creating an input for 'rtmp://*.*.net:1935/vod2/ok-1454430619.flv'
main debug: using timeshift granularity of 50 MiB, in path '/tmp'
main debug: `rtmp://*.*.net:1935/vod2/ok-1454430619.flv' gives access `rtmp' demux `' path `*.*.net:1935/vod2/ok-1454430619.flv'
main debug: creating demux: access='rtmp' demux='' location='*.*.net:1935/vod2/ok-1454430619.flv' file='(null)'
main debug: looking for access_demux module: 0 candidates
main debug: no access_demux module matched "rtmp"
main debug: TIMER module_need() : 0.155 ms - Total 0.155 ms / 1 intvls (Avg 0.155 ms)
main debug: creating access 'rtmp' location='*.*.net:1935/vod2/ok-1454430619.flv', path='(null)'
main debug: looking for access module: 1 candidate
avcodec debug: Opening 'rtmp://*.*.net:1935/vod2/ok-1454430619.flv'
avcodec error: Failed to open url using libavformat
main debug: no access module matching "rtmp" could be loaded
main debug: TIMER module_need() : 0.217 ms - Total 0.217 ms / 1 intvls (Avg 0.217 ms)
main error: open of `rtmp://*.*.net:1935/vod2/ok-1454430619.flv' failed
main debug: finished input
main debug: dead input
main debug: TIMER input launching for 'rtmp://*.*.net:1935/vod2/ok-1454430619.flv' : 25.102 ms - Total 25.102 ms / 1 intvls (Avg 25.102 ms)
main debug: changing item without a request (current 0/1)
main debug: nothing to play

@ibizawold
Copy link

I think it's a case of trial and error with all versions. I find versions
that don't work well in Windows work perfect in Linux but it must be
something to do with the display parameters.

On Wednesday, 3 February 2016, Antonio notifications@github.com wrote:

Previous versions are even worst, VLC returns an error when i try to
stream the recorder video. This is the log:

main debug: Creating an input for 'rtmp://..net:1935/vod2/ok-1454430619.flv'
main debug: using timeshift granularity of 50 MiB, in path '/tmp'
main debug: rtmp://*.*.net:1935/vod2/ok-1454430619.flv' gives accessrtmp' demux ' path..net:1935/vod2/ok-1454430619.flv'
main debug: creating demux: access='rtmp' demux='' location='..net:1935/vod2/ok-1454430619.flv' file='(null)'
main debug: looking for access_demux module: 0 candidates
main debug: no access_demux module matched "rtmp"
main debug: TIMER module_need() : 0.155 ms - Total 0.155 ms / 1 intvls (Avg 0.155 ms)
main debug: creating access 'rtmp' location='..net:1935/vod2/ok-1454430619.flv', path='(null)'
main debug: looking for access module: 1 candidate
avcodec debug: Opening 'rtmp://..net:1935/vod2/ok-1454430619.flv'
avcodec error: Failed to open url using libavformat
main debug: no access module matching "rtmp" could be loaded
main debug: TIMER module_need() : 0.217 ms - Total 0.217 ms / 1 intvls (Avg 0.217 ms)
main error: open of `rtmp://ubiatar.cloudapp.net:1935/vod2/ok-1454430619.flv' failed
main debug: finished input
main debug: dead input
main debug: TIMER input launching for 'rtmp://..net:1935/vod2/ok-1454430619.flv' : 25.102 ms - Total 25.102 ms / 1 intvls (Avg 25.102 ms)
main debug: changing item without a request (current 0/1)
main debug: nothing to play


Reply to this email directly or view it on GitHub
#758 (comment)
.

@antoniogiordano
Copy link
Author

I don't think it's a VLC problem, because either VLC and JW player act the same.

Also because i don't know if once the packets go out from the server, there is any difference between a VOD against a LIVE (except latency maybe).

I'm pretty sure the problem should be on the publish part, from my android app or in the server, keeping in mind that i can actually watch the flv already published from the /tmp/av directory

@antoniogiordano
Copy link
Author

Ok, i didn't know i need
rtmp_auto_push on;
with multi workers!

Now it works.

Thanks everybody

@surabhi1694
Copy link

hey i want to send live video stream to rtmp server from android coding and display the live strem by calling rtmp url so ... please tell me how to write my config file for nginx please.................

@Hemant0601
Copy link

Hemant0601 commented Feb 26, 2020

@surabhi1694
Add this to your nginx file and restart , you can remove record options if dont needed.

rtmp {
        server {
                listen 1935;
                chunk_size 4096;

                application live {
                        live on;
                        record all;
                        record_path /var/www/html/recordings/;
                        record_unique on;
                        record_append on;
                        record_max_size 100M;
                        record_interval 90m;
                    
                }
        }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants