I'm attempting to write a script for use with HandbrakeCli to convert .mkv-files to XBOX 360 compatible mp4-files.

#!/bin/bash
for f in "$@"
do
    echo "$f"
    HandbrakeCli -i "$f" -o ~/Movies/Handbrake/"${f%.*}.mp4" --preset "Normal" --mixdown "stereo" --subtitle "1" --width 720 --keep-display-aspect
done

The problem is that the destination filename (according to output from HandbrakeCli) includes the full path to the input file. Naturally I'm only interested in the filename without extension since I add .mp4.

I'm not used to shell scripting so help is very much appreciated!

link|improve this question
1  
Also see this discussion of this question on stackoverflow: stackoverflow.com/questions/965053/… – guenter Oct 3 '11 at 11:11
feedback

2 Answers

up vote 3 down vote accepted

First remove the path:

outf=${f##*/}

Then change the extension:

outf=${outf%.*}.mp4

(Not necessarily in that order, of course.)

link|improve this answer
feedback

Since you know the extension of your input file, you can say

new=~/Movies/Handbrake/"$(basename "$f" .mkv)".mp4
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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