Skip to content

Instantly share code, notes, and snippets.

@swarzesherz
Forked from lad1337/transcode10to8.sh
Last active August 29, 2015 14:05
Show Gist options
  • Save swarzesherz/80d38b883040c6afe7b9 to your computer and use it in GitHub Desktop.
Save swarzesherz/80d38b883040c6afe7b9 to your computer and use it in GitHub Desktop.
reencode 10-bit anime mkvs into 8-bit versions using HandBrakeCli and mkvmerge, only when needed
#!/bin/bash
# ----------
# This script uses HandBrakeCLI to convert any mkv x264 video to 8-bit (high @ 4.1 Profile)
# and this only when needed!!
#-----------
# based on the script by enolive located here http://pastebin.com/2rCh13Pk
# ----------
# Needed tools:
# HandBrakeCLI get it here http://handbrake.fr/downloads2.php
# MKVToolNix get https://www.bunkus.org/videotools/mkvtoolnix/downloads.html
# ----------
# Usage:
# <this_file> -i FILE
# ----------
NO_ARGS=0
E_OPTERROR=85
USAGE="$(basename $0) [-i file] [-o file] [-d] [-t] [-t] [-h] -- reencode 10-bit anime mkvs into 10-bit versions using HandBrakeCli
where:
-h print this help and exit
-i input file
-o output file. if it is ommited output will be the <input_file_basename>[8-bit].mkv
-d dirmode. if it ommit output and input
-r replace. replace the input file. a temp file is writen and then replaced with the temp file. -o is irgnored
-v verbose. show final CMD, HandBreak output
-t test. only transcode the first 30s
-f force transcode
-p profile. currently only \"magao\"
- magao: high quality profile. from http://forum.xbmc.org/showthread.php?tid=106051&pid=1061440#pid1061440"
if [ $# -eq "$NO_ARGS" ] # Script invoked with no command-line args?
then
echo "$USAGE"
exit $E_OPTERROR # Exit and explain usage.
# Usage: scriptname -options
# Note: dash (-) necessary
fi
#defaults
REPLACE=0
INPUT=''
OUTPUT=''
TEST=false
DEBUG=0
PROFILE=''
IFS='
'
input=""
output=""
# initialize variables
while getopts ":ro:i:c:dftvp:h" Option
do
case $Option in
o ) output="$OPTARG";;
i ) input="$OPTARG";;
f ) force=1;;
c ) chapters="$OPTARG";;
t ) test=1;;
d ) decomb=1;;
v ) verbose=1;;
p ) profile="$OPTARG";;
r ) replace=1;;
h ) echo "$USAGE"; exit $E_OPTERROR;;
* ) echo "Unimplemented option chosen.";; # Default.
esac
done
if [ "${input}" = "" ]
then
dirmode=1
fi
spinner()
{
local pid=$1
local message=$2
local delay=0.25
local start=$(date +"%s")
local spinstr='|/-\'
while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
local temp=${spinstr#?}
printf "\r%s [%c] " "${message}" "${spinstr}"
local spinstr=$temp${spinstr%"$temp"}
sleep $delay
done
local duration=$(($(date +"%s")-$start))
printf "\r%s [done] %02d:%02d:%02d" "${message}" "$((${duration}/3600))" "$(((${duration}%3600)/60))" "$((${duration}%60))"
}
transcode(){
if [ "${output}" = "" ]
then
output=${input##*/}
tmpfile=${output%.*}[8-bit].tmp.mkv
output=${output%.*}[8-bit].mkv
else
tmpfile=${output##*/}
tmpfile=${tmpfile%.*}.tmp.mkv
fi
HandBrakeCLI --scan -i "$input" 2>&1 | grep -q "High 10"
isHi10=$?
if [ ${isHi10} -eq 0 -o ${force:-0} -eq 1 ]
then
echo "Processing $input -> $output ..."
# no audio
# constant framerate
# quality 18
# 8 <----- 18 <----- 23 -----> 28 -----> 51
# lossless better worse worst
CMD="HandBrakeCLI -a none -e x264 --x264-tune animation --x264-profile high --h264-level 4.1 --cfr -q18 -o \"$tmpfile\" -i \"$input\""
if [ ${test:-0} -eq 1 ]
then
echo "test run first 30s"
CMD="$CMD --stop-at duration:30"
fi
if [ "${chapters}" != "" ]
then
CMD="${CMD} --chapters ${chapters}"
fi
if [ "${deinterlace}" != "" ]
then
CMD="${CMD} --deinterlace ${deinterlace}"
fi
if [ ${decomb:-0} -eq 1 ]
then
CMD="${CMD} --decomb"
fi
if [ "$profile" != "" ]
then
echo "Profile choosen: $profile"
fi
# add new profiles here
case $profile in
# from http://forum.xbmc.org/showthread.php?tid=106051&pid=1061440#pid1061440 .. well thats what i think it is :3
magao ) CMD="$CMD -x ref=6:weightp=1:rc-lookahead=10:trellis=0:8x8dct=0:bframes=5:b-adapt=2:me=umh:merange=9:psy-rd=0.5,0.00";;
* ) ;; # Default. use HandBrakes Defaults
esac
if [ ${verbose:-0} -eq 0 ]
then
CMD="$CMD 2>/dev/null"
else
echo "Transcoding: $CMD"
fi
echo "Processing in progress, this can take very long, plz wait..."
# run transcoding
# use eval beacuse of spaces in file names ...
eval $CMD
if [ ${verbose:-0} -eq 0 ]
then
spinner $! "Transcoding"
echo ""
fi
CMD="mkvmerge -o \"$output\" -D \"$input\" -A -S -T --no-global-tags --no-chapters \"$tmpfile\" --track-order 1:0"
if [ ${test:-0} -eq 1 ]
then
CMD="$CMD --split parts:00:00:00-00:00:30"
fi
if [ "${chapters}" != "" ]
then
CMD="${CMD} --split chapters:${chapters}"
fi
if [ ${verbose:-0} -eq 0 ]
then
CMD="$CMD &>/dev/null &"
else
echo "Merging: $CMD"
fi
eval $CMD
if [ ${verbose:-0} -eq 0 ]
then
spinner $! "Merging"
fi
# clean up temp files
if [ -e "${tmpfile}" ]
then
rm -f "${tmpfile}"
fi
if [ ${replace:-0} -eq 1 ]
then
rm -f "$input"
mv "$output" "$input"
output=$input
fi
echo
echo "########################"
echo "Processing DONE ^_^"
echo "8bit file at: $output"
echo "########################"
else
echo
echo "########################"
echo "NOT processing $input its not 10bit :3"
echo "########################"
fi
}
if [ ${dirmode:-0} -eq 1 ]
then
for i in $( find ./ -name "*.mkv" -o -name "*.mp4" )
do
output=""
input=${i##*/}
transcode
done
else
transcode
fi
# all done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment