Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When you want to extract two minutes of a video file from minute 2 to minute 4, you will use command

ffmpeg -i input.mpg -sameq -ss 00:02:00 -t 00:02:00 output.mpg

I'd like to split the video into two minutes chunks. I have an idea to use a bash script, but there are a few errors.

for i in seq 50;
do ffmpeg -i input.mpg -sameq -ss 00:`expr $i \* 2 - 2`:00 -t 00:02:00 output.mpg; done

Do you know how to correct the errors in script that are where I use $i expression? Or is there some better solution?

share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted

You forgot to use backticks - or better: $( ) subshell - in the seq invocation. This works:

for i in $( seq 50 );
do ffmpeg -i input.mpg -sameq -ss 00:`expr $i \* 2 - 2`:00 -t 00:02:00 output.mpg; done

Another thing is that you probably don't want output.mpg to be overwritten in each run, do you? :) Use $i in the output filename as well.

Apart from that: In bash, you can just use $(( )) or $[ ] instead of expr - it also looks more clear (in my opinion). Also, there is no need for seq - brace expansion is all you need to get a sequence. Here's an example:

for i in {1..50}
 do ffmpeg -i input.mpg -sameq -ss 00:$[ i* 2 - 2 ]:00 -t 00:02:00 output_$i.mpg
done

Another good thing about braces is that you can have leading zeros in the names (very useful for file sorting in the future):

for i in {01..50}
 do ffmpeg -i input.mpg -sameq -ss 00:$[ i* 2 - 2 ]:00 -t 00:02:00 output_$i.mpg
done

Notice as well, that i*2 - 2 can be easily simplified to i*2 if you just change the range:

for i in {00..49}
 do ffmpeg -i input.mpg -sameq -ss 00:$[ i*2 ]:00 -t 00:02:00 output_$i.mpg
done
share|improve this answer
    
Thank you, it's great to learn these features when I need them. –  xralf Oct 14 '11 at 8:45
add comment

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.