Page 1 of 2
HandBrakeCLI Script

Posted:
Thu May 16, 2013 3:53 pm
by FreddieShoreditch
Hi all,
This is my first post on this forum and I'm not long a user of the software that is revolutionising my Mac productivity. A significant use of my iMac is to power through my BDs and DVDs, for which I use a mixture of MakeMKV, HandBrake and then a folder of services that are useful for batch tagging etc. that makes it all look pretty in iTunes and on the Apple TV.
However, I need some help with my HandBrake script that I have so far. HandBrakeCLI is in my /usr/bin folder and is running fine from the script. The issue is that I think there might be something wrong with the way I've labelled the directories to output to. If someone could give me a heads up why this wouldn't work or a solution, I'd be most grateful:
- Code: Select all
filename=$(basename $1)
filename=${filename%.*}
output_directory="/Users/Freddie/Movies/Blu-Ray Converter"
output_filenameHD="$output_directory/$filename (HD).m4v"
output_filenameSD="$output_directory/$filename (SD).m4v"
HandBrakeCLI -i "$1" -o $output_filenameHD -f mp4 -4 -m -e x264 -b 6000 -2 --cfr -E ca_aac -B 448 -6 5point1 -w 1920 -l 1080 --modulus 8 -s scan -F --subtitle-burned -N eng ; HandBrakeCLI -i "$1" -o $output_filenameSD -f mp4 -m -e x264 -b 1500 -2 --cfr -E ca_aac -B 160 -6 dpl2 -w 720 -l 576 --custom-anamorphic --display-width 1024 --modulus 8 -s scan -F --subtitle-burned -N eng
Best to you all
Re: HandBrakeCLI Script

Posted:
Thu May 16, 2013 4:40 pm
by sjk
Hi there.
Call me dense, but I'm not quite sure what your issue is. Have you tested the script with Hazel yet or wanting help checking its syntax before doing that?
Two things:
- Move HandBrakeCLI to /usr/local/bin; /usr/bin is intended to be reserved for Apple's use.
- Use a full pathname for HandBrakeCLI in your script (see the recent
similar suggestion).
Re: HandBrakeCLI Script

Posted:
Thu May 16, 2013 4:57 pm
by FreddieShoreditch
I've tried both of the suggestions you made.
The reason I was posting is that I've tried the script in Hazel and HandBrakeCLI comes up in activity monitor and shows it's definitely going at it, but there are no files that are written either on the final pass or that are moved into the folders I say at the end, which is puzzling me.
Re: HandBrakeCLI Script

Posted:
Thu May 16, 2013 5:36 pm
by a_freyer
That's because you're not quoting
output_filenameHD and
output_filenameSD:
- Code: Select all
/path/to/HandBrakeCLI -i "$1" -o "$output_filenameHD" -f mp4 -4 -m -e x264 -b 6000 -2 --cfr -E ca_aac -B 448 -6 5point1 -w 1920 -l 1080 --modulus 8 -s scan -F --subtitle-burned -N eng
/path/to/HandBrakeCLI -i "$1" -o "$output_filenameSD" -f mp4 -m -e x264 -b 1500 -2 --cfr -E ca_aac -B 160 -6 dpl2 -w 720 -l 576 --custom-anamorphic --display-width 1024 --modulus 8 -s scan -F --subtitle-burned -N eng
Re: HandBrakeCLI Script

Posted:
Thu May 16, 2013 7:33 pm
by sjk
Good call, a_freyer!
And thanks for the extra explanation, FreddieShoreditch!
Re: HandBrakeCLI Script

Posted:
Fri May 17, 2013 5:19 am
by FreddieShoreditch
I've changed around my code a little bit to try and make it work a bit better.
- Code: Select all
filename=$(basename $1)
filename=${filename%.*}
converting_directory="/Users/Freddie/Movies/HandBrake - Converting (Blu-Ray)/"
output_directory="/Users/Freddie/Movies/Blu-Ray Converter/"
converting_filenameHD="$converting_directory$filename (HD).m4v"
converting_filenameSD="$converting_directory$filename (SD).m4v"
/usr/local/bin/HandBrakeCLI -i "$1" -o "$converting_filenameHD" -f mp4 -4 -m -e x264 -b 6000 -2 --cfr -E ca_aac -B 448 -6 5point1 -w 1920 -l 1080 --modulus 8 -s scan -F --subtitle-burned -N eng
mv "$converting_filenameHD" "$output_directory"
/usr/local/bin/HandBrakeCLI -i "$1" -o "$converting_filenameSD" -f mp4 -m -e x264 -b 1500 -2 --cfr -E ca_aac -B 160 -6 dpl2 -w 720 -l 576 --custom-anamorphic --display-width 1024 --modulus 8 -s scan -F --subtitle-burned -N eng
mv "$converting_filenameSD" "$output_directory"
The above still runs under /bin/bash
I tried it last night and got it to work nearly perfectly. However, the biggest problem I found this morning is that the file names are strange.
Taking the film "Just Go With It (2011).mkv" as the base file, with $1, I presume, having the value of "/Users/Freddie/Movies/Blu-Ray Converter/Just Go With It (2011).mkv" therefore:
Rather than the file names being of the form, "Just Go With It (2011).m4v", when they are moved back into the output directory, they are of the following form,
Blu-Ray
Just
Go
With
It
(2011) (HD).mkv
I can't quite remember whether the last line had another break as I changed it before I wrote on the forum. Does anyone know why it would be doing this though? Is there something else I've missed?
May I say a big thank you so far for your support, this is really appreciated!
Re: HandBrakeCLI Script

Posted:
Fri May 17, 2013 5:33 am
by FreddieShoreditch

Just so you can see what I mean with another film.
Re: HandBrakeCLI Script

Posted:
Fri May 17, 2013 6:23 am
by FreddieShoreditch
Once again, I've changed the code slightly, just to try and focus it a bit more.
I don't know if the below image helps solve the problem as to why this is occurring?

Re: HandBrakeCLI Script

Posted:
Fri May 17, 2013 11:17 am
by a_freyer
Again, this is a quoting issue. My apologies, I should have noticed this one too from your original post.
You must enclose
$1 in quotes to get the basename of the file (see first line), else you will return a newline-delimited list of the base names of each space in the directory:
- Code: Select all
filename=$(basename "$1")
filename=${filename%.*}
converting_directory="/Users/Freddie/Movies/HandBrake - Converting (Blu-Ray)/"
output_directory="/Users/Freddie/Movies/Blu-Ray Converter/"
converting_filenameHD="$converting_directory$filename (HD).m4v"
converting_filenameSD="$converting_directory$filename (SD).m4v"
/usr/local/bin/HandBrakeCLI -i "$1" -o "$converting_filenameHD" -f mp4 -4 -m -e x264 -b 6000 -2 --cfr -E ca_aac -B 448 -6 5point1 -w 1920 -l 1080 --modulus 8 -s scan -F --subtitle-burned -N eng
mv "$converting_filenameHD" "$output_directory"
/usr/local/bin/HandBrakeCLI -i "$1" -o "$converting_filenameSD" -f mp4 -m -e x264 -b 1500 -2 --cfr -E ca_aac -B 160 -6 dpl2 -w 720 -l 576 --custom-anamorphic --display-width 1024 --modulus 8 -s scan -F --subtitle-burned -N eng
mv "$converting_filenameSD" "$output_directory"
These files
are converting because you're passing "
$1" properly to handbrake, but if you echo the
filename variable, you'll see that it looks like you're seeing in the output.
Re: HandBrakeCLI Script

Posted:
Fri May 17, 2013 2:11 pm
by sjk
FreddieShoreditch wrote:Taking the film "Just Go With It (2011).mkv" as the base file, …
Just wondering … if any video tracks in your MKV files are already in H.264 format could you use a much quicker, quality retaining passthrough option in other software instead of reencoding it with HandBrakeCLI?
Re: HandBrakeCLI Script

Posted:
Sat May 18, 2013 12:45 pm
by FreddieShoreditch
The films that I'm reencoding are either directly from Blu-Rays (usually), or in some format that is of an aspect ratio of >1.78:1
I've got a thing about viewing films in 16:9 given that is the aspect ratio of my TV, iMac, most monitors etc. so I always encode with that in mind.
I'll try quoting in the first line and see what comes out. Excited!
Re: HandBrakeCLI Script

Posted:
Sat May 18, 2013 3:42 pm
by sjk
FreddieShoreditch wrote:The films that I'm reencoding are either directly from Blu-Rays (usually)
Don't they typically use a video codec (AVC/H.264?) that's compatible with iTunes & Apple TV (mentioned in your original post)? Also curious why you're reencoding audio to AAC instead AC-3.
Btw, I've never used Blu-Ray.
or in some format that is of an aspect ratio of >1.78:1
I've got a thing about viewing films in 16:9 given that is the aspect ratio of my TV, iMac, most monitors etc. so I always encode with that in mind.
That makes sense. I prefer retaining the aspect ratio even with letterboxing/pillarboxing/windowboxing playback.
And smaller file sizes with reencoding can be desirable when any quality difference is insignificant or unnoticeable.
Thanks for your feedback (and patience

).
PS: Just noticed
HandBrake 0.9.9 was released today.
Re: HandBrakeCLI Script

Posted:
Sat May 18, 2013 4:54 pm
by FreddieShoreditch
A lot of them are H.264 or VC-1. The former is not a problem, the problem is the file size.
Most BD films are lets say 15-25GB in size and as cheap as storage is, it's not that cheap. I have a collection of around 400 films, i.e. 10TB worth of stuff my computer has gone through. You can see how with a large Aperture library and music libraries, that becomes a problem.
I reencode the audio to AAC rather than do AC3 passthrough generally for homogeneity and also for compatibility. Most things do AC3, but having H.264 + AAC is probably the most compatible format. Also, some films have DTS rather than AC3. I'd rather just have it all in one format.
Most people seem to like having it in the resolution and aspect ratio it was meant for, but I've spent too many years looking at black bars to care for that anymore.
I've found that a 6Mb/s ABR with 0.5Mb/s CBR isn't noticeably distinguishable from the original, but brings the file size down to around 4-6GB usually. That's far more pleasing for me.
I'll try the new release as I've been using the latest release candidate whilst I've been trying to put this script together. It seems to have a problem with the Life file I've been chucking at it today which is slightly strange. The video that's come out has been corrupted and very strange. I don't know why, but it did. I'll give it another go with the 0.9.9 release.
The script now seems to be working, at least the HandBrake part. Now I've just got to get it to move the file automatically when it's finished and put it on the desktop for tagging. Alternatively, I also need to look into writing metadata to it automatically using Batch actions or iFlicks.
Thank you for your patience and help, you've really helped me so much.
Re: HandBrakeCLI Script

Posted:
Mon Nov 18, 2013 8:09 pm
by Plex
I've found this thread most helpful - thanks everyone. What has me confused, and I'm going back to the beginning, is the mapping syntax at the beginning:
- Code: Select all
filename=$(basename "$1")
filename=${filename%.*}
converting_directory="/Users/Freddie/Movies/HandBrake - Converting (Blu-Ray)/"
output_directory="/Users/Freddie/Movies/Blu-Ray Converter/"
converting_filenameHD="$converting_directory$filename (HD).m4v"
converting_filenameSD="$converting_directory$filename (SD).m4v"
Can anyone recommend an online resource (or these forums) so I can educate myself on this part of the script - how to use it, what the variables are, and how they interrelate, etc.?
I've created a HandBrakeCLI script with my own particular settings and have pasted into Hazel with the above text as the lead in (modified to fit my particular directories) but the script never runs. The HandBrakeCLI runs fine if I start it from the terminal.
Any ideas?
Thanks!
Re: HandBrakeCLI Script

Posted:
Tue Nov 19, 2013 9:35 am
by Plex
To the extent it's helpful, here's the actual code I'm using and the colors I'm seeing in the script:
If *all* *Extension* *is* MTS
Do the following to the matched file or folder:
*Run shell script* *embedded script*
Shell: /bin/bash
filename=$(basename "$1")
filename=${filename%.*}
converting_directory=“/Volumes/HTPC/Media/Movies/AVCHD/Process/”
output_directory="/Volumes/HTPC/Media/Movies/AVCHD/Output/“
converting_filenameHD="$converting_directory$filename (HD).m4v"
/usr/local/bin/HandBrakeCLI -i "$1" -o "$converting_filenameHD” -f mp4 -4 --decomb -w 1920 -l 1080 --loose-anamorphic --modulus 2 -e x264 -q 20 -r 30 --pfr -a 1 -E faac -6 dpl2 -R Auto -B 160 -D 0 --gain 0 --audio-fallback ffac3 --x264-profile=high --h264-level=4.0
And my logs:
2013-11-19 06:42:36.901 hazelworker[14698] 20131117184830.MTS: Rule Process Sony to Handbrake CLI matched.
2013-11-19 06:42:37.218 hazelworker[14698] [Error] Shell script failed: Error processing shell script on file /Volumes/HTPC/Media/Movies/AVCHD/Process/20131117184830.MTS.
2013-11-19 06:42:37.219 hazelworker[14698] Shellscript exited with non-successful status code: 2
2013-11-19 06:42:37.225 hazelworker[14698] Done processing folder Process