|
#!/bin/bash |
|
# file-name: mkv-substrip.sh |
|
# Website: http://needforbits.tumblr.com/ |
|
# https://gist.github.com/maxwelleite |
|
# https://github.com/maxwelleite |
|
# Author: Maxwel Leite |
|
# License: GPLv3 |
|
# Description: Remove all subtitle tracks (strip) from all MKV files in a specified folder or file, |
|
# then put the new mkv file(s) in a previously-created sub-folder named "output". |
|
# Param: "folder" (with a least one mkv file) or "file.mkv" (*relative paths also works*) |
|
# "-f" -> force to remuxing every mkv file, even if don't have any subtitle (disabled for default) |
|
# Usage: |
|
# Folder (mkv files): mkv-substrip.sh "/home/user/folder-to-mkv-files" |
|
# Single file: mkv-substrip.sh "/home/user/file-with-subtitle.mkv" |
|
# For a (whole) local dir: mkv-substrip.sh . |
|
# Force to remux: mkv-substrip.sh "/home/user/file-with-subtitle.mkv" -f |
|
# TODO: Apply/check a header Compression fix (if needed) |
|
# Last update: 2019-07-13 |
|
|
|
clear |
|
if ! which mkvmerge >/dev/null; then |
|
echo "" |
|
echo "Error: mkvmerge is required (from mkvtoolnix)" |
|
echo "Run the following command to install it:" |
|
echo "sudo apt-get install mkvtoolnix" |
|
echo "Or, install the latest version from:" |
|
echo "https://mkvtoolnix.download/downloads.html" |
|
exit |
|
fi |
|
|
|
_checkSub() { |
|
if [ "$checksub" == "1" ]; then |
|
# check if mkv file have any subtitle |
|
if [ -z "`mkvmerge -i "$mkvfile" | grep subtitles`" ]; then |
|
echo -e "mkv-substrip: Skipped, no subtitle found!\n" >> "$outputlog" |
|
echo -e "Skipped, no subtitle found!" |
|
((skipped++)) |
|
# have a error |
|
error=1 |
|
fi |
|
fi |
|
} |
|
|
|
_Do() { |
|
# if have error, skip |
|
if [ "$error" == "0" ]; then |
|
mkvmerge -o "$outputmkv" --no-subtitles --no-track-tags --no-global-tags --disable-track-statistics-tags --ui-language en_US "$mkvfile" >> "$outputlog" |
|
if grep -q -i Error "$outputlog"; then |
|
((errors++)) |
|
echo -e "mkv-substrip: Error found in file. File corrupted/damage!?\n" >> "$outputlog" |
|
echo -e "Error! File corrupted/damage!?" |
|
else |
|
echo -e "Done!" |
|
fi |
|
fi |
|
} |
|
|
|
_Summary(){ |
|
echo "mkv-substrip: End log" >> "$outputlog" |
|
echo " " |
|
echo "ALL DONE!" |
|
echo "" |
|
echo "Summary:" |
|
echo "" |
|
echo "Total of MKV files: $files" |
|
echo "Skipped files: $skipped" |
|
echo "Errors detected: $errors" |
|
echo "" |
|
echo "Checkout the output folder:" |
|
echo "$outputdir" |
|
echo "" |
|
exit |
|
} |
|
|
|
# if set "-f" == force to remuxing every mkv file, even if don't have any subtitle (skip checkSub procedure) |
|
[ "$2" == "-f" ] && checksub=0 || checksub=1 |
|
|
|
|
|
# checks if the directory exists |
|
if [ -d "$1" ]; then |
|
|
|
# void problems with relative path |
|
realdir=`readlink -f $1` |
|
|
|
# checks if the directory is writable |
|
if [ ! -w "$realdir" ]; then |
|
echo "" |
|
echo -e "Error: The folder is not writable.\nCheck if it have right permissions." |
|
echo "" |
|
exit |
|
fi |
|
|
|
# check if the folder have any mkv file (insensive case) |
|
if [ -z "`find "$realdir" -maxdepth 1 -iname '*.mkv' -print0 -quit`" ]; then |
|
echo "" |
|
echo -e "Error: Couldn't find any MKV file in this location:\n\n$realdir" |
|
echo "" |
|
exit |
|
fi |
|
|
|
outputdir="$realdir"/output/ |
|
mkdir -p "$outputdir" >/dev/null |
|
echo -e "mkv-substrip.sh\n" |
|
echo "Processing files..." |
|
errors=0 |
|
skipped=0 |
|
files=0 |
|
|
|
# Looping through files with spaces in the names (insensive case wildcard in find and works with white space) |
|
# http://unix.stackexchange.com/a/9499/99915 |
|
# backup IFS |
|
OIFS="$IFS"; IFS=$'\n' |
|
for mkvfile in `find "$realdir" -maxdepth 1 -iname '*.mkv' -print`; do |
|
error=0 |
|
echo -n "'"`basename "$mkvfile"`"'... " |
|
outputlog="$outputdir`basename "$mkvfile"`.log" |
|
outputmkv="$outputdir`basename "$mkvfile"`" |
|
((files++)) |
|
|
|
# begin to logging |
|
echo "mkv-substrip:" > "$outputlog" |
|
_checkSub |
|
_Do |
|
done |
|
# restore IFS |
|
IFS="$OIFS" |
|
_Summary |
|
fi |
|
|
|
# checks if file exists and if current folder is writable. |
|
if [ -f "$1" ]; then |
|
files=1 |
|
error=0 |
|
errors=0 |
|
skipped=0 |
|
|
|
mkvfile=`readlink -f $1` |
|
if [ ! -w "`dirname "$mkvfile"`" ]; then |
|
echo "" |
|
echo -e "Error: The folder is not writable.\nCheck if it have right permissions." |
|
echo "" |
|
exit |
|
fi |
|
echo -e "mkv-substrip.sh\n" |
|
echo "Processing file..." |
|
echo -n "'"`basename "$mkvfile"`"'... " |
|
outputdir="`dirname "$mkvfile"`"/output/ |
|
outputlog="$outputdir`basename "$mkvfile"`.log" |
|
outputmkv="$outputdir`basename "$mkvfile"`" |
|
mkdir -p "$outputdir" >/dev/null |
|
# begin to logging |
|
echo "mkv-substrip: Start log" > "$outputlog" |
|
_checkSub |
|
_Do |
|
_Summary |
|
fi |
|
|
|
echo "" |
|
echo -e "Error: Nothing to do.\nTry again!" |
|
echo "" |
|
echo "Usage:" |
|
echo " For a folder: mkv-substrip.sh /home/user/folder-to-mkv-files" |
|
echo " For a file: mkv-substrip.sh /home/user/file-with-subtitle.mkv" |
|
echo " For a local dir: mkv-substrip.sh ." |
|
echo "" |