HandbrakeCLI und Hazel

13 Comments

Ich habe euch hier  ja bereits einige Möglichkeiten zur Video Konvertierung vorgestellt. Doch da auch ich tendenziell eher faul bin und so viel wie möglich automatisiere, auch wenn das oft mehr Zeit in Anspruch nimmt als es schnell selber zu machen ;) bietet es sich natürlich auch hier an einen schicken Workflow zusammen zu basteln.

Das HandbrakeCLI gibt es auf der Handbrake Seite zum Download und es bietet die gleichen möglichkeiten wie die GUI version.

Nach dem Download kopieren wir HandbrakeCLI wie üblich in unseren Application Ordner. Öffnen ein Terminal und lassen uns die Verfügbaren Presetts anzeigen.
cd /Applications
./HandBrakeCLI -z

Auf der linken Seite sehen wir den Namen des Presets und auf der rechten die für dieses Preset verwendeten Einstellungen. Ob wir das preset nutzen oder die kompletten Einstellungen kopieren ist eigentlich egal es würde zum gleichen Ergebnis führen. Um zu sehen was die einzelnen Optionen bedeuten können wir uns mit ./HandBrakeCLI -h die komplette Hilfe anzeigen lassen.

Die nutzung erfolg immer nach folgendem schema:
HandBrakeCLI -i source -o destination
gefolgt von weiteren einstellungen.

Generell ist es eine gute Idee wenn möglich vorhandene Presets zu verwenden die man ggf. an die eigenen Bedürfnisse anpasst, so unterlaufen einem bei der Eingabe weniger Fehler.

Ein Beispiel mit dem iPhone & iPod Touch preset sieht so aus:

./HandBrakeCLI -i ~/Desktop/test.avi -o ~/Desktop/test.mp4 -Z "iPhone & iPod Touch"

Das iPhone & iPod Preset mit verringerter Audio und Video Qualität.

./HandBrakeCLI -i ~/Desktop/test.avi -o ~/Desktop/test.mp4 -Z "iPhone & iPod Touch" -B 96 -q 25.0

Soviel zur Theorie ;)

Als erstes erstellen wir einen Ordner in den später die Videos kopiert werden. In diesem erstellen wir zwei weitere Ordner einen für die umgewandelten videos und den anderen für die originale. Bei mir heißen diese source und mp4.

Jetzt brauchen wir noch ein paar Hazel Regeln und ein kleines Shell Script.

In Hazel fügen wir unseren Ordner hinzu und erstellen eine neue Regel.

Wenn ein Film erkannt wird soll zuerst das Script ausgeführt werden und anschließend die Datei in den Source ordner verschoben werden, man könnte sie natürlich auch direkt löschen. Und damit wir nicht ständig nachgucken müssen ob das Video schon fertig ist gibts am ende noch eine Benachrichtigung per Growl.

Und hier das Shell Script, wenn ihr andere Einstellungen möchtet könnt ihr es nach dem gleichen Schema wie oben abändern.

if [[ $1 =~ (/[a-zA-ZäÄöÖüÜ_.-]*)\..* ]]; then
/Applications/HandBrakeCLI -i "$1" -o "mp4/${BASH_REMATCH[1]}.mp4" -Z "iPhone & iPod Touch" -B 96 -q 25.0
fi

Je nach bedarf kann man den Ordner jetzt noch in die Sidebar vom Finder ziehen.

Viel Spaß ;)

Related posts:

  1. iOS 4 jailbreak
  2. iOS 4 auf iPod Touch 2. Generation
  3. Hazel Security Workflow
  4. Zeitraffer und Stopmotion Videos mit ffmpeg erstellen
  5. Video Konvertierung Teil 2: Video Monkey

13 Comments (+add yours?)

  1. Michael
    Okt 31, 2010 @ 21:53:50

    Hi,

    first of all many thanks for for this nice tutorial, When I copy files into my defined movie folder the hazel worklow starts, I can see that because in my finder i see the files are beeing moved to the source folder and i got the growl notification. But anyway there is no encoding to mp4. I can see that for 2 reasons.

    1. No files are in mp4 folder
    2. The growl notification comes directly after I put a video file into my movie folder.
    So there is no encoding time.

    I am using your shell script just with another preset:

    if [[ $1 =~ (/[a-zA-ZäÄöÖüÜ_.-]*)\..* ]]; then
    echo “$1 matches”
    echo ${BASH_REMATCH[1]}
    /Applications/HandBrakeCLI -i “$1″ -o “mp4/${BASH_REMATCH[1]}.mp4″ -Z “High Profile”
    fi

    Do you have an idea why there is no encoding done ?

  2. goe
    Okt 31, 2010 @ 22:44:03

    Hi
    it seems to be my fault, there shouldn’t be a slash in front of the bracket.
    this one should work.
    if [[ $1 =~ ([a-zA-ZäÄöÖüÜ_.-]*)\..* ]]; then
    /Applications/HandBrakeCLI -i “$1″ -o “mp4/${BASH_REMATCH[1]}.mp4″ -Z “High Profile”
    fi

    to test this out just save it as text file named test.sh or something like this.
    copy a test movie file into the same directory
    then go to the terminal and move with cd to the path you saved the file to
    then type
    chmod +x test.sh
    ./test.sh yourmoviefile.avi

  3. goe
    Okt 31, 2010 @ 22:50:44

    i’ve forgotten to mention that you have to include
    #!/bin/sh in the first line of the text file if you wanna test this.
    in hazel this is not necessary.

  4. Michael
    Nov 01, 2010 @ 01:41:25

    Hi again,

    many thanks now its working. But it renames the movies to for instance isfhixsdufhixsfuhisfhsixfhxsifhisx.avi into s.mpg. Is that expected? I am totally new to scripts. Another question which comes up is how can I see if the workflow is doing progress? I just have the feeling it stoped after it encoded the first movie. files are moved to source but nothing more happend so far and this is 10 min ago.

  5. goe
    Nov 01, 2010 @ 20:29:01

    no, the filename shout be the old filename only with a new extension. I checked it with the filename you provided and for me it works. Maybe you have to improve the regexp pattern to match your filenames. Just google for regexp and you will see how it works.

    you can’t see if the workflow is making progress. but you can take a look at the activity monitor or type “top” at the terminal.
    When running, Handbrake will produce some heavy load on your cpu.
    also you can take a look at the filesize of the output files. Handbrake runs in the background and you can’t see its output. Maybe it is possible to run it in the foreground. but i don’t think that this is quiet usefull.
    Just test your script on the commandline to make shure it works like you expect.

    A major issue with this workflow is that all files are processed in parallel.
    This mean if you drop more than one file at a time into your hazel folder, all will be converted at the same time.
    There shout be some sort of queue to avoid this.

  6. Michael
    Nov 05, 2010 @ 18:39:07

    Hi goe,

    what will happen when I have a started process, for instance i put a view movies in the folder. OK then hazel grabs it and starts. But what happens when I then (may be 2 min later) put another file in the folder?
    Does it mean handbreak starts over again, does it mean there is a queu?

    I will try to sort my script issue out now. Did not have so much time in the last days.

    rgds

    Michael

  7. Michael
    Nov 05, 2010 @ 19:01:02

    Hi goe,

    pls can you check my test result below:

    ./handbreakcli.sh my.name.is.earl.s03e04.720p.hdtv.x264-ctu.mp4
    Output format couldn’t be guessed from file name, using default.
    [18:56:08] hb_init: checking cpu count
    [18:56:08] hb_init: starting libhb thread
    HandBrake 0.9.4 (2009112300) – Darwin x86_64 – http://handbrake.fr
    2 CPUs detected
    Opening “my.name.is.earl.s03e04.720p.hdtv.x264-ctu.mp4″…
    [18:56:08] hb_scan: path=“my.name.is.earl.s03e04.720p.hdtv.x264-ctu.mp4″, title_index=1
    [18:56:08] scan: trying to open with libdvdread
    libdvdnav: Using dvdnav version 4.1.3
    libdvdread: Using libdvdcss version 1.2.10 for DVD access
    libdvdread: Can’t stat “my.name.is.earl.s03e04.720p.hdtv.x264-ctu.mp4″
    No such file or directory
    libdvdnav: vm: failed to open/read the DVD
    [18:56:08] dvd: not a dvd – trying as a stream/file instead
    [18:56:08] hb_stream_open: open “my.name.is.earl.s03e04.720p.hdtv.x264-ctu.mp4″ failed
    [18:56:08] scan: unrecognized file type
    [18:56:08] libhb: scan thread found 0 valid title(s)
    No title found.
    HandBrake has exited.

    And the same happens when I change the name of the source movie:

    ./handbreakcli.sh mynameisearl.mp4
    Output format couldn’t be guessed from file name, using default.
    [18:59:52] hb_init: checking cpu count
    [18:59:52] hb_init: starting libhb thread
    HandBrake 0.9.4 (2009112300) – Darwin x86_64 – http://handbrake.fr
    2 CPUs detected
    Opening “mynameisearl.mp4″…
    [18:59:52] hb_scan: path=“mynameisearl.mp4″, title_index=1
    [18:59:52] scan: trying to open with libdvdread
    libdvdnav: Using dvdnav version 4.1.3
    libdvdread: Using libdvdcss version 1.2.10 for DVD access
    libdvdread: Can’t stat “mynameisearl.mp4″
    No such file or directory
    libdvdnav: vm: failed to open/read the DVD
    [18:59:52] dvd: not a dvd – trying as a stream/file instead
    [18:59:52] hb_stream_open: open “mynameisearl.mp4″ failed
    [18:59:52] scan: unrecognized file type
    [18:59:52] libhb: scan thread found 0 valid title(s)
    No title found.
    HandBrake has exited.

    rgds

    Michael

  8. goe
    Nov 05, 2010 @ 19:50:48

    1. When you drop another file into the folder hazel will start another Handbrake process. There is NO queue so far.
    2. Please try this expression the one i posted above seems not to be very reliable. I hope this one finally works.

    if [[ $1 =~ (.*)\..[^.]* ]]; then
    /Applications/HandBrakeCLI -i “$1″ -o “mp4/${BASH_REMATCH[1]}.mp4″ -Z “iPhone & iPod Touch”
    fi

  9. Michael
    Nov 05, 2010 @ 21:00:04

    Again, no encoding done. I don´t think its the regex:

    ./handbreakcli.sh mynameisearl.mp4
    Output format couldn’t be guessed from file name, using default.
    [20:55:29] hb_init: checking cpu count
    [20:55:29] hb_init: starting libhb thread
    HandBrake 0.9.4 (2009112300) – Darwin x86_64 – http://handbrake.fr
    2 CPUs detected
    Opening “mynameisearl.mp4″…
    [20:55:29] hb_scan: path=“mynameisearl.mp4″, title_index=1
    [20:55:29] scan: trying to open with libdvdread
    libdvdnav: Using dvdnav version 4.1.3
    libdvdread: Using libdvdcss version 1.2.10 for DVD access
    libdvdread: Can’t stat “mynameisearl.mp4″
    No such file or directory
    libdvdnav: vm: failed to open/read the DVD
    [20:55:29] dvd: not a dvd – trying as a stream/file instead
    [20:55:29] hb_stream_open: open “mynameisearl.mp4″ failed
    [20:55:29] scan: unrecognized file type
    [20:55:29] libhb: scan thread found 0 valid title(s)
    No title found.
    HandBrake has exited.

    With the gui version from handbreak its no problem, encoding is ongoing now:

    [20:56:53] hb_init: checking cpu count
    [20:56:53] hb_init: starting libhb thread
    [20:56:53] macgui: Handbrake Version: 0.9.4 i386 (2009112300)
    [20:56:53] hb_init: checking cpu count
    [20:56:53] hb_init: starting libhb thread
    [20:56:53] hb_init: checking cpu count
    [20:56:53] hb_init: starting libhb thread
    [20:57:19] macgui: trying to open video_ts folder (parent directory chosen)
    [20:57:19] hb_scan: path=/Users/michael-bierewitz/mynameisearl.mp4, title_index=0
    [20:57:19] scan: trying to open with libdvdread
    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
    [20:57:19] dvd: not a dvd – trying as a stream/file instead
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from ‘/Users/michael-bierewitz/mynameisearl.mp4′:
    Duration: 00:20:16.98, start: 0.000000, bitrate: 1584 kb/s
    Stream #0.0(und): Video: h264, yuv420p, 640×352 [PAR 1:1 DAR 20:11], 30 tbr, 30 tbn, 60 tbc
    Stream #0.1(und): Audio: aac, 44100 Hz, stereo, s16
    [20:57:19] scan: decoding previews for title 1
    [20:57:19] scan: 10 previews, 640×352, 30.000 fps, autocrop = 0/0/2/0, aspect 16:9, PAR 1:1
    [20:57:19] scan: title (0) job->width:640, job->height:352
    [20:57:19] libhb: scan thread found 1 valid title(s)
    [20:57:54] macgui: Rip: Pending queue count is 0
    [20:57:54] macgui: scanning specifically for title: 1
    [20:57:54] macgui: performNewQueueScan currentQueueEncodeIndex is: 0
    [20:57:54] hb_scan: path=/Users/michael-bierewitz/mynameisearl.mp4, title_index=1
    [20:57:54] scan: trying to open with libdvdread
    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
    [20:57:54] dvd: not a dvd – trying as a stream/file instead
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from ‘/Users/michael-bierewitz/mynameisearl.mp4′:
    Duration: 00:20:16.98, start: 0.000000, bitrate: 1584 kb/s
    Stream #0.0(und): Video: h264, yuv420p, 640×352 [PAR 1:1 DAR 20:11], 30 tbr, 30 tbn, 60 tbc
    Stream #0.1(und): Audio: aac, 44100 Hz, stereo, s16
    [20:57:54] scan: decoding previews for title 1
    [20:57:54] scan: 10 previews, 640×352, 30.000 fps, autocrop = 0/0/2/0, aspect 16:9, PAR 1:1
    [20:57:54] scan: title (0) job->width:640, job->height:352
    [20:57:54] libhb: scan thread found 1 valid title(s)
    [20:57:54] macgui: ScanDone state received from fQueueEncodeLibhb
    [20:57:54] macgui: Preset: Normal (Default)
    [20:57:54] macgui: processNewQueueEncode number of passes expected is: 1
    [20:57:54] macgui: prepareJob exiting
    [20:57:54] 1 job(s) to process
    [20:57:54] starting job
    [20:57:54] job configuration:
    [20:57:54] * source
    [20:57:54] + /Users/michael-bierewitz/mynameisearl.mp4
    [20:57:54] + title 1, chapter(s) 1 to 1
    [20:57:54] + container: mov,mp4,m4a,3gp,3g2,mj2
    [20:57:54] + data rate: 1584 kbps
    [20:57:54] * destination
    [20:57:54] + /Users/michael-bierewitz/Movies/mynameisearl.mp4
    [20:57:54] + container: MPEG-4 (.mp4 and .m4v)
    [20:57:54] * video track
    [20:57:54] + decoder: h264
    [20:57:54] + frame rate: same as source (around 30.000 fps)
    [20:57:54] + strict anamorphic
    [20:57:54] + modulus: 0
    [20:57:54] + storage dimensions: 640 * 352 -> 638 * 352, crop 0/0/2/0
    [20:57:54] + pixel aspect ratio: 1 / 1
    [20:57:54] + display dimensions: 638 * 352
    [20:57:54] + encoder: x264
    [20:57:54] + options: ref=2:bframes=2:subq=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0
    [20:57:54] + quality: 20.00 (RF)
    [20:57:54] * audio track 0
    [20:57:54] + decoder: Unknown (AAC) (track 1, id 1)
    [20:57:54] + mixdown: Stereo
    [20:57:54] + encoder: ca_aac
    [20:57:54] + bitrate: 160 kbps, samplerate: 48000 Hz
    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
    [20:57:54] dvd: not a dvd – trying as a stream/file instead
    [20:57:54] reader: first SCR 0 id 1 DTS 0
    [20:57:54] encx264: keyint-min: 30, keyint-max: 300
    [20:57:54] encx264: encoding with stored aspect 1/1
    [20:57:54] encx264: Encoding at constant RF 20.000000
    x264 [info]: using SAR=1/1
    x264 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 FastShuffle SSE4.1 Cache64
    x264 [info]: profile Main, level 3.0
    [20:57:54] sync: expecting 36539 video frames

    Output top:

    19811- HandBrake 140.0 02:46.35 18/10 2 160 758 95M+ 89M 116M+ 194M 1166M 19811 219 running

  10. Michael
    Nov 05, 2010 @ 21:02:24

    Thats means the video file is ok. The regex should not be a problem. Filename is pretty normal, no strange pattern or smth like that.
    WTF I really want to use your tutorial I like it, but honestly I don#t know what I am doing wrong:(

  11. goe
    Nov 05, 2010 @ 21:47:42

    sorry but i can’t reproduce this error. when i pass a filename of a file that dosn’t exist to the script, i get nearly the same output only without the
    “Output format couldn’t be guessed from file name, using default.”

    When u execute it in the terminal be sure that script and movie are in the same directory.

    maybe its an issue with the handbrake preset.
    does it work with other files not mp4?

  12. iMartien
    Jan 27, 2011 @ 16:53:33

    My source folder for the handbrake encodes contains multiple video files and I dont want to start the HandbreakCLI process multiple times…

    Is there a way to check if the handbreakCLI proccess is already running? And if it is exit the script without doing anything?

Leave a Reply