Search code examples
pythonnginxffmpegrtmpnginx-config

Restreaming another nginx rtmp stream


First of all, I'll explain what I want to achieve, because there might be even better way to achieve this. I am looking at making 2 streams work, one is pushing to twitch.tv and youtube with delay and other one is live (without delay) that can be watched using VLC or whatever.

I am able to achieve this partially, but "live" stream just breaks sometimes randomly with this error:

Failed to update header with correct duration.
Failed to update header with correct filesize.

Before, I had "Could not find codec parameters" error, but I solved it by adding this to my ffmpeg command:

-analyzeduration 2147483647 -probesize 2147483647

What I've already done is:

I made these rtmp server and apps in my nginx.conf

rtmp {
server {
    listen 1935;
    chunk_size 4096;

    application delay_live {
        live on;
        record off;
        push_reconnect 500ms;

        push rtmp://live-vie.twitch.tv/app/my_stream_key;
        push rtmp://a.rtmp.youtube.com/live2/my_stream_key;

    }

    application live {
                    live on;
                    record off;
            }

    application delay {
        live on;
        record all;
        record_path /tmp/nginx;

        # Work with timestamp to know when to continue streaming
        record_suffix .flv;
        record_unique on;

        # Work with signals to know when to continue streaming
        #record_append on;

        exec_publish sudo sh /home/start.sh;
    }

    exec_static mkdir /tmp/nginx;   #Working dir. Must be consistend with the delayer.py
    }
}

On exec_publish I run this .sh script:

sudo screen -dmS delay bash -c "python /usr/local/nginx/sbin/delay/rtmp_stream_delayer.py; sleep 9999";
sleep 0.5;
sudo screen -dmS live bash -c "python /usr/local/nginx/sbin/live/rtmp_stream_live.py; sleep 9999";

Those two python scripts are a little bit changed script from this git:

https://github.com/sistason/rtmp_stream_delayer Few things I changed there is I used ffmpeg instead of avconv to call the commands, and inside rtmp_stream_live.py I've set the same directory/file as rtmp_stream_delayer.py (so it basically uses same .flv file to stream live). rtmp_stream_live.py has delay set to 0. Also I added -analyzeduration 2147483647 -probesize 2147483647 to my live stream ffmpeg call to avoid codec errors I previously had.

Full ffmpeg calls that I use:

rtmp_stream_delayer.py

subprocess.check_output('ffmpeg -re -i {0} -codec copy -f flv {1}'.format(filename, "rtmp://my_ip:port/delay_live").split())

rtmp_stream_live.py

subprocess.check_output('ffmpeg -re -analyzeduration 2147483647 -probesize 2147483647 -i /tmp/nginx/{0} -codec copy -f {1}'.format(filename, STREAM_DESTINATION).split())

I tried adding this ffmpeg flag, but it didn't help at all (codec errors are back again):

 flv -flvflags no_duration_filesize

Delay stream works like a charm and without any problems, but live stream randomly stops with update header errors, I haven't been able to trigger it myself, it just happens randomly!

Thanks in advance!


Solution

  • I ended up with simply pushing stream to another application. For some reason, I thought this wasn't allowed... So my config just looks like this now:

    rtmp {
    server {
       listen 1935;
       chunk_size 4096;
    
       application delay_live {
           live on;
           record off;
           push_reconnect 500ms;
    
           push rtmp://live-vie.twitch.tv/app/my_stream_key;
           push rtmp://a.rtmp.youtube.com/live2/my_stream_key;
    
       }
    
       application live {
                      live on;
                      record off;
              }
    
       application delay {
           live on;
           record all;
           record_path /tmp/nginx;
    
           # Work with timestamp to know when to continue streaming
           record_suffix .flv;
           record_unique on;
    
           # Work with signals to know when to continue streaming
           #record_append on;
    
           push rtmp://localhost:1935/live;
           exec_publish sudo sh /home/start.sh;
           }
    
       exec_static mkdir /tmp/nginx;   #Working dir. Must be consistend with the delayer.py
       }
    }
    

    And I just have run delay stream with my python script using start.sh and opened live stream in vlc using rtmp://localhost:1935/live/stream_key