3

I'm trying to cut a video into 2-minute clips using FFMpeg. I am using Ubuntu 10.10.

Here is my code:

#!/bin/sh
COUNTER=0
BEG=0
MIN=`ffmpeg -i ${1} 2>&1 | grep "Duration" | cut -d ' ' -f 4 | sed s/,// | cut -d ":" -f 2`
echo $MIN
((MIN=MIN-2))
before_last_dot=${1%.*};
while [ $COUNTER -lt $MIN ]; do
    ((BEG=COUNTER*60))
    echo "MIN:${MIN}"
    echo "ffmpeg -sameq -i ${1} -ss ${BEG} -t 120 ${before_last_dot}.${COUNTER}.wmv"
    ((COUNTER=COUNTER+2))
done

echo "ffmpeg -sameq -i ${1} -ss ${BEG} -t 120 ${before_last_dot}.${COUNTER}.wmv" should be ffmpeg -sameq -i ${1} -ss ${BEG} -t 120 ${before_last_dot}.${COUNTER}.wmv. I print it to check it. ${1} is the video name.

But the problem is, ((COUNTER=COUNTER+2)) or ((COUNTER+=2))never works! COUNTER is always 0, BEG is always 0 too. ((MIN=MIN-2)) never works too.

I tried to replace ((MIN=MIN-2)) with let "MIN-=2" I get an error: let: not found

I+ve double checked but still don't know why. I'm getting gray hair on this.

bbaja42
  • 2,099
  • 18
  • 34
DocWiki
  • 3,488
  • 10
  • 39
  • 49
  • 1
    Have you tried running the script with debugging flag -x? `#!/bin/sh -x` It will echo result of the each command – bbaja42 Jul 09 '11 at 01:01
  • Perhaps you're not even running bash? Try changing the first line to `#!/bin/bash` – Martin Jul 09 '11 at 01:02
  • @Martin: You have a sharp eye! `#!/bin/bash` works! Problem solved. Thanks! – DocWiki Jul 09 '11 at 01:08
  • I still dont know the difference between `/bin/sh` and `/bin/bash`. – DocWiki Jul 09 '11 at 01:09
  • @DocWiki: There are many shells, bash, the Bourne Again Shell, is just one of them. `/bin/sh` is the default, usually the most basic on an installation, shell scripts that are simple default to it, as it is practically always there - you can not assume it is a given shell though. – Orbling Jul 09 '11 at 01:12
  • The are many different shells in the linux world. `bin/sh` is a link to the default implementation, which is dash shell on the ubuntu (for few last editions). – bbaja42 Jul 09 '11 at 01:14

1 Answers1

4

The ((MIN=MIN-2)) syntax that you're using is a bash-specific feature.

I don't have Ubuntu 10.10 to hand to test with, but I'd guess that your /bin/sh is not bash, but a smaller and simpler shell with only the basic features required by POSIX. (In which case, ((MIN=MIN-2)) probably launches a sub-shell, which launches a sub-shell, which does nothing but set a variable MIN to the string MIN-2 and then exit.)

Try #!/bin/bash on the first line instead.

Matthew Slattery
  • 45,290
  • 8
  • 103
  • 119