Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Rmafive

macrumors 6502
Original poster
Jul 25, 2008
296
1
Richmond, Virginia
Hey guys,

I was wondering if it is possible to automate handbrake's destination location based on the preset you choose. I don't want to set up HandbrakeCLI and other scripts just to automate the destination, although I would if I could rip my blu-rays with one click. I currently rip two versions of every blu ray movie, converted with MakeMK: one for iPads and iPods and one for my HTPC (they each go into a separate folder). If there is a way to automat this based on my presets that would be awesome!
 
Use a script to create symbolic links to the directory you want. Then let handbrake do it's thing to the same directory.

IE: setup handbrake to go to ~/Desktop/Handbrake/output

Then you can do a "ln -s ~/Desktop/Handbrake/output ~/Movies/BluRay" or something like that.
 
Use a script to create symbolic links to the directory you want. Then let handbrake do it's thing to the same directory.

IE: setup handbrake to go to ~/Desktop/Handbrake/output

Then you can do a "ln -s ~/Desktop/Handbrake/output ~/Movies/BluRay" or something like that.

But will that work if I want different presets to send the files they encode to different folders like HD and SD folders? I have tried to set up the CLI for hours and I keep getting an Mp4create error so I would prefer to continue using the GUI.
 
Last edited:
I keep getting an Mp4create error so I would prefer to continue using the GUI.
usually that error is the mp4 muxer not able to mux the mp4 as its larger than 4 GB and you don't have the cli muxing to a 64 bit mp4 (this is akin to the gui's "Large File Size" checkbox).

Would have to see the exact error though.
 
Here is the Log:


Last login: Wed Aug 31 13:26:34 on ttys000
Alexander-Server:~ Robert$ /Applications/HandBrakeCLI -i /users/Robert/Desktop/Red.mkv -o /users/Rmafive/desktop/Red -e x264 -x ref=2:bframes=2:subq=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0 -s 12000 -T -a 1,2 -E AC3,AAC -B 640,160 -6 6ch,dpl2 -R Auto,Auto -D 0.0,0.0 -f mp4 -4 --loose-anamorphic -m
[13:27:49] hb_init: checking cpu count
[13:27:49] hb_init: starting libhb thread
HandBrake 0.9.5 (2011010300) - Darwin x86_64 - http://handbrake.fr
12 CPUs detected
Opening /users/Robert/Desktop/Red.mkv...
[13:27:49] hb_scan: path=/users/Robert/Desktop/Red.mkv, title_index=1
libbluray/bdnav/index_parse.c:157: indx_parse(): error opening /users/Robert/Desktop/Red.mkv/BDMV/index.bdmv
libbluray/bluray.c:960: nav_get_title_list(/users/Robert/Desktop/Red.mkv) failed (0x104000000)
[13:27:49] bd: not a bd - trying as a stream/file instead
libdvdnav: Using dvdnav version 4.1.3
libdvdread: Using libdvdcss version 1.2.10 for DVD access
libdvdnav:DVDOpenFileUDF:UDFFindFile /VIDEO_TS/VIDEO_TS.IFO failed
libdvdnav:DVDOpenFileUDF:UDFFindFile /VIDEO_TS/VIDEO_TS.BUP failed
libdvdread: Can't open file VIDEO_TS.IFO.
libdvdnav: vm: failed to read VIDEO_TS.IFO
[13:27:49] dvd: not a dvd - trying as a stream/file instead
Input #0, matroska,webm, from '/users/Robert/Desktop/Red.mkv':
Duration: 01:51:09.66, start: 0.000000, bitrate: 640 kb/s
Chapter #0.0: start 0.000000, end 431.264156
Metadata:
title : Chapter 00
Chapter #0.1: start 431.264156, end 843.425911
Metadata:
title : Chapter 01
Chapter #0.2: start 843.425911, end 1253.460533
Metadata:
title : Chapter 02
Chapter #0.3: start 1253.460533, end 1446.570111
Metadata:
title : Chapter 03
Chapter #0.4: start 1446.570111, end 1750.457022
Metadata:
title : Chapter 04
Chapter #0.5: start 1750.457022, end 2156.571067
Metadata:
title : Chapter 05
Chapter #0.6: start 2156.571067, end 2606.687400
Metadata:
title : Chapter 06
Chapter #0.7: start 2606.687400, end 2952.532911
Metadata:
title : Chapter 07
Chapter #0.8: start 2952.532911, end 3356.936911
Metadata:
title : Chapter 08
Chapter #0.9: start 3356.936911, end 3792.121667
Metadata:
title : Chapter 09
Chapter #0.10: start 3792.121667, end 4182.011156
Metadata:
title : Chapter 10
Chapter #0.11: start 4182.011156, end 4524.269733
Metadata:
title : Chapter 11
Chapter #0.12: start 4524.269733, end 4852.180667
Metadata:
title : Chapter 12
Chapter #0.13: start 4852.180667, end 5234.103867
Metadata:
title : Chapter 13
Chapter #0.14: start 5234.103867, end 5584.787533
Metadata:
title : Chapter 14
Chapter #0.15: start 5584.787533, end 5911.906000
Metadata:
title : Chapter 15
Chapter #0.16: start 5911.906000, end 6196.648778
Metadata:
title : Chapter 16
Chapter #0.17: start 6196.648778, end 6669.663000
Metadata:
title : Chapter 17
Stream #0.0(eng): Video: h264, yuv420p, 1920x1080 [PAR 1:1 DAR 16:9], 23.98 fps, 24 tbr, 1k tbn, 47.95 tbc
Stream #0.1(eng): Audio: ac3, 48000 Hz, 5.1, s16, 640 kb/s
Metadata:
title : 3/2+1
[13:27:49] scan: decoding previews for title 1
[13:27:49] scan: audio 0x1: AC-3, rate=48000Hz, bitrate=640000 English (AC3) (5.1 ch)
Scanning title 1...
Scanning title 1...
[13:27:50] scan: 10 previews, 1920x1080, 23.976 fps, autocrop = 0/0/0/0, aspect 16:9, PAR 1:1
[13:27:50] scan: title (0) job->width:1920, job->height:1072
[13:27:50] libhb: scan thread found 1 valid title(s)
+ title 1:
+ stream: /users/Robert/Desktop/Red.mkv
+ duration: 01:51:09
+ size: 1920x1080, pixel aspect: 1/1, display aspect: 1.78, 23.976 fps
+ autocrop: 0/0/0/0
+ chapters:
+ 1: cells 0->0, 0 blocks, duration 00:07:11
+ 2: cells 0->0, 0 blocks, duration 00:06:52
+ 3: cells 0->0, 0 blocks, duration 00:06:50
+ 4: cells 0->0, 0 blocks, duration 00:03:13
+ 5: cells 0->0, 0 blocks, duration 00:05:03
+ 6: cells 0->0, 0 blocks, duration 00:06:46
+ 7: cells 0->0, 0 blocks, duration 00:07:30
+ 8: cells 0->0, 0 blocks, duration 00:05:45
+ 9: cells 0->0, 0 blocks, duration 00:06:44
+ 10: cells 0->0, 0 blocks, duration 00:07:15
+ 11: cells 0->0, 0 blocks, duration 00:06:29
+ 12: cells 0->0, 0 blocks, duration 00:05:42
+ 13: cells 0->0, 0 blocks, duration 00:05:27
+ 14: cells 0->0, 0 blocks, duration 00:06:21
+ 15: cells 0->0, 0 blocks, duration 00:05:50
+ 16: cells 0->0, 0 blocks, duration 00:05:27
+ 17: cells 0->0, 0 blocks, duration 00:04:44
+ 18: cells 0->0, 0 blocks, duration 00:07:53
+ audio tracks:
+ 1, English (AC3) (5.1 ch) (iso639-2: eng), 48000Hz, 640000bps
+ subtitle tracks:
ERROR: Invalid audio input track '2', exiting.
Alexander-Server:~ Robert$

I think I might have messed up the audio settings in the original code. I want the 6 channel audio as the first track and the dolby as the second track, but I think I messed up the order and the code.
 
Last edited:
Alexander-Server:~ Robert$ /Applications/HandBrakeCLI -i /users/Robert/Desktop/Red.mkv -o /desktop/Red -e x264 -x cabac=0:ref=2:me=umh:b-adapt=2:weightb=0:trellis=0:weightp=0 -q 20.0 -r 29.97 --pfr -a 1,1 -E faac,copy:ac3 -B 160,160 -6 dpl2,auto -R Auto,Auto -D 0.0,0.0 -f mp4 -4 -X 1280 --loose-anamorphic -m

....

MP4ERROR: MP4Open: failed: No such file or directory
ERROR: muxmp4.c: MP4Create failed!

You have both a "Desktop" directory and a"desktop" directory ? If not, you told the HBCli to write to a directory that doesn't exist ... also given its case sensitive are you sure its not "Users" instead of "users" ?

My guess is instead of:
Code:
-i /users/Robert/Desktop/Red.mkv -o /desktop/Red
You'd want:

Code:
-i /Users/Robert/Desktop/Red.mkv -o /Users/Robert/Desktop/Red<add to the name here with appropriate extension for the output file>
or something.

Paths matter and so does case sensitivity.

Edit: you can also read https://trac.handbrake.fr/wiki/CLIGuide which gives some rudimentary tips for how to use a cli regarding paths.

For instance since your reading from an writing to your home directory "/Users/Robert/ ...." you could use the tilde which tells it to use the home directory as per the user guide.

In this case

Code:
/Users/Robert/Desktop

would be shortened to :

Code:
~/Desktop


----------

I think I might have messed up the audio settings in the original code. I want the 6 channel audio as the first track and the dolby as the second track, but I think I messed up the order and the code.

out of curiosity ... why would you want that ?
 
So I can play the files in iTunes, which can't handle the 6 channel audio and Plex which can. But I want the 6 channel to be the default choice (thus the first track).

Edit: I fixed it myself. I will post with results once it finishes encoding! My next step is to create a service with automator so I don't have to use the terminal anymore.
 
That seems to work fine, but now I have another question. How do I set up a service with automator so I can select the .mkv file I want to encode and have it replace the path (below) with the information from the movie I have selected and then encode it. I want to replace the blue stuff with the information from the mkv file I select:

/Applications/HandBrakeCLI -i /Users/Robert/Desktop/Red.mkv -o /Users/Robert/desktop/Red.mp4 -e x264 -x ref=2:bframes=2:subq=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0 -s 12000 -2 -T -a 1,1 -E AC3,AAC -B 640,160 -6 6ch,dpl2 -R Auto,Auto -D 0.0,0.0 -f mp4 -4 --strict-anamorphic -m
 
That seems to work fine, but now I have another question. How do I set up a service with automator so I can select the .mkv file I want to encode and have it replace the path (below) with the information from the movie I have selected and then encode it. I want to replace the blue stuff with the information from the mkv file I select:

/Applications/HandBrakeCLI -i /Users/Robert/Desktop/Red.mkv -o /Users/Robert/desktop/Red.mp4 -e x264 -x ref=2:bframes=2:subq=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0 -s 12000 -2 -T -a 1,1 -E AC3,AAC -B 640,160 -6 6ch,dpl2 -R Auto,Auto -D 0.0,0.0 -f mp4 -4 --strict-anamorphic -m

This may be more than your looking for, but I wrote an Automator Action called Batch Encode that does what your looking for:

https://forums.macrumors.com/threads/805573/

You can enter your custom setting for HBCLI and an output directory, then save separate Batch Encode Service workflows and run them depending on your needs.
 
This may be more than your looking for, but I wrote an Automator Action called Batch Encode that does what your looking for:

https://forums.macrumors.com/threads/805573/

You can enter your custom setting for HBCLI and an output directory, then save separate Batch Encode Service workflows and run them depending on your needs.

I tried out downloading your automator actions and while they look helpful, they are too complex for what I am trying to do. All I need to do is create a service that changes the path in the handbrake code to whatever mkv I have selected and then encode it. I know there is a way to do this with automator because I saw a video with someone doing exactly that, but there was no description with it!
 
I tried out downloading your automator actions and while they look helpful, they are too complex for what I am trying to do. All I need to do is create a service that changes the path in the handbrake code to whatever mkv I have selected and then encode it. I know there is a way to do this with automator because I saw a video with someone doing exactly that, but there was no description with it!

Here's a quick rundown of what you'll need to do.

  1. Open Automator and Create a New Service.
  2. At the top the workflow, Set the pull-down buttons to read "Service receives selected files or folders in any application.
  3. Drag and Add the "Run Shell Script" Action from the Actions Library to your workflow.
  4. In the "Run Shell Script" Action Panel set the Pass Input pull-down button to "as arguments"
  5. Next, Copy and paste the code below into the Script area, replacing any existing text.

    Code:
    for f in "$@"
    do
    	# SET YOUR OUTPUT DIRECTORY
    	outputDir="$HOME/Desktop"
    	# SET YOUR OUTPUT FILE EXTENSION (MP4/MKV/M4V)
    	outputFileExt="mp4"
    
    	inputFileExt=`echo "$f" | sed 's|.*\.||'`
    	inputFileNameNoExt=`basename "$f" ".${inputFileExt}"`
    
    	/Applications/HandBrakeCLI -i "$f" -o "${outputDir}/${inputFileNameNoExt}.${outputFileExt}" -e x264 -x ref=2:bframes=2:subq=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0 -s 12000 -2 -T -a 1,1 -E AC3,AAC -B 640,160 -6 6ch,dpl2 -R Auto,Auto -D 0.0,0.0 -f mp4 -4 --strict-anamorphic -m
    done
  6. Change the "outputDir" variable to the directory you want your encoded file to be saved to.
  7. Save the Service.
 
Thanks Mac.Jedi that was super helpful. I have two more questions:

1: Is there any way to view the progress of the encoding (maybe in terminal)?

2: If I want to convert the same mkv file twice, one HD (using my current settings) and one SD (using the handbrake universal setting), would I have to create a separate service or is it possible to set up a queue (I read this can be done with semicolons) with two different destinations (maybe outputDir2)?

Thanks for all of your help! :D
 
Thanks Mac.Jedi that was super helpful. I have two more questions:

1: Is there any way to view the progress of the encoding (maybe in terminal)?

2: If I want to convert the same mkv file twice, one HD (using my current settings) and one SD (using the handbrake universal setting), would I have to create a separate service or is it possible to set up a queue (I read this can be done with semicolons) with two different destinations (maybe outputDir2)?

Thanks for all of your help! :D

No problemo, I'm happy to help. I've been doing this stuff for a long time.

For multiple encodes from the same source, you should be able to just add another HB command on another line below your first one. Like you stated, you'll have to add another outputDir variable and change the command to output to the new directory.

Getting the progress in Terminal is more difficult. I'll take a look at the code I use for Batch Encode and see if I can simplify it for you.
 
Here is the code I have set up with the 2nd output:


for f in "$@"
do
# SET YOUR OUTPUT DIRECTORY
outputDir="$Users/Robert/Desktop/Encoded Movies/HD"
outputDir2="$Users/Robert/Desktop/Encoded Movies/SD"
# SET YOUR OUTPUT FILE EXTENSION (MP4/MKV/M4V)
outputFileExt="mp4"

inputFileExt=`echo "$f" | sed 's|.*\.||'`
inputFileNameNoExt=`basename "$f" ".$mp4"`

/Applications/HandBrakeCLI -i "$f" -o "${outputDir}/${inputFileNameNoExt}.${outputFileExt}" -e x264 -x ref=2:bframes=2:subq=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0 -s 12000 -2 -T -a 1,1 -E AC3,AAC -B 640,160 -6 6ch,dpl2 -R Auto,Auto -D 0.0,0.0 -f mp4 -4 --strict-anamorphic -m

/Applications/HandBrakeCLI -i "$f" -o "${outputDir2}/${inputFileNameNoExt}.${outputFileExt}" -e x264 -q 20.0 -a 1,1 -E faac,copy:ac3 -B 160,160 -6 dpl2,auto -R Auto,Auto -D 0.0,0.0 -f mp4 -X 720 --loose-anamorphic -m -x cabac=0:ref=2:me=umh:bframes=0:weightp=0:8x8dct=0:trellis=0:subme=6

done


I am assuming it will work, but to test will take about 4-5 hours even on a Mac Pro!
 
I am assuming it will work, but to test will take about 4-5 hours even on a Mac Pro!

To get it to run in Terminal you'll need to create a separate script file:
  1. Copy and paste your script into a plain text editor.
    Code:
    for f in "$@"
    do
    # SET YOUR OUTPUT DIRECTORIES
    outputDir="$Users/Robert/Desktop/Encoded Movies/HD"
    outputDir2="$Users/Robert/Desktop/Encoded Movies/SD"
    # SET YOUR OUTPUT FILE EXTENSION (MP4/MKV/M4V)
    outputFileExt="mp4"
    
    inputFileExt=`echo "$f" | sed 's|.*\.||'`
    inputFileNameNoExt=`basename "$f" ".$mp4"`
    
    /Applications/HandBrakeCLI -i "$f" -o "${outputDir}/${inputFileNameNoExt}.${outputFileExt}" -e x264 -x ref=2:bframes=2:subq=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0 -s 12000 -2 -T -a 1,1 -E AC3,AAC -B 640,160 -6 6ch,dpl2 -R Auto,Auto -D 0.0,0.0 -f mp4 -4 --strict-anamorphic -m
    
    /Applications/HandBrakeCLI -i "$f" -o "${outputDir2}/${inputFileNameNoExt}.${outputFileExt}" -e x264 -q 20.0 -a 1,1 -E faac,copy:ac3 -B 160,160 -6 dpl2,auto -R Auto,Auto -D 0.0,0.0 -f mp4 -X 720 --loose-anamorphic -m -x cabac=0:ref=2:me=umh:bframes=0:weightp=0:8x8dct=0:trellis=0:subme=6
    
    done
  2. Save the text file with a ".sh" extension.
  3. To make your script executable, Open Terminal and Type:
    Code:
    chmod 777 "/path/to/script.sh"
  4. Press Return
  5. Open Automator and Create a New Service.
  6. At the top the workflow, Set the pull-down buttons to read "Service receives selected files or folders in any application.
  7. Drag and Add the "Run Shell Script" Action from the Actions Library to your workflow.
  8. In the "Run Shell Script" Action Panel set the Pass Input pull-down button to "as arguments"
  9. Next, Copy and paste the code below into the Script area, replacing any existing text.
    Code:
    # SET THE PATH TO YOUR SCRIPT
    scriptPath="$HOME/Desktop/encodeScript.sh"
    
    for thePath in "$@"
    do
    	quotedPath=$(echo \'"$thePath"\')
    	sourceList=$(echo "$sourceList$quotedPath ")
    done
    
    osascript -e "tell application \"Terminal\" to do script \"$scriptPath\" & space & \"$sourceList\""
    
    echo "$@"
  10. Change the "scriptPath" variable to the path to your encode script.
  11. Save the Service.

I hope this helps get you going :)
 
Thank you soo much for your help! I seem to keep getting this error though:

Alexander-Server:~ Robert$ $/Users/Robert/Desktop/script.sh '/Users/Robert/Desktop/Red.mkv'
-bash: $/Users/Robert/Desktop/script.sh: No such file or directory

I have checked the spelling and capitalization of everything. I literally have no idea why this is happening!
 
Thank you soo much for your help! I seem to keep getting this error though:

Alexander-Server:~ Robert$ $/Users/Robert/Desktop/script.sh '/Users/Robert/Desktop/Red.mkv'
-bash: $/Users/Robert/Desktop/script.sh: No such file or directory

I have checked the spelling and capitalization of everything. I literally have no idea why this is happening!

Check your "scriptPath" variable … it's probably not quoted properly. Here's what it should look like using the path and filename you used in your post.

Code:
scriptPath="$HOME/Desktop/script.sh"
 
Sorry to keep bothering you, but now I get this error:

Alexander-Server:~ Robert$ /Users/Robert/Desktop/script.sh '/Users/Robert/Desktop/Red.mkv'
/Users/Robert/Desktop/script.sh: line 1: {rtf1ansiansicpg1252cocoartf1138: command not found
/Users/Robert/Desktop/script.sh: line 2: syntax error near unexpected token `}'
/Users/Robert/Desktop/script.sh: line 2: `{\fonttbl\f0\fmodern\fcharset0 Courier;}'


I used this code in the automator service:

Code:
scriptPath="$HOME/Desktop/script.sh"

for thePath in "$@"
do
	quotedPath=$(echo \'"$thePath"\')
	sourceList=$(echo "$sourceList$quotedPath ")
done

osascript -e "tell application \"Terminal\" to do script \"$scriptPath\" & space & \"$sourceList\""

echo "$@"

and here is the script's code:

Code:
for f in "$@"
do
# SET YOUR OUTPUT DIRECTORIES
outputDir="$Users/Robert/Desktop/Encoded Movies/HD"
outputDir2="$Users/Robert/Desktop/Encoded Movies/SD"
# SET YOUR OUTPUT FILE EXTENSION (MP4/MKV/M4V)
outputFileExt="mp4"

inputFileExt=`echo "$f" | sed 's|.*\.||'`
inputFileNameNoExt=`basename "$f" ".$mp4"`

/Applications/HandBrakeCLI -i "$f" -o "${outputDir}/${inputFileNameNoExt}.${outputFileExt}" -e x264 -x ref=2:bframes=2:subq=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0 -s 12000 -2 -T -a 1,1 -E AC3,AAC -B 640,160 -6 6ch,dpl2 -R Auto,Auto -D 0.0,0.0 -f mp4 -4 --strict-anamorphic -m

/Applications/HandBrakeCLI -i "$f" -o "${outputDir2}/${inputFileNameNoExt}.${outputFileExt}" -e x264 -q 20.0 -a 1,1 -E faac,copy:ac3 -B 160,160 -6 dpl2,auto -R Auto,Auto -D 0.0,0.0 -f mp4 -X 720 --loose-anamorphic -m -x cabac=0:ref=2:me=umh:bframes=0:weightp=0:8x8dct=0:trellis=0:subme=6

done

Did I screw something up? I do not understand why the error message talks about the font!
 
Sorry to keep bothering you, but now I get this error:

Alexander-Server:~ Robert$ /Users/Robert/Desktop/script.sh '/Users/Robert/Desktop/Red.mkv'
/Users/Robert/Desktop/script.sh: line 1: {rtf1ansiansicpg1252cocoartf1138: command not found
/Users/Robert/Desktop/script.sh: line 2: syntax error near unexpected token `}'
/Users/Robert/Desktop/script.sh: line 2: `{\fonttbl\f0\fmodern\fcharset0 Courier;}'


Did I screw something up? I do not understand why the error message talks about the font!

You must have saved the script as a rich text file, not plain text. Open the file in TextEdit. In the Menu Bar, select Format > Make Plain Text (Command+Shift+T). Then Save your file.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.