I'm trying to stream on Youtube with a cloud ubuntu 22.04 instance, but it freezes after no more than 30 mins or so, the ffmpeg process keeps running in the background, and Youtube is keeping the stream up, no video, no music, it's just there loading, frozen, I kill the process and rerun it with nohup
in the background, and it freezes again after a short amount of time, the log file from nohup
contains only warnings of things like:
[libx264 @ 0xaaaaee6feb60] VBV is incompatible with constant QP, ignored.
which I think are not really important, but I see in the log that it exits normally:
Exiting normally, received signal 15.
I'm using this bash script to stream:
#! /bin/bash
VBR="4500k"
FPS="24"
QUAL="ultrafast"
YOUTUBE_URL="rtmp://a.rtmp.youtube.com/live2"
KEY="XXXX-XXXX-XXXX-XXXX-XXXX"
VIDEO_SOURCE="/home/ubuntu/video.mp4"
AUDIO_SOURCE="/home/ubuntu/audio.mp3"
ffmpeg \
-re -f lavfi -i "movie=filename=$VIDEO_SOURCE:loop=0, setpts=N/(FRAME_RATE*TB)" \
-thread_queue_size 512 -i "$AUDIO_SOURCE" \
-map 0:v:0 -map 1:a:0 \
-map_metadata:g 1:g \
-vcodec libx264 -pix_fmt yuv420p -preset $QUAL -crf 0 -r $FPS -g $(($FPS * 2)) -b:v $VBR \
-acodec libmp3lame -ar 44100 -threads 6 -crf 0 -b:a 320000 -bufsize 512k \
-f flv \
-flvflags no_duration_filesize \
"$YOUTUBE_URL/$KEY"
I'm unexperienced in this, so if anyone knows a solution, please explain in details if possible and with commands.
So after some research, I found an answer, which is that I can do an infinite while loop that checks if the streaming process is working or not, and runs it if it's not with this script:
but, before doing that, I'll need to run the script in a tmux
session, so that I can leave it running and keep using the terminal outside of that session.
#! /bin/bash
VBR="4500k"
FPS="24"
QUAL="ultrafast"
YOUTUBE_URL="rtmp://a.rtmp.youtube.com/live2"
KEY="XXXX-XXXX-XXXX-XXXX-XXXX"
VIDEO_SOURCE="/home/Cinos/video.mp4"
while true;
do
if ! pgrep -x "ffmpeg" &> /dev/null 2>&1;
then
while read line; do
AUDIO_NAME="$line"
done < song.txt;
AUDIO_SOURCE="/home/Cinos/$AUDIO_NAME.mp3"
ffmpeg \
-re -stream_loop -1 -i "$VIDEO_SOURCE" \
-stream_loop -1 -i "$AUDIO_SOURCE" \
-drop_pkts_on_overflow 1 \
-attempt_recovery 1 \
-recover_any_error 1 \
-map 0:v:0 -map 1:a:0 \
-map_metadata:g 1:g \
-vcodec libx264 -pix_fmt yuv420p -preset $QUAL -r $FPS -g $(($FPS * 2)) -b:v $VBR \
-acodec libmp3lame -ar 44100 -threads 6 -b:a 160k -bufsize 6000k \
-f flv \
-flvflags no_duration_filesize \
"$YOUTUBE_URL/$KEY"
else
sleep 5
fi
done