Shell script to join & convert video files

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Shell script to join & convert video files Mon Apr 02, 2012 9:52 pm • by zouav
Hi there,
Sorry my first post is a cry for help rather than a helpful tip, I started using Hazel only a few days ago and I'm not quite there yet! However, my question could interest people in a quest of tweaking further their video files management.

Basically I'm trying to do 2 things:
[list=]
[*]join movie files that are split in xxx-cd[digit].extension - for avi & mkv files (99% of my video files)
[*]convert movies currently in 2 files (1 for the video + 1 for the subtitles) into just 1 mkv file - for srt & sub files
[/list]
Being 1) not knowledgeable when it comes to programming 2) but still geek enough to spend quite a few hours on google to search for a solution, I stumbled upon a lot of tutorials about sorting TV shows folders and files. At first, I thought I could adapt the one from http://www.mactalk.com.au/content/organising-media-hazel-pt-2-986/ to solve problem 1 (joining files).

Here is what I started with...

Image

Until I realised AJoiner coulnd't be controlled through applescript because it doesn't have an applescript dictionary. So I was just opening it up but still have to do the rest manually. Half-baked.

For problem 2 (merging video+subtitles into one mkv file), I did discover a way to convert sub to srt (http://www.robelix.com/sub2srt/) and then use mkvmerge with the srts.

Fortunately I found a similar question from a Linux user looking to batch clean his video storage folder with shell scripts: https://www.linuxquestions.org/questions/programming-9/making-a-batch-shell-script-to-convert-merge-and-append-join-movies-to-mkv-773699/

I have been trying to adapt his bits of code to a Hazel routine but either the 2 files I'm trying to join as a test vanish (deleted) without being joined or I get a shell script error. I have never used shell script before and I can't seem to find my way around it, although I can see that those bits of code (hereafter) are the way to go.

To join video files:
Code: Select all
#!/bin/bash
for FIRST in ./*.cd1.mkv ; do
   NAME=$(basename "$FIRST" .cd1.mkv)
   REST=("./$NAME".cd[^1].mkv)
   mkvmerge -o "$NAME.mkv" "$FIRST" "${REST[@]/#/+}"
   rm "$FIRST"
   rm "${REST[@]/#}"
done

To merge video & subtitle files:
Code: Select all
#!/bin/bash
AVI_DIR="avi"
SRT_DIR="srt"
MKV_DIR="mkv"
for AVI in "$AVI_DIR"/*.avi ; do
   NAME=$(basename "$AVI" .avi)
   if [[ -r "$SRT_DIR/$NAME.srt" ]] ; then
      echo "Subbing $NAME (SRT)"
      mkvmerge -o "$MKV_DIR/$NAME.mkv" "$AVI" "$SRT_DIR/$NAME.srt"
      rm "$AVI"
      rm "$SRT_DIR/$NAME.srt"
   else
      echo "No subtitles found for $NAME"
   fi
done

So if anyone could point me in the right direction or is willing to help out with those code snippets, I'd be very grateful :D

Thanks 8)
zouav
 
Posts: 1
Joined: Mon Apr 02, 2012 8:41 pm

You scripts seem to assume what directory it's in, which is a very bad thing to do in general. Hazel passes in the file or folder as $1 so whatever you do, base it off of that. You can use the basename or dirname commands as needed.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support