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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0