Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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!

share|improve this question

3 Answers 3

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 \;
share|improve this answer
    
it works, thank you very much! –  Tom Feb 1 '13 at 21:33

Just set j to the desired name

j=image

instead of setting it to the video's basename.

share|improve this answer
    
not working. It doesn't create the image in the subdirectory but in the / directory –  Tom Feb 1 '13 at 21:33

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: http://stackoverflow.com/a/6121114

share|improve this answer
    
dirname and basename are the tools you're looking for extracting path components: –  Gurjinder Singh Feb 8 '13 at 5:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.