1

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?
halfer
  • 19,824
  • 17
  • 99
  • 186
Arpit Shukla
  • 9,612
  • 1
  • 14
  • 40

1 Answers1

2
  • 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.
user14967413
  • 1,264
  • 1
  • 6
  • 12
  • Thanks a lot. Also, what is the use of an infinite loop here? Is it to wait until ffprob gives a non-null output? – Arpit Shukla Jun 04 '22 at 10:49
  • @ArpitShukla The loop continues even if `ffrop` gives non-null output. It is just repeating the work infinitely. – user14967413 Jun 04 '22 at 11:44
  • So, doesn't ffmpeg block until the stream has ended? Or does it transmit only a small chunk of stream data in single iteration and so to keep on transmitting data to node.js we need need an infinite loop? – Arpit Shukla Jun 04 '22 at 11:51
  • @ArpitShukla `ffmpeg` reads file from `$RTMP_INPUT` (file or URL) and blocks until `ffmpeg` and `node` script process it, then waits 3 seconds. Apparently after that time data stored under `$RTMP_INPUT` may change, so the process starts again. – user14967413 Jun 04 '22 at 13:58
  • Thanks a lot for the entire explanation. – Arpit Shukla Jun 04 '22 at 14:01