[Script] MKV to MP4 - All audio / Subtitles

Discussion of the HandBrake command line interface (CLI)
Forum rules
An Activity Log is required for support requests. Please read How-to get an activity log? for details on how and why this should be provided.
Post Reply
bisol
Posts: 8
Joined: Sun Mar 03, 2013 5:45 pm

[Script] MKV to MP4 - All audio / Subtitles

Post by bisol »

Hello All,

I am a newbie with ripping / Hanbrake and now. I have a huge collection of Dvds (more 500) and I want to rip them all.

The goal is this :
- An input string to set if it is a movie or an episode (tv - anime) : OK
- Audio & subtitles : with french / english / japanese (lot of animes) : Only if the choice is episode : OK
- if audio 2.0 only : encode with coreaudio aac 2.0 : OK
- if audio 5.1 is available : encore without mixdown (I want true 5.1) : OK

Last modification :
- Added 2 passes with turbo mode
- Choix of movie or anime (high quality or normal)
- Added check foreign subtitles

Code

Code: Select all

#!/bin/bash

#
# Liste des presets : https://trac.handbrake.fr/wiki/BuiltInPresets
#

type="$1"
dossiersource="$2"
#IFS=$'\n'

config_video_defaut=" -t 1 -e x264 -q 19.25 -B 160,160 -f mp4 -R Auto,Auto -D 0.0,0.0 -m -2 --turbo --subtitle scan,1-9 --subtitle-burn --subtitle-forced scan --native-language fre"

if [ $type == "film" ]; then
	config_video_x264=" --detelecine --decomb --loose-anamorphic -x b-adapt=2:rc-lookahead=50"
elif [ $type == "anime" ]; then
    config_video_x264=" --strict-anamorphic -x ref=1:weightp=1:subq=2:rc-lookahead=10:trellis=0:8x8dct=0"
fi

#for i in `find $2 -type f -name "*.mkv"`  
find $2 -type f -name "*.mkv" | while read fichiers; do
	
		#Nombre de piste audio + type (vérification seulement si 5.1)
		tracks=`echo '' | /Applications/HandBrakeCLI -t 0 -i "$fichiers" 2>&1|grep "Audio:"|wc -l`
		if [ $tracks -eq "1" ]; then
			audio="1"
		elif [ $tracks -eq "2" ]; then
			audio="1,2"
		elif [ $tracks -eq "3" ]; then
			audio="1,2,3"
		fi

		#Si pas de 5.1, converti sinon copie
		audio_type=`echo '' | /Applications/HandBrakeCLI -t 0 -i "$fichiers" 2>&1|grep "5.1 ch"|wc -l`
		if [ $audio_type -eq "0" ]; then
			aencoder="ca_aac"
			audio_type_ch=`echo '' | /Applications/HandBrakeCLI -t 0 -i "$fichiers" 2>&1|grep "1.0 ch"|wc -l`
			if [ $audio_type_ch -eq "0" ]; then	
				mixdown="stereo"
			else
				mixdown="mono"
			fi
		else
			aencoder="copy"
			mixdown="auto"
		fi
	     
		#Lancement de handbrake
		echo '' | /Applications/HandBrakeCLI $config_video_defaut $config_video_x264 --audio $audio --aencoder $aencoder --mixdown $mixdown -i "$fichiers" -o "${fichiers%.*}.m4v"
		
		
done
Thanks by advance for your reply.
Last edited by bisol on Fri Mar 08, 2013 10:29 pm, edited 9 times in total.
mduell
Veteran User
Posts: 8207
Joined: Sat Apr 21, 2007 8:54 pm

Re: [Script] MKV to MP4 - All audio / Subtitles

Post by mduell »

You can parse the output of a scan to see what audio / subtitle tracks exist, then run the encode with the appropriate numbers.

The syntax for multiple tracks is 1,2,3...
bisol
Posts: 8
Joined: Sun Mar 03, 2013 5:45 pm

Re: [Script] MKV to MP4 - All audio / Subtitles

Post by bisol »

I have modified the script. I didn't choose the cleanest way to detect 5.1 but this is working for 5-10 movies so, I hope this is good.

Warning : The parameters are from the OSX version of Hanbrake (x264 advanced options & more). So, there is a difference with the normal preset found here (https://trac.handbrake.fr/wiki/BuiltInPresets) and the normal preset in the OSX version.

Edit : Added the AUDIO_TYPE_CH for the 1.0 channel
bisol
Posts: 8
Joined: Sun Mar 03, 2013 5:45 pm

Re: [Script] MKV to MP4 - All audio / Subtitles

Post by bisol »

Script is now finished. I've added scan for foreing languages, params in variable, ...
AlBundy
Bright Spark User
Posts: 377
Joined: Mon Dec 31, 2012 4:47 am

Re: [Script] MKV to MP4 - All audio / Subtitles

Post by AlBundy »

You can also have a look at my script: viewtopic.php?f=10&t=26163 ;-)

Al
bisol
Posts: 8
Joined: Sun Mar 03, 2013 5:45 pm

Re: [Script] MKV to MP4 - All audio / Subtitles

Post by bisol »

I have ! Your script is very good but if I've understand, there is a difference.

If the audio is not 5.1, I wan't to mixdown it (stereo / audio) to have small files.

But I repeat, you did a very very good job with your script. Mine is more adapted for my situation.
AlBundy
Bright Spark User
Posts: 377
Joined: Mon Dec 31, 2012 4:47 am

Re: [Script] MKV to MP4 - All audio / Subtitles

Post by AlBundy »

THe default setting is to copy the audio-stream, you can set --audio-mixdown to mixdown all tracks to Dolby Pro Logic II.
And of course you can define your own decoder settings.

Al
bisol
Posts: 8
Joined: Sun Mar 03, 2013 5:45 pm

Re: [Script] MKV to MP4 - All audio / Subtitles

Post by bisol »

OK. Mixdown is not correct because I want Passtru if it's 5.1.
So, does it possible to do ?
- if 5.1 : passthru
- if not : mixdown mono / stereo (detect if 1.0 or 2.0 or 2.1...)

I know, thi sis not logical but I do not have so much space on my hdd to copy all audio...
AlBundy
Bright Spark User
Posts: 377
Joined: Mon Dec 31, 2012 4:47 am

Re: [Script] MKV to MP4 - All audio / Subtitles

Post by AlBundy »

the convert-settings in my script are global.

Are you sure, that you'll save much space with your settings?

Al
bisol
Posts: 8
Joined: Sun Mar 03, 2013 5:45 pm

Re: [Script] MKV to MP4 - All audio / Subtitles

Post by bisol »

I have more than 1000 mkv so, if I can save 20mb for some files, I will be happy lol.

I've tried with a 45 min esisode with 2 languages audio and subtitles.

Copy audio : 480-490mo
Mixdown mono : 450mo
AlBundy
Bright Spark User
Posts: 377
Joined: Mon Dec 31, 2012 4:47 am

Re: [Script] MKV to MP4 - All audio / Subtitles

Post by AlBundy »

Ok, now I understand.
It seems that using your own script ist the best option for you. ;-)

Al
AlBundy
Bright Spark User
Posts: 377
Joined: Mon Dec 31, 2012 4:47 am

Re: [Script] MKV to MP4 - All audio / Subtitles

Post by AlBundy »

I've added an argument to my script to define own mixdown-mappings
here's an example:
hb.rb --input ~/test.mkv --output ~/test.m4v --audio-mixdown --audio-mixdown-mappings "5.1:copy,1.0:mono,2.0:stereo,Dolby Surround:copy"
bisol
Posts: 8
Joined: Sun Mar 03, 2013 5:45 pm

Re: [Script] MKV to MP4 - All audio / Subtitles

Post by bisol »

Great news !

I make some tests...
AlBundy
Bright Spark User
Posts: 377
Joined: Mon Dec 31, 2012 4:47 am

Re: [Script] MKV to MP4 - All audio / Subtitles

Post by AlBundy »

I've uploaded a new archive.
The new syntax is
hb.rb --input /dev/rdisk1 --output "~/Desktop/Output_#pos#.m4v" --audio-mixdown "5.1:copy,1.0:mono,2.0:stereo,Dolby Surround:copy"

5.1, Dolby Surround etc. are regular expressions - if the description of an audio-track matches the regex the track will be mixed down.

The correct and maybe more common syntax would be something like this: --audio-mixdown ".*5[.]1.*:copy,.*1[.]0.*:mono,.*2[.]0.*:stereo"
but the example is more readabe and works well....
bisol
Posts: 8
Joined: Sun Mar 03, 2013 5:45 pm

Re: [Script] MKV to MP4 - All audio / Subtitles

Post by bisol »

You did a very good job. I use your script now.

Thanks!
Post Reply