Skip to content

Instantly share code, notes, and snippets.

@cmbirk
Created October 25, 2013 04:58
Show Gist options
  • Save cmbirk/7149605 to your computer and use it in GitHub Desktop.
Save cmbirk/7149605 to your computer and use it in GitHub Desktop.
#!/bin/bash
filedir=$1;
echo "Starting converstion to mp4 script"
if [ -d "$filedir" ]; then
cd "$filedir"
for file in ./* ;
do
if [[ $file = *.mkv || $file = *.avi ]] ; then
if [[ $file == *","* ]] ; then
newfile=${file//,/}
mv "$file" "$newfile"
file="$newfile"
fi
file=${file##*/}
donefile=${file:0:${#file}-4}".mp4"
############################
# First, extract subtitles #
############################
# Find out which tracks contain the subtitles
if [[ $file = *.mkv ]] ; then
mkvmerge -i "$file" | grep 'subtitles' | while read subline
do
# Grep the number of the subtitle track
tracknumber=`echo $subline | egrep -o "[0-9]{1,2}" | head -1`
# Get base name for subtitle
subtitlename=${file%.*}
# Extract the track to a .tmp file
echo "Extracting subtitles for file: $file"
`mkvextract tracks "$file" $tracknumber:"$subtitlename.srt.tmp" > /dev/null 2>&1`
`chmod g+rw "$subtitlename.srt.tmp"`
# Do a super-primitive language guess: ENGLISH
langtest=`egrep -ic ' je | tu | le ' "$subtitlename".srt.tmp`
# Check if subtitle passes our language filter (10 or more matches)
if [ $langtest -ge 10 ]; then
`mv $subtitlename.srt.tmp $subtitlename.srt`
`chmod g+rw "$subtitlename.srt"`
else
`rm "$subtitlename.srt.tmp"`
fi
done
fi
# Wait while any other ffmpeg processes are running
while [ -n "$(ps -ef | egrep "ffmpeg" | grep -v grep)" ];
do
echo -e "\n[$(date +%b\ %d\ %Y:\ %H:%M:%S)]\nFound another instance of ffmpeg running, pausing 5 minutes..."
sleep 300
done
# ffmpeg cmd
/usr/local/bin/ffmpeg -i "$file" -c:v copy -c:a libfdk_aac "$donefile"
if [ -f "$donefile" ]; then
rm "$file"
fi
fi
done
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment