handbrakecli/ffmpeg command to encode and auto filename(Ubuntu)

534 views Asked by At

I have 2 preset .json files(from the GUI version on windows) to convert mkv to mp4.

  1. converts to h264 and adds subtitle 1
  2. converts to h264

I'm only trying to get no.2 to work at this stage.

for i in `*.mkv`; do HandBrakeCLI --preset-import-file HPRESET.json -Z "MYPRESET" --output *.mp4; done no output name

HandBrakeCLI -i $1 --preset-import-gui HPRESET.json -Z "MYPRESET" --output $1.mp4

errors on output name

for i in `*.mkv`; do HandBrakeCLI --title $i --preset "Very Fast 1080p30" --output *.mp4; done

errors on output name AND not valid preset.

$ for i in `seq 4`; do HandBrakeCLI --input /dev/dvd --title $i --preset Normal --output NameOfDisc_Title$i.mp4; done copied this from another stackoverflow question, but outputs as 1.mp4 and then 2.mp4 etc.

1

There are 1 answers

3
mashuptwice On

You can extract the filename without extension with something like that:

noext=${i%.*}

Example:

╰─$ for i in *.mkv; do echo "$i"; noext=${i%.*}; echo "$noext"; done
asdf.mkv
asdf
test.mkv
test

Same loop, different notation:

for i in *.mkv
do
    #put the commands you want to run after "do" and before "done"
    echo "$i"
    noext=${i%.*}
    echo "$noext"
done

Note that the for command will search any file in the current directory ending with .mkv. For each file it has found, it will save the files name into the variable $i, then execute the commands after do, then save the next files name into the variable $i and execute the commands between do and done. It will repeat that cycle until every file which has been found is processed.

As I have no experience with handbrake presets, here a solution with ffmpeg:

for i in *.mkv
do
    #put the commands you want to run after "do" and before "done"
    noext=${i%.*}
    ffmpeg -i "$i" -c:v libx264 -c:a copy -c:s copy "$noext.mp4"
done