Home > Software design >  Batch process audio files and image files to create movie files by matching multiple wildcard values
Batch process audio files and image files to create movie files by matching multiple wildcard values

Time:09-29

I'm attempting to batch-process audio and image files to create video files using a Shell script with FFmpeg.

  1. The ffmpeg script works in Terminal:
ffmpeg -loop 1 -i image.png -i audio.wav -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -shortest output.mp4
  1. A nested for loop with wildcards works as a batch script:
#!/bin/sh
for img in *.png; do
    for wav in *.wav; do
        ffmpeg -loop 1 -i $img -i $wav -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -shortest ../mb/$img.mp4
    done
done

The only problem here is that it creates more combinations than I need. I'd like to be able to match wildcard values and only mix those together. I'd like to be able to prepare the filenames to match in advance to make this easier: For example, only match 1.png with 1.wav to make 1.mp4, 2.png with 2.wav to make 2.mp4 and so on. I'm able to modify the script to match the wildcards in a Regex, but I'm not sure if there is a way to then execute the logic above. Here is what I am attempting:

#!/bin/sh
img=*.png
wav=*.wav

if [[ ${img%.*} == ${wav%.*} ]];
    then
        ffmpeg -loop 1 -i $img -i $wav -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -shortest $img.mp4;
    else
        echo "Failure"
    fi

This begins by overwriting the image files, so it does not appear to work as planned. Is there a simpler way to do this? Perhaps looping through images {1..5} in one folder and audio files {1..5} in another?

Thanks for any insights, and happy to provide more context (very new to this, so still learning).

CodePudding user response:

You can loop through *.png and derive *.wav from them like this:

for img in *.png; do
  ffmpeg -loop 1 -i "$img" -i "${img%.*}.wav" -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -shortest "${img%.*}.mp4"
done
  • Related