Skip to content

Instantly share code, notes, and snippets.

@markmals
Last active June 16, 2020 01:08
Show Gist options
  • Save markmals/c69550688eaa8309ceda8083a9be2fe1 to your computer and use it in GitHub Desktop.
Save markmals/c69550688eaa8309ceda8083a9be2fe1 to your computer and use it in GitHub Desktop.
Transcode video files to Apple-compatible HEVC using ffmpeg
if [[ -d "$1" ]]; then
for file in "$1"/**/*(.); do
ext="${file##*.}"
if [[ $ext == "mkv" ]] || [[ $ext == "mp4" ]] || [[ $ext == "avi" ]]; then
transcode "$file"
fi
done
trash "$1"
elif [[ -f "$1" ]]; then
orig="$1"
ext="${orig##*.}"
base=$(basename $orig $ext)
# sub="${orig%.*}.srt"
# sub=$(printf %q $sub) # /Users/ullr/Movies/Downloads/Vikings\ S01/Vikings\ -\ S01E08.srt
out="$(pwd)/${base}mp4"
vtest=$(ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 $orig)
atest=$(ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 $orig)
vcodec="-c:v libx265 -crf 23"
acodec="-c:a eac3 -b:a 320k"
# subcmd=""
if [[ $vtest == "hevc" ]]; then
vcodec="-c:v copy"
fi
if [[ $atest == "eac3" ]]; then
acodec="-c:a copy"
fi
# if [[ -f $sub ]]; then
# subcmd="-i $sub -c:s mov_text -metadata:s:s:0 language=eng"
# fi
ffmpeg -i "$orig" -metadata:s:a:0 language=eng -metadata:s:a:0 title="ENG" ${=vcodec} ${=acodec} -tag:v hvc1 "$out"
trash "$orig"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment