user1891836 user1891836 - 4 years ago 269
Bash Question

Bash arguments HELL while looping a complex ffmpeg command. How do I escape it?

I am trying to create a waveform png for a bunch of videos using ffmpeg -filter_complex

The single line command works fine & produces the expected waveform graphic for a single video file:

ffmpeg -i INPUT.mp4 -filter_complex:a "showwavespic=s=1080x120" -frames:v 1 OUTPUT.png


When I try to automate the process for multiple files

for f in *.mp4
do
ffmpeg
-i "$f"
-filter_complex:a "showwavespic=s=1080x120"
-frames:v 1
'"${f%.mp4}.png"'
done


ffmpeg returns an error:


Unable to find a suitable output format for '"INPUT.png"'

'"INPUT.png"': invalid argument


I also tried the stripped down command:

for f in *.mp4
do
ffmpeg -i "$f" -filter_complex 'showwavespic' -frames:v 1 “${f%.mp4}.png”
done


but got the same argument error.

I'm guessing the problem is because of bash syntax, and I am not escaping the quoted arguments properly. I tried multiple versions of the command, but to no success. How do I fix this for loop?

Answer Source

Remove the single quotes (') around "${f%.mp4}.png".

Your second command seems to use typographer's quotes () instead of plain double quotes ("), which is what you want.

Double quotes (") tells Bash that what's within the quotes should be treated as one parameter to the called command, even if it has a space within it. The quotes will not be passed to the called command. Bash does not consider typographer's quotes to be special so it will pass them to the command, and your script won't work because they aren't actually part of the filename.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download