Skip to content

Ubuntu FFMPEG dynamic image file name

I currently have a directory full of a variety of mp4 files, and I am currently able to loop through the whole directory and screen caps at every five second interval. Currently, the command names them starting at “00000001.png” and increments for the next screen cap, but I would like it to use the file name along with the numbers being appended. I attempted to re-use the “$file” variable, but I get an odd error from running the loop.


for file in /datadrive/video_repo/ffmpeg_tmp/*.mp4; do /usr/bin/ffmpeg -i "$file" -r 0.5 /datadrive/image_repo/"${file%.}".%8d.png; done

But I get the following error at runtime:

Could not open file : /datadrive/image_repo//datadrive/video_repo/ffmpeg_tmp/testvideo00000001.png
av_interleaved_write_frame(): Input/output error

It looks like the output location is getting confused with the input for some reason. However, if I remove "${file%.}", it runs fine. (The reason I use “${file%.}”, is because it uses the files name, and omits the extension).

Any help on this would be very appreciative.



Here: /datadrive/image_repo//datadrive/video_repo/ffmpeg_tmp/testvideo00000001.png

You’re using the path TWICE, so he finds the path and add again here: /datadrive/image_repo/”${file%.}”.%8d.png but your ${file%.} is already /datadrive/image_repo/

for file in /datadrive/video_repo/ffmpeg_tmp/*.mp4; 
    do /usr/bin/ffmpeg -i "$file" -r 0.5 "${file%.}".%8d.png; 
User contributions licensed under: CC BY-SA
8 People found this is helpful