Last active
May 1, 2020 02:29
-
-
Save sqbs/f8898fbf54b8ca11313a03fee1e45891 to your computer and use it in GitHub Desktop.
reemux.sh v1.0
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/sh | |
#====================================================================== | |
# reemux.sh by sqbs | |
#====================================================================== | |
# | |
# Special thanks to Bill, lts | |
# | |
#====================================================================== | |
# WHAT IS IT? | |
#====================================================================== | |
# | |
# A POSIX script for removing unwanted tracks from MKVs | |
# | |
# Requires: | |
# | |
# - mkvtoolnix-cli | |
# | |
#====================================================================== | |
# WHAT DOES IT DO? | |
#====================================================================== | |
# | |
# Creates a remuxed MKV container and removes specified tracks | |
# | |
#====================================================================== | |
# HOW TO USE / EXAMPLES | |
#====================================================================== | |
# | |
# When prompted to select audio/sub tracks select the number | |
# next to mkvextract: (track ID for mkvmerge & mkvextract: 1) | |
# (track ID for mkvmerge & mkvextract: 2) | |
# (track ID for mkvmerge & mkvextract: 3) | |
# | |
# reemux.sh file1.mkv file*.mkv file_S*E*.mkv | |
# | |
# Ask for audio and sub tracks before each file | |
# | |
# reemux.sh -f file1.mkv file*.mkv file_S*E*.mkv | |
# | |
# Same audio and sub tracks selected for all | |
# | |
# reemux.sh -c file1.mkv file*.mkv file_S*E*.mkv | |
# | |
# Only trigger on MKVs with labelled commentary tracks | |
# | |
# reemux.sh -C file1.mkv file*.mkv file_S*E*.mkv | |
# | |
# Auto target and filter labelled commentary tracks | |
# | |
#====================================================================== | |
# FUNCTIONS | |
#====================================================================== | |
version="1.0" | |
info() { | |
echo "" | |
printf "reemux.sh v%s " "$version" | |
printf "\n\n%s\n\n" "https://gist.github.com/sqbs/f8898fbf54b8ca11313a03fee1e45891" | |
} | |
help() { | |
info | |
echo "A POSIX script for removing unwanted tracks from MKVs" | |
echo "" | |
echo "Syntax: burnsub.sh [-c|C|f|h|v]" | |
echo "Options:" | |
echo "" | |
echo " c Target videos with detactable commentary tracks" | |
echo " C Auto target and remove commentary tracks" | |
echo " f Same audio/subtitle selection for all files" | |
echo " h Display this screen" | |
echo " v Display version number" | |
echo "" | |
} | |
get_user_input() { | |
file=$* | |
case "${file_ext}" in | |
"mkv") | |
printf "\nFile: \"%s\"\n\n" "$file" | |
;; | |
*) | |
printf "\nSource file is not a .mkv\n" | |
exit 1 | |
;; | |
esac | |
printf "%s\n" "$mkvinfo" | |
printf "\nSpecify tracks to remove separated by \",\": " | |
read -r selected_tracks | |
case $selected_tracks in | |
*[0-9,]*) ;; | |
"") ;; | |
*) | |
printf "\n\"%s\" is/are not valid tracks\n" "$selected_tracks" | |
exit 1 | |
;; | |
esac | |
} | |
has_commentary() { | |
export IFS="&" | |
mkvinfo=$(mkvinfo "$var" | sed -nr '/[\w\W]*(Track number|Track type|Language|Display|Name)[\w\W]*/p') | |
track=-2 | |
for word in $mkvinfo; do | |
track=$((track+1)) | |
if echo "$word" | grep -q "Track type: audio" ; then | |
if echo "$word" | grep -iq "commentary"; then | |
commentary_track=$((track)) | |
else | |
commentary_track=0 | |
fi | |
fi | |
done | |
} | |
print_execution_time() { | |
time_end=$(date +%s) | |
runtime=$((time_end - time_start)) | |
min=0 | |
hour=0 | |
day=0 | |
if [ $runtime -gt 59 ]; then | |
sec=$((runtime % 60)) | |
runtime=$((runtime / 60)) | |
if [ $runtime -gt 59 ]; then | |
min=$((runtime % 60)) | |
runtime=$((runtime / 60)) | |
if [ $runtime -gt 23 ]; then | |
hour=$((runtime % 24)) | |
day=$((runtime / 24)) | |
else | |
hour=$runtime | |
fi | |
else | |
min=$runtime | |
fi | |
else | |
sec=$runtime | |
fi | |
[ $day -gt 0 ] && output="$output ${day}d" | |
[ $hour -gt 0 ] && output="$output ${hour}h" | |
[ $min -gt 0 ] && output="$output ${min}m" | |
[ $sec -gt 0 ] && output="$output ${sec}s" | |
[ "$output" = "" ] && output="0s" | |
printf "\nExecution time: %s\n\n" "$output" | |
} | |
end() { | |
echo "Done" | |
print_execution_time | |
exit 0 | |
} | |
#=========================================================== | |
# INIT | |
#=========================================================== | |
case "$1" in | |
"-"*["h"]*) help; exit 0;; | |
"-"*["v"]*) info; exit 0;; | |
*);; | |
esac | |
time_start=$(date +%s) | |
[ -x "$(command -v mkvmerge)" ] || pass=false | |
[ "$pass" != false ] || { printf "\nPlease install mkvtoolnix-cli and run me again\n\n"; exit 1; } | |
#=========================================================== | |
# MAIN | |
#=========================================================== | |
case "$1" in | |
"-"*) | |
if [ -z "$2" ]; then | |
file=0 | |
else | |
file=1 | |
fi | |
;; | |
*) | |
if [ -z "$1" ]; then | |
file=0 | |
else | |
file=1 | |
fi | |
;; | |
esac | |
if [ $file -eq 0 ]; then | |
printf "\nNo .mkv file specified\n\n" | |
exit 1 | |
fi | |
printf " | |
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ | |
⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⠋⠉⡁⡉⠙⠻⠿⣿⣿⠿⠛⠛⠛⠛⠿⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿ | |
⣿⣿⣿⣿⣿⣿⣿⡟⢃⢰⢨⡪⡪⠪⠪⠪⡠⡁⢄⢢⢪⢌⢎⢦⢡⢀⠙⣿⣿⣿⣿⣿⣿⣿⣿ | |
⣿⣿⣿⣿⣿⣿⡏⠠⣊⢎⢌⢆⡇⣏⢎⢇⢧⡑⠌⢎⢊⣈⡁⣁⢡⢡⢀⠙⠿⣿⣿⣿⣿⣿⣿ | |
⣿⣿⣿⣿⠟⠛⠡⡱⡱⡱⡕⡵⠱⡑⠥⢑⢐⢘⢄⠳⡱⡱⡱⡱⠕⡕⠱⢀⠄⠘⠻⢿⣿⣿⣿ | |
⣿⣿⠋⢀⠔⡅⡇⡏⡮⣚⡘⡌⢈⣤⣴⣶⣶⣶⣤⡔⠈⠪⠡⠑⠁⣂⣥⣴⣶⠷⢶⣮⢿⣿⣿ | |
⣿⡏⢀⢆⢏⢎⢮⢺⢸⢢⢣⢘⠿⢿⢿⣿⣿⣀⣈⠿⠗⠲⠶⠿⠿⠿⠿⠿⠿⠶⠚⢻⣿⣿⣿ | |
⡟⠃⡆⠥⢸⢸⢪⡪⣪⢣⡣⡣⡄⡄⡄⡈⠨⠁⣂⢂⢅⢔⣐⠢⢢⢐⠄⠔⠄⣠⣴⣿⣿⣿⣿ | |
⡇⢰⢸⢸⡸⡪⣪⠪⡊⡌⡪⡘⡌⢅⢃⢝⠜⠜⠔⡜⡜⡕⡎⡎⠎⠮⡚⡈⡄⢘⣿⣿⣿⣿⣿ | |
⢀⢇⢧⢣⡣⡳⢡⢑⠑⠈⠄⠄⠄⠁⠈⠂⠑⠩⠊⠌⠆⠃⣊⠘⠘⠈⢈⣐⣠⣾⣿⣿⣿⣿⣿ | |
⠐⡕⡵⠱⢱⠑⡌⠄⠄⠄⠠⠄⠄⡀⠄⠄⠄⠄⠄⠄⣴⣾⣿⣿⣶⣿⠿⠛⠉⠉⠈⠙⣿⣿⣿ | |
⠄⢱⢱⢱⡁⡌⡂⠄⠄⠁⠄⠄⠄⠄⠄⠈⠄⠄⠄⠄⠄⠈⠉⠉⠄⠄⣠⣤⣴⣶⣦⣤⣿⣿⣿ | |
⡂⠐⢕⢕⠕⡨⡀⠄⠄⠁⠄⠐⠈⠄⠄⠄⠄⠄⠄⢸⣷⣶⣶⣶⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ | |
⠧⢀⠣⢳⢱⢐⢌⢂⢂⠢⡂⠪⠨⠂⠕⡐⠄⢄⢀⢘⢛⠛⡛⠛⠛⠛⠻⣿⣿⣿⣿⣿⣿⣿⣿ | |
⢌⢦⢳⢱⢬⢸⡐⡔⡢⡪⡪⡱⡙⠄⣃⢃⠬⠤⡄⠥⡠⠡⣀⣀⣦⣤⣾⣿⣿⠟⠻⠟⢛⣿⣿ | |
⢕⢕⢵⢱⢕⢵⢱⡹⠜⢎⢪⡰⡍⡗⡬⡸⢸⢱⢕⡲⡸⡀⡉⠻⢿⢿⠿⠋⢁⢐⠔⠄⠂⢿⣿ | |
⢕⢕⢇⢗⢕⢵⢱⡸⡸⡱⣱⢱⠑⡅⡦⡪⡪⡎⡮⡪⡺⡸⣰⢰⠁⢔⡰⡱⣑⠑⣨⣴⣶⣿⣿ | |
R E E M U X v%s | |
" "$version" | |
if case "$1" in "-"*["c""C"]*) true;; *) false;; esac; then | |
printf "\nSearching files for commentary tracks...\n" | |
fi | |
for var in "$@" | |
do | |
if case "$var" in "-"*["f""c""C"]*) true;; *) false;; esac; then | |
continue | |
fi | |
filename_ext=$(basename -- "$var") | |
file_ext="${filename_ext##*.}" | |
filename=${filename_ext%".$file_ext"} | |
filename_save="${filename}_REEMUX.${file_ext}" | |
folder_path=$(realpath "$var") | |
folder_path=${folder_path%"/$filename_ext"} | |
has_commentary "$var" | |
if case "$1" in "-"*["c""C"]*) true;; *) false;; esac; then | |
if [ "$commentary_track" -eq 0 ]; then | |
printf "\n\"%s\" no commentary tracks detected\n\nSkipping...\n\n" "$filename_ext" | |
continue | |
fi | |
fi | |
case "$1" in | |
"-"*["C"]*) ;; | |
"-"*"f"*) | |
if [ ! "$received_user_input" ]; then | |
get_user_input "$var" | |
received_user_input=true; | |
fi | |
;; | |
*) get_user_input "$var" ;; | |
esac | |
printf "\nPreparing \"%s\"...\n" "$filename_ext" | |
export IFS="&" | |
keep_video='' | |
keep_audio='' | |
keep_subtitles='' | |
track=-2 | |
for word in $mkvinfo; do | |
track=$((track+1)) | |
if case "$1" in "-"*["C"]*) false;; *) true;; esac; then | |
if [ "${selected_tracks#*$track}" != "$selected_tracks" ]; then | |
continue | |
fi | |
fi | |
if echo "$word" | grep -q "Track type: video"; then | |
keep_video="${keep_video},${track}" | |
fi | |
if echo "$word" | grep -q "Track type: audio"; then | |
if case "$1" in "-"*["C"]*) true;; *) false;; esac; then | |
if echo "$word" | grep -iq "commentary"; then | |
continue | |
fi | |
fi | |
keep_audio="${keep_audio},${track}" | |
fi | |
if echo "$word" | grep -q "Track type: subtitles" ; then | |
keep_subtitles="${keep_subtitles},${track}" | |
fi | |
done | |
keep_video=$(echo $keep_video | cut -c 2-) | |
keep_audio=$(echo $keep_audio | cut -c 2-) | |
keep_subtitles=$(echo $keep_subtitles | cut -c 2-) | |
if [ "$keep_video" != "" ]; then | |
mkvmerge_video="--video-tracks $keep_video" | |
else | |
mkvmerge_video="--no-video" | |
fi | |
if [ "$keep_audio" != "" ]; then | |
mkvmerge_audio="--audio-tracks $keep_audio" | |
else | |
mkvmerge_audio="--no-audio" | |
fi | |
if [ "$keep_subtitles" != "" ]; then | |
mkvmerge_subtitles="--subtitle-tracks $keep_subtitles" | |
else | |
mkvmerge_subtitles="--no-subtitles" | |
fi | |
eval "mkvmerge -o \"${folder_path}/${filename_save}\" ${mkvmerge_video} ${mkvmerge_audio} ${mkvmerge_subtitles} \"${folder_path}/${filename_ext}\"" | |
done | |
end "$1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment