How to write a custom bash script that will keep checking the stream health

363 views Asked by At

I want to write a custom bash script that will act on stream health and in case of an error will try to fix or restart the stream

I am trying to stream a live video using the following command:

ffmpeg -re -i input.ts or .m3u8 -c:v libx264 -c:a libmp3lame -ar 44100 -ac 1 -f flv rtmp://ip/application name/stream key

My stream automatically closes after some time and shows the error below:

[flv @ 0x5c1d580] Failed to update header with correct duration.
[flv @ 0x5c1d580] Failed to update header with correct filesize.

Below is the full error detail -

[flv @ 0x5c1d580] Failed to update header with correct duration.
[flv @ 0x5c1d580] Failed to update header with correct filesize.
frame= 2810 fps= 11 q=-1.0 Lsize=   23375kB time=00:01:52.43 bitrate=1703.1kbits/s speed=0.45x
video:22374kB audio:879kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.527018%
[libx264 @ 0x5c27400] frame I:13    Avg QP:18.87  size: 78912
[libx264 @ 0x5c27400] frame P:871   Avg QP:22.12  size: 17876
[libx264 @ 0x5c27400] frame B:1926  Avg QP:27.19  size:  3279
[libx264 @ 0x5c27400] consecutive B-frames:  4.9%  9.5%  5.2% 80.4%
[libx264 @ 0x5c27400] mb I  I16..4:  9.2% 72.5% 18.3%
[libx264 @ 0x5c27400] mb P  I16..4:  2.3%  6.3%  0.7%  P16..4: 42.3% 11.9%  6.6%  0.0%  0.0%    skip:30.0%
[libx264 @ 0x5c27400] mb B  I16..4:  0.1%  0.2%  0.0%  B16..8: 38.6%  2.7%  0.5%  direct: 0.6%  skip:57.2%  L0:43.8% L1:52.9% BI: 3.4%
[libx264 @ 0x5c27400] 8x8 transform intra:67.8% inter:78.0%
[libx264 @ 0x5c27400] coded y,uvDC,uvAC intra: 48.8% 68.1% 23.0% inter: 8.1% 10.7% 0.7%
[libx264 @ 0x5c27400] i16 v,h,dc,p: 17% 40% 17% 26%
[libx264 @ 0x5c27400] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 14% 25% 29%  4%  6%  5%  6%  4%  6%
[libx264 @ 0x5c27400] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 20% 24% 20%  4%  7%  7%  6%  5%  5%
[libx264 @ 0x5c27400] i8c dc,h,v,p: 51% 26% 18%  5%
[libx264 @ 0x5c27400] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x5c27400] ref P L0: 69.6% 13.2% 13.8%  3.5%
[libx264 @ 0x5c27400] ref B L0: 91.3%  7.7%  1.1%
[libx264 @ 0x5c27400] ref B L1: 97.8%  2.2%
[libx264 @ 0x5c27400] kb/s:1630.03

Please help me how to debug this?

1

There are 1 answers

0
AAber On

You can start with this script, it will try the streaming command 10 times and report success or failure:

#!/bin/bash

RETRY=10
RUN_STREAM="ffmpeg -re -i input.ts or .m3u8 -c:v libx264 -c:a libmp3lame -ar 44100 -ac 1 -f flv rtmp://ip/a"
EXIT_CODE=1

for i in $(seq 1 $RETRY)
do
    $RUN_STREAM
    if [ $? -eq 0 ]; then
            echo Stream sent Ok
            EXIT_CODE=0
            break
    else
            echo Try number $i failed, will try again
            continue
    fi
done
if [ $EXIT_CODE -eq 1 ]; then
    echo Streaming failed.
fi