- -- Based on the inner workings of MKV4XBox created by Stephan Linke
- -- http://www.macupdate.com/info.php/id/30486/mkv4xbox
- on open names
- -- tools
- set MEDIAINFO to "/Library/Application\\ Support/MKV4XBox/Tools/mediainfo"
- set MKVEXTRACT to "/Library/Application\\ Support/MKV4XBox/Tools/mkvextract"
- set MKVPATCH to "/Library/Application\\ Support/MKV4XBox/Tools/mkvpatch"
- set FFMPEG to "/Library/Application\\ Support/MKV4XBox/Tools/ffmpeg"
- set DTSDEC to "/Library/Application\\ Support/MKV4XBox/Tools/dtsdec"
- set MP4BOX to "/Library/Application\\ Support/MKV4XBox/Tools/mp4box"
- -- process each file supplied
- repeat with input_file in names
- -- get the path to the file
- set input_path to POSIX path of input_file
- -- get the base directory of file
- set file_dir to do shell script "perl -e '$_=\"" & input_path & "\";/(.*)\\/(.*?)$/;print $1'"
- -- get the file name, minus .mkv extension
- set file_name to do shell script "perl -e '$_=\"" & input_path & "\";/(.*)\\/(.*?).mkv$/i;print $2'"
- -- get the name of TV show - probably want to separate this out at some point
- -- set tv_show_name to do shell script "perl -e '$_=\"" & input_path & "\";/.*\\/(.*?)$/;$1=~/(.*?\\.s\\d\\de\\d\\d)/i;$filename=$1;$filename=~tr/A-Z/a-z/;print $filename'"
- -- get the ID for the video
- set video_id to do shell script MEDIAINFO & " '--Inform=Video;%ID%' " & quoted form of input_path
- -- get the ID for the audio
- set audio_id to do shell script MEDIAINFO & " '--Inform=Audio;%ID%' " & quoted form of input_path
- -- get the video framerate
- set frame_rate to do shell script MEDIAINFO & " '--Inform=Video;%FrameRate%' " & quoted form of input_path
- -- get the audio format
- set audio_format to do shell script MEDIAINFO & " '--Inform=Audio;%Format%' " & quoted form of input_path
- -- get the audio sampling rate
- set sampling_rate to do shell script MEDIAINFO & " '--Inform=Audio;%SamplingRate%' " & quoted form of input_path
- -- working directory
- set working_dir to file_dir & "/convert-" & file_name
- -- create working directory
- try
- do shell script "mkdir " & quoted form of working_dir
- end try
- -- set up some variables
- set file_movie to working_dir & "/movie.x264"
- set file_audiotrack to working_dir & "/audiotrack.ac3"
- set file_audio to working_dir & "/audio.mp4"
- set file_moviemp4 to working_dir & "/movie.mp4"
- set file_outputfile to file_dir & "/" & file_name & ".mp4"
- -- extract mkv
- do shell script MKVEXTRACT & " tracks " & quoted form of input_path & " " & video_id & ":" & quoted form of file_movie & " " & audio_id & ":" & quoted form of file_audiotrack
- -- patch the source video file
- do shell script "dd if=" & MKVPATCH & " bs=1 conv=notrunc of=" & quoted form of file_movie & " seek=7 count=2"
- -- convert the source audio file
- do shell script FFMPEG & " -i " & quoted form of file_audiotrack & " -ab 320 -aq " & sampling_rate & " -vol 256 -ac 2 -acodec aac " & quoted form of file_audio
- -- mux audio and video together
- do shell script MP4BOX & " -add " & quoted form of file_movie & "#video -add " & quoted form of file_audio & "#audio -fps " & frame_rate & " " & quoted form of file_moviemp4
- -- move output file back to file dir
- do shell script "mv " & quoted form of file_moviemp4 & " " & quoted form of file_outputfile
- -- clean up working directory
- try
- do shell script "rm -rf " & quoted form of working_dir
- end try
- -- move source file to trash
- do shell script "mv " & quoted form of input_path & " ~/.Trash/"
- end repeat
- end open