#!/bin/bash # Evaluate the file passed to the script for information relevant to the process find . -type f | grep .mkv$ | while read file do directory=`dirname "$file"` title=`basename "$file" .mkv` # Check the audio track used in the files AC3=`mkvinfo "$file" | grep AC3` #check if it's AC3 audio or DTS AAC=`mkvinfo "$file" | grep AAC` order=`mkvinfo "$file" | grep "Track type" | sed 's/.*://' | head -n 1 | tr -d " "` #check if the video track is first or the audio track # Start processing # If video is the first track if [ "$order" = "video" ]; then fps=`mkvinfo "$file" | grep duration | sed 's/.*(//' | sed 's/f.*//' | head -n 1` #store the fps of the video track # If audio is encoded in AC3 if [ -n "$AC3" ]; then mkvextract tracks "$file" 1:"${title}".264 2:"${title}".ac3 ffmpeg -i "${title}".ac3 -acodec libfaac -ab 576k "${title}".aac # mplayer -ao pcm:file="${title}".wav:fast "${title}".ac3 # faac -o "${title}".aac "${title}".wav # If audio is encoded in AAC elif [ -n "$AAC" ]; then mkvextract tracks "$file" 1:"${title}".264 2:"${title}".aac # If encoded in DTS or something else else mkvextract tracks "$file" 1:"${title}".264 2:"${title}".dts ffmpeg -i "${title}".dts -acodec libfaac -ab 576k "${title}".aac fi else # If video is not the first track fps=`mkvinfo "$file" | grep duration | sed 's/.*(//' | sed 's/f.*//' | tail -n 1` if [ -n "$AC3" ]; then mkvextract tracks "$file" 1:"${title}".ac3 2:"${title}".264 ffmpeg -i "${title}".ac3 -acodec libfaac -ab 576k "${title}".aac # mplayer -ao pcm:file="${title}".wav:fast "${title}".ac3 # faac -o "${title}".aac "${title}".wav elif [ -n "$AAC" ]; then mkvextract tracks "$file" 1:"${title}".264 2:"${title}".aac else mkvextract tracks "$file" 1:"${title}".dts 2:"${title}".264 ffmpeg -i "${title}".dts -acodec libfaac -ab 576k "${title}".aac fi fi MP4Box -new "${directory}/${title}".mp4 -add "${title}".264 -add "${title}".aac -fps $fps rm -f "$title".aac "$title".dts "$title".ac3 "$title".264 "${title}".wav # if [ -f "${directory}/${title}".mp4 ]; then # rm -f "$file" # fi done