7

So, I'm using a script I've made to convert videos to the webm format. A certain program calls the script, sending %f which is the full, absolute file name of the video, like this:

converter.sh %f

where %f has two possible extensions, .avi or .mpg.

# /bin/bash
ffmpeg -i $1 `dirname $1``basename $1 avi`webm && rm $1

It currently works perfectly when $1 contains a .avi file, because basename removes the .avi extension. When $1 ends with .mpg instead, the result is .mpgwebm.

How can I modify that script to be able to receive those two possible different formats?


Resuming: If $1 is /somedir/video.avi, the script should do:

ffmpeg -i /somedir/video.avi /somedir/video.webm

And if $1 is /somedir/video.mpg, the script should do:

ffmpeg -i /somedir/video.mpg /somedir/video.webm

I know this might be fool for some people, but I'm kind of new with the bash.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Tomas
  • 1,014
  • 2
  • 12
  • 18

1 Answers1

9

You can use substitution mechanisms provided by most shells:

B=$(basename "$1"); D=$(dirname "$1");
ffmpeg -i "$1" "$D/${B%.*}.webm" && rm "$1"

In fact, basename and dirname could also be emulated by substitutions.

Note: direct .* suffix removal is not correct on paths like this.dir/file.

Stéphane Gimenez
  • 28,527
  • 3
  • 76
  • 87