Last active
August 29, 2015 14:18
-
-
Save samirfor/6dd0d0e8e2344ba3d093 to your computer and use it in GitHub Desktop.
This script adds a subtitle file srt in a mkv file. In addition, it converts the encoding of the srt file to UTF-8, for some TVs (such as the Sony Bravia KDL-40W605) use this as the default encoding.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
################################################################ | |
# This script adds a subtitle file srt in a mkv file. # | |
# In addition, it converts the encoding of the srt file # | |
# to UTF-8, for some TVs (such as the Sony Bravia KDL-40W605) # | |
# use this as the default encoding. # | |
# # | |
# TIP: Combine with script "mkvsrtaddbatch" (available in # | |
# https://gist.github.com/samirfor/3e9fe8aa2a2feb89f8e7to) to # | |
# process a batch of files at once. # | |
################################################################ | |
usage="[i] Usage: $(basename "$0") video.mkv" | |
# Checking parameter | |
if [ -z "$1" ]; then | |
echo ""; echo "$usage"; echo ""; exit | |
fi | |
# Setting up vars | |
mkvfile=$1 | |
newsrt="/tmp/tmpsrt.srt" # place to put converted subtitle | |
newmkvfolder=$(dirname "${mkvfile}") | |
filename="${mkvfile##*/}" | |
filename="${filename%.*}" | |
if [ "${filename: -6}" == ".nosub" ]; then | |
filename=${filename:0:${#filename}-6} # remove '.nosub' suffix | |
echo "INFO: '.nosub' suffix removed." | |
fi | |
if [ "${filename: -3}" == ".br" ]; then | |
echo "INFO: Skipping... this file has subtitle already." | |
exit 0 | |
fi | |
# Adding suffix in new filename | |
suffix="br" # to distinguish the old mkv from the new | |
newmkvfilename="${filename}.${suffix}.mkv" # gets the filename without the path | |
subtitle="${newmkvfolder}/${filename}.srt" # full path filename | |
newmkv=$newmkvfilename # do the work on current directory | |
# Checking available free space | |
freeSpace=$(df -k . | tail -n 1 | awk -F' ' '{ print $4 }') # k = 1024 bytes | |
fileSize=$(du -k "$1" | awk -F' ' '{ print $1 }') # k = 1024 bytes | |
diffSize=$((fileSize-freeSpace)) | |
#echo -e " Size: $fileSize\nF.Space: $freeSpace\n Diff: $diffSize" | |
if [ "$diffSize" -gt 0 ]; then | |
echo "ERROR: Insufficient disk space. The minimum required free space is at least the size of the input file." | |
echo | |
exit 1 | |
fi | |
# Checking if subtitle file exist | |
if [ ! -f "$subtitle" ]; then | |
echo "SKIP: Subtitle cannot be found. Be sure it is a srt file and name \ | |
match with video's name." | |
echo | |
exit 2 | |
fi | |
# Checking srt encode type | |
strencode=$(file "$subtitle") | |
if [[ "$strencode" =~ ISO-8859 ]]; then | |
# Converting srt subtitle from ISO-8859-1 to UTF-8 | |
iconv -f ISO-8859-1 -t UTF-8 "$subtitle" > "$newsrt" | |
echo "INFO: Subtitle encode converted to UTF-8." | |
elif [[ "$strencode" =~ UTF-8 ]]; then | |
# Simply copying the srt file | |
cp "$subtitle" "$newsrt" | |
echo "INFO: Subtitle encode UTF-8 ok." | |
else | |
echo "ERROR: Subtitle encode unknown." | |
read -p "Convert to UTF-8 anyway? [Y/n] " -n 1 -r | |
echo # (optional) move to a new line | |
if [[ $REPLY =~ ^[Nn]$ ]]; then | |
echo "INFO: Aborted. You have to convert subtitle encode manually." | |
exit 1 | |
else | |
iconv -f ISO-8859-1 -t UTF-8 "$subtitle" > "$newsrt" | |
echo "INFO: Subtitle encode converted to UTF-8." | |
fi | |
fi | |
# Add the new srt file into mkv container without video reencode (about 20 seconds) | |
mkvmerge -o "$newmkv" "$mkvfile" --language 0:por --track-name 0:Portuguese "$newsrt" | |
success=$? | |
# Cleaning original files | |
if [ $success == 0 ]; then | |
echo "SUCCESS: Subtitle added" | |
rm "$mkvfile" "$subtitle" | |
echo "INFO: The original files were removed." | |
else | |
echo "ERROR: A error ocurred. Be aware." | |
fi | |
rm "$newsrt" # Cleaning temp subtitle file | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fix MacOS compatibility