ffmpeg running as a background task [duplicate] - linux

This question already has answers here:
ffmpeg hangs when run in background
(3 answers)
Closed 1 year ago.
I am trying to get ffmpeg to run as a background task in linux, I have been trying to do it like this
ffmpeg -i input.mkv -loglevel panic output.mp4 &
But whenever I run the program it halts in the beginning until I put it as a foreground task fg $1 and then I can leave the task and it runs in the background just fine, how can I make it run from the beginning as a background task without having to put it as a foreground task temporarily?
I have also tried this and it didn't work
ffmpeg -i input.mkv -loglevel panic output.mp4 2> /dev/null &

I just needed to supply some sort of input to ffmpeg so it would run, I just piped nothing into ffmpeg and it runs as a background task, done like so
echo "" | ffmpeg -i input.mkv -loglevel panic output.mp4 2> /dev/null &
Edit:
This same effect can be done by adding -nostdin as a flag to ffmpeg, as demonstrated below
ffmpeg -i input.mkv -nostdin -loglevel panic output.mp4 2> /dev/null &

Related

Having issues with FFMPEG

Im using the following code to timelapse a set of files in the format of yyyy-mm-dd-hh-mm
It is using fswebcam to make the image and does so every minute with a crontab, that works fine, however the ffmpeg isn't working, im using the following command for it
cat $(ls | sort -V) | ffmpeg -framerate 10 -i - -vcodec libx264 -pix_fmt yuv420p -r 20 outputvideo.avi
When running the code I get a
pipe:: Invalid data found when processing input
I got this from https://youtu.be/_uVaZalaSbI to make the image creator and https://youtu.be/miYSR8yjbAM to make the video processor.
Any help would be nice, thanks.
Thanks for mivk for helping on this, and in my case the command
for f in *.jpg; do cat "$f"; done | ffmpeg -r 10 -f image2pipe -i - -vcodec libx264 -pix_fmt yuv420p outputvideo.avi
worked perfectly for me and outputted a timelapse as planned.

Understanding a script which uses ffmpeg to send rtmp input to node.js script

I was trying to understand this shell script which uses ffmpeg to take an rtmp input stream and send it to a node.js script. But I am having trouble understanding the syntax. What is going on here?
The script:
while :
do
echo "Loop start"
feed_time=$(ffprobe -v error -show_entries format=start_time -of default=noprint_wrappers=1:nokey=1 $RTMP_INPUT)
printf "feed_time value: ${feed_time}"
if [ ! -z "${feed_time}" ]
then
ffmpeg -i $RTMP_INPUT -tune zerolatency -muxdelay 0 -af "afftdn=nf=-20, highpass=f=200, lowpass=f=3000" -vn -sn -dn -f wav -ar 16000 -ac 1 - 2>/dev/null | node src/transcribe.js $feed_time
else
echo "FFprobe returned null as a feed time."
fi
echo "Loop finish"
sleep 3
done
What is feed_time here? What does it represent?
What is this portion doing - 2>/dev/null | node src/transcribe.js $feed_time?
What is the use of sleep 3? Does this mean that we are sending audio stream to node.js in chuncks of 3 seconds?
feed_time variable represents standard output of ffprobe command. This value needs to be passed to node script.
- character doesn't have special meaning in bash, i.e. it is interpreted by ffmpeg command itself (see here). According to ffmpeg docs:
A - character before the stream identifier creates a "negative"
mapping. It disables matching streams from already created mappings.
2>/dev/null is a redirection that sends standard error output of ffmpeg command to /dev/null device, thus effectively discarding the error output (see here). It is done because you want only the standard output (not error output) to be passed to node script.
| is a pipe. It sends standard output of ffmpeg command to standard input of node script.
sleep just delays execution of a script.

Get PID of process in one line

I have a bash script that is called by a phone system that gets some audio from a URL and (using ffmpeg, mplayer etc.) then pipes it back to the application. The file can have several URL's that are called so if the first one say goes off line or gives a 404 it will go to the next line.
I have an issue where some times the server will produce content however there is no audio. In such a case I want to kill the current PID of ffmpeg, mplayer etc. so that the script should move on.
I can't foreground it and get the last PID since once it's ran in the foreground the media is no longer being piped to the application calling it. I can't use exec in the beginning since if I then issue a kill to the PID the script dies which I don't want.
The script looks something like this:
#!/bin/bash
/usr/bin/ffmpeg -i 'http://1.1.1.1/soft_music' -vn -ar 8000 -ac 1 -f s16le -
/usr/bin/ffmpeg -i 'http://2.2.2.2/soft_music' -vn -ar 8000 -ac 1 -f s16le -
I assume I need to add something that will allow me to log the pid of the current ffmpeg command running so my external script can get it and kill it. Once that's done it will go to the next line and try the next stream from 2.2.2.2
It seems the script below would do what I am looking for:
#!/bin/bash
/usr/bin/ffmpeg -i 'http://1.1.1.1/soft_music' -vn -ar 8000 -ac 1 -f s16le - & echo $! > /tmp/my_pid
%1
/usr/bin/ffmpeg -i 'http://2.2.2.2/soft_music' -vn -ar 8000 -ac 1 -f s16le - & echo $! > /tmp/my_pid
%1
What this does is put the ffmpeg process into the background just for one moment to store the PID in a file. As soon as it goes to the next line we recover it with the job ID which will be 1.

FFMPEG Combining images to video and streaming in one command line

Currently, using ffmpeg, I am using two commands on my Terminal to:
1) create a video from a bunch of images:
ffmpeg -r 60 -f image2 -s 1920x1080 -i rotated-pano_frame%05d_color_corrected_gradblend.jpg -vcodec libx264 -crf 25 -pix_fmt yuv420p test.mp4
2) stream the video to a udp address:
ffmpeg -re -i test.mp4 -c copy -f flv udp://127.0.0.1:48550
How can I combine these two commands, into one single ffmpeg command?
A concern I have is that it takes a couple of minutes to generate the video from the images. Therefore, these commands have to happen serially, whereby the second command waits a few minutes for the first command to be completed, before it ensues.
Just add && between the two commands. This will execute the second command if the first executes successfully.
ffmpeg -r 60 -f image2 -s 1920x1080 -i rotated-pano_frame%05d_color_corrected_gradblend.jpg -vcodec libx264 -crf 25 -pix_fmt yuv420p test.mp4 && ffmpeg -re -i test.mp4 -c copy -f flv udp://127.0.0.1:48550

Have 2 blocking scripts interact with each other in linux

I have 2 blocking shell scripts which I want to have interact with each other. The scripts in question are peerflix (nodejs script) and ffmpeg (a simple bash script).
What happens: Peerflix fires up, feeds data to ffmpeg bash scrip which terminates peerflix on completion.
So once peerflix starts it outputs 2 lines and blocks immediately:
[08:15 PM]-[vagrant#packer-virtualbox-iso]-[/var/www/test]-[git master]
$ node /var/www/test/node/node_modules/peerflix/app.js /var/www/test/flexget/torrents/test.torrent -r -q
listening: http://10.0.2.15:38339/
process: 9601
I have to feed the listening address to the ffmpeg bash script:
#!/bin/sh
ffmpeg -ss 00:05:00 -i {THE_LISTENING_PORT} -frames:v 1 out1.jpg
ffmpeg -ss 00:10:00 -i {THE_LISTENING_PORT} -frames:v 1 out2.jpg
After the bash script is done I have to kill the peerflix script (hence me outputting the PID).
My question is how do I achieve this?
I think you want something like this:
# Start the node process in the background
node /var/www/test/node/node_modules/peerflix/app.js /var/www/test/flexget/torrents/test.torrent -r -q &
# Get the PID of the node process
PID=$!
# Run your stuff
ffmpeg -ss 00:05:00 -i {THE_LISTENING_PORT} -frames:v 1 out1.jpg
ffmpeg -ss 00:10:00 -i {THE_LISTENING_PORT} -frames:v 1 out2.jpg
# Stop the node process
kill $PID
You could put this in one script or have a node script that starts/kills the node process be a wrapper and run another script in the middle. Hope this helps.

Resources