1

I have the following bash script generating a jpg file with the same name of the video file. Now I need to change the bash script to generate the jpg file with the name image.jpg instead of with the same name of the video.

I tried lots of different ways to do that but I'm not really good in bash scripting and I have some problem with the basename and subdirectories.

May you please help me?

#!/bin/bash
for i in */*.mp4; do 
j=`basename $i .mp4`; 
/usr/bin/ffmpeg -i "$i" -r 1 -f image2 -vframes 1 -s 640x360 -ss 0:0:30 $j.jpg;
done

Following the directory tree

/
/a/video1.mp4
/b/video2.mp4
/c/video3.mp4

Following the result I need

/
/a/video1.mp4
/a/image.jpg
/b/video2.mp4
/b/image.jp4
/c/video3.mp4
/c/image.jpg

Thank you very much!

3 Answers 3

1

There's a problem with for i in */*.mp4: you can't loop over files in subdirectories like this (try it: for i in */*.mp4; do echo $i; done , this only prints */*.mp4).

For a case like this, there's no way around find.

One liner solution with find:

find /your/base/directory -type f -iname "*.mp4" -execdir /usr/bin/ffmpeg -i "{}" -r 1 -f image2 -vframes 1 -s 640x360 -ss 0:0:30 image.jpg \;
0
0

Just set j to the desired name

j=image

instead of setting it to the video's basename.

1
  • not working. It doesn't create the image in the subdirectory but in the / directory
    – Tom
    Feb 1, 2013 at 21:33
0

Change code of your bash ;)

#!/bin/bash
for i in */*.mp4; do 
DIR=$(dirname ${i})
/usr/bin/ffmpeg -i "$i" -r 1 -f image2 -vframes 1 -s 640x360 -ss 0:0:30 $DIR/image.jpg;
done

need explanation read: https://stackoverflow.com/a/6121114

1
  • dirname and basename are the tools you're looking for extracting path components: Feb 8, 2013 at 5:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .