Interesting ffmpeg commands and scripts

January 30, 2020 written by @phlhar

I had serveral occasions, where I wanted something weird done in ffmpeg. The following ffmpeg commands and scripts are things I tried out, experimented with and also used. I might add onto the list in the future, as I stumble upon other interesting tasks done with ffmpeg.

The first script is to use ffmpeg as an audio dsp with multiband equalizer in MacOS. You could add other audio filters if you want, though that might increase the latency which is already quite high. For me the input is the built in microphone, though you can adjust that accordingly. I wrote the script to make it easier to configure the equalizer.

#:/bin/sh

# range is from 0 to 20
# 1b: Set 65Hz band gain.
f1b=10
# 2b: Set 92Hz band gain.
f2b=10
# 3b: Set 131Hz band gain.
f3b=10
# 4b: Set 185Hz band gain.
f4b=10
# 5b: Set 262Hz band gain.
f5b=10
# 6b: Set 370Hz band gain.
f6b=10
# 7b: Set 523Hz band gain.
f7b=10
# 8b: Set 740Hz band gain.
f8b=10
# 9b: Set 1047Hz band gain.
f9b=10
# 10b: Set 1480Hz band gain.
f10b=10
# 11b: Set 2093Hz band gain.
f11b=10
# 12b: Set 2960Hz band gain.
f12b=10
# 13b: Set 4186Hz band gain.
f13b=10
# 14b: Set 5920Hz band gain.
f14b=10
# 15b: Set 8372Hz band gain.
f15b=10
# 16b: Set 11840Hz band gain.
f16b=10
# 17b: Set 16744Hz band gain.
f17b=10
# 18b: Set 20000Hz band gain.
f18b=10

ffmpeg -f avfoundation -i ":0" -filter_complex superequalizer=1b=$f1b:2b=$f2b:3b=$f3b:4b=$f4b:5b=$f5b:6b=$f6b:7b=$f7b:8b=$f7b:9b=$f9b:10b=$f10b:11b=$f11b:12b=$f12b:13b=$f13b:14b=$f14b:15b=$f15b:16b=$f16b:17b=$f17b:18b=$f18b -f f32le - | ffplay -f f32le -ar 44.1k -ac 2 -

The next command merges several .mp3 files into one file.

cat *.mp3 | ffmpeg -i pipe:0 -acodec libmp3lame -map_metadata -1 "output.mp3"

It is possible to just do cat *.mp3 > output.mp3. This should work in most players, but the problem is that between the encoded audio data are the headers of the mp3 files. By reencoding the audio with ffmpeg, the length of the audio and the metadata are stored at the beginning of the mp3 file as it should be. I use -map_metadata -1 to remove any metadata of the mp3 files. Otherwise ffmpeg would copy the ID3 tags and other metadata of the first file to output.mp3.

I got one more command. This one is to make the video of a usb webcam or built in camera available on the network. It takes /dev/video0 as an input, and opens a tcp server on the specified IP address and port. You can access it by ffplay tcp://ip:port for example, but you can also use it as a OBS source or access it with vlc. By using -c:v copy the video is not reencoded, and thus this command has very little cpu consumption and there is less delay. This command is linux only, as it uses the video4linux2 input device.

ffmpeg -f v4l2 -framerate 30 -video_size 1920x1080 -vcodec h264 -i /dev/video0 -c:v copy -f h264 tcp://ip:port?listen