-
-
Save b-ggs/d5b99a6233340d3d6806c13698691b92 to your computer and use it in GitHub Desktop.
This file contains 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 | |
# usage: ./remove-mkv-subtitles.sh [--no-dry] [path(s)] | |
# Script to find MKV files in the given paths and remove subtitle tracks from them. Dry run by default. | |
# (usually) non-default dependencies: mkvtoolnix, dc | |
remvsub(){ | |
set -e | |
orig="$1" | |
temp="$2" | |
# Copy from $orig path to $temp path, excluding subtitles (-S) | |
# This requires no transcoding by mkvmerge, so it is relatively | |
# quick, bottlenecked on disk io. | |
mkvmerge -q -o "$temp" -S "$orig" | |
mv "$temp" "$orig" | |
} | |
scanmkv(){ | |
mkv="$1" | |
dryrun="$2" | |
nicepath="$(basename "$mkv")" | |
echo -en "[\e[0;34mSCAN\e[m] $nicepath" | |
# Check if we need to bother | |
mkvinfo "$mkv" | grep -q 'Track type: subtitles' | |
ret=$? | |
if [ $ret -ne 0 ]; then | |
echo -e "\e[0K\r[\e[0;33mSKIP\e[m] $nicepath" | |
exit | |
fi | |
if $dryrun; then | |
echo -e "\e[0K\r[\e[0;32mNOOP\e[m] $nicepath" | |
exit | |
fi | |
of="$mkv" | |
nf="$mkv.nosubs" | |
ofs=$(stat -c%s "$of") | |
touch "$nf" > /dev/null 2>&1 | |
if [ $? -ne 0 ]; then | |
echo -e "\e[0K\r[\e[0;31mPERM\e[m] $nicepath" | |
exit | |
fi | |
# Execute the removal in the background, so we can | |
# display a nice progress meter inline | |
remvsub "$of" "$nf" > /dev/null 2>&1 & | |
# progress counter, will advance after background | |
# remvsub function finishes running | |
while ps -p $! > /dev/null 2>&1; do | |
sleep 0.5 | |
if [ -e "$nf" ]; then | |
nfs=$(stat -c%s "$nf") | |
pc=$(echo "2k $nfs $ofs / 100 * p" | dc) | |
pad_pc=$(printf "%03.f" $pc) | |
echo -en "\e[0K\r[\e[0;32m$pad_pc%\e[m] $nicepath" | |
fi | |
done | |
# If the new (temporary) file still exists after our | |
# background job exited we know the process failed. | |
if [ -e "$nf" ]; then | |
echo -e "\e[0K\r[\e[0;31mFAIL\e[m] $nicepath" | |
else | |
echo -e "\e[0K\r[\e[0;32mDONE\e[m] $nicepath" | |
fi | |
} | |
if [ "$1" = '--no-dry' ]; then | |
shift | |
dryrun=false | |
else | |
dryrun=true | |
echo "Dry-run mode. 'NOOP' means this file would have its subtitles removed" | |
echo "To act on these files run: $0 --no-dry $@" | |
fi | |
defpath=. | |
if [ ! -z "$@" ]; then | |
defpath="$@" | |
fi | |
export -f scanmkv remvsub | |
find $path "$defpath" -name '*.mkv' -type f -print0 | xargs -0 -IARG bash -c "scanmkv \"ARG\" $dryrun" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment