#!/usr/bin/env bash

echo "Enter Movie Title:"

read -r title

mkdir "../$title/temp"

HandBrakeCLI -i "../$title/$title - 4k DV.mkv" -o "../$title/temp/handbrake.mkv" -f av_mkv -m -e x265_10bit --encoder-preset slower -q 20 --encoder-profile auto --all-audio -E copy --audio-copy-mask aac,eac3,dts,ac3,truehd,dtshd,mp3 --crop-mode auto --auto-anamorphic --all-subtitles

ffmpeg -i "../$title/$title - 4k DV.mkv" -c:v copy -f hevc - | ./dovi_tool -m 2 --crop extract-rpu - -o "../$title/temp/rpu.bin"

ffmpeg -i "../$title/temp/handbrake.mkv" -c:v copy "../$title/temp/nonhdr.hevc"

./dovi_tool inject-rpu -i "../$title/temp/nonhdr.hevc" --rpu-in "../$title/temp/rpu.bin" -o "../$title/temp/hdrcompressed.hevc"

mkvmerge --output "../$title/$title - 4k DVP8 Subs.mkv" "../$title/temp/hdrcompressed.hevc" -D -S -B -T --no-chapters -M --no-global-tags "../$title/temp/handbrake.mkv" --default-track-flag 0:no --language 0:en "../$title/$title.en.srt" -D -A "../$title/temp/handbrake.mkv"
