1

Have a script that generates a rtmp stream that runs inside a screen using ffmpeg but this fails for some reason? If it run it directly in the command line it works so I tried to run it by opening > screen , running it and closing the screen with ctrl + d but even so it sometimes closes for reason.

Is there a way to log the stderr from ffmpeg inside the script to see why the command fails inside the script but works when run directly?

#!/bin/bash

if [ "$1" = "run" ]; then
        source $HOME/.bash_aliases
        while [ 1 ]; do 
ffmpeg -f x11grab -s 1000x563 -framerate 30 -i $DISPLAY+10,151 -f pulse -ac 2 -i default -c:v libx264 -preset ultrafast -tune zerolatency -x264opts keyint=30:min-keyint=10 -b:v 2000k -pix_fmt yuv420p -s 1000x562 -c:a aac -b:a 160k -ar 44100 -t '4:00:00' -threads 0 -f flv rtmp://IPADDRESS:1935/live1/$CHANNEL
        done
else
        killall -9 ffmpeg > /dev/null 2>&1
        sleep 5s;
        screen -ls | grep ffmpeg > /dev/null 2>&1
        if [ "$?" = "1" ]; then
                screen -mdS ffmpeg $0 run
        fi
fi

Thank you ~

2
  • Is this your full script? How does it know the value of $CHANNEL? – nxnev Jan 5 '18 at 9:07
  • $channel is defined in $HOME/.bash_aliases – C.Astraea Jan 5 '18 at 9:15
3

Is there a way to log the stderr from ffmpeg inside the script to see why the command fails inside the script but works when run directly?

Here you go, forward stream 2, stderr, to a file in tmp. Oh, and because you do not want it to overwrite the tmp file all the time, add the PID to its name:

my_command 2> /tmp/ffmpeg_error$$.log

Now, applied to your ffmpeg:

ffmpeg -f x11grab -s 1000x563 -framerate 30 -i $DISPLAY+10,151 -f pulse -ac 2 -i default -c:v libx264 -preset ultrafast -tune zerolatency -x264opts keyint=30:min-keyint=10 -b:v 2000k -pix_fmt yuv420p -s 1000x562 -c:a aac -b:a 160k -ar 44100 -t '4:00:00' -threads 0 -f flv rtmp://IPADDRESS:1935/live1/$CHANNEL 2> /tmp/ffmpeg_error$$.log
1

Without more information the first thing that comes to my mind is that when you run the script you probably have a minimal environment. Are you running it from cron? If so you may not have access to $CHANNEL for example.

In any case, you may call your script using 2> to redirect standard error output to a file. For example:

./script.sh > result.log 2> error.log

If you are calling ffmpeg from a script then you should add redirections at the end of your ffmpeg command inside that script. You would do something like this inside your script:

ffmpeg (your-params-here) > result.log 2> error.log
4
  • Generated the files but are empty. For the moment just running it from the command line ./script – C.Astraea Jan 5 '18 at 9:17
  • Modified my answer to clear up that you should add redirections at the end of your ffmpeg command – Calabacin Jan 5 '18 at 11:10
  • 2>> error.log – Hauke Laging Jan 6 '18 at 15:52
  • @HaukeLaging 2>> can be used to add at the end of a file instead of deleting de contents and start at the beginning, but I do not think he wants that. – Calabacin Jan 17 '18 at 14:44
0

Try the -nostdin flag to ffmpeg.

Although possibly not directly related to the top-posters original question, this is the top search result at the moment for "ffmpeg fails from within script", so I'm sharing my finding anyway in the hopes that someone else is helped.

I hit this whenever running ffmpeg from within a cat 'file' | while read INFILE ; do loop. Running ffmpeg outside of that loop or by itself would work fine. Adding the loop would cause it to dump data to console. I'm not exactly sure why but adding -nostdin cured my issue, as I think the while read and ffmpeg were fighting over stdin. I'm open to hearing other peoples explanations as to why -nostdin worked in my case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.