Skip to content

Instantly share code, notes, and snippets.

@bwarden
Forked from thornbill/post-process.sh
Last active December 13, 2021 05:28
Show Gist options
  • Save bwarden/fa9c6c713386e7c0552a8e1490440533 to your computer and use it in GitHub Desktop.
Save bwarden/fa9c6c713386e7c0552a8e1490440533 to your computer and use it in GitHub Desktop.
DVR post processing script for Jellyfin
#!/usr/bin/env bash
set -o errexit
set -o pipefail
set -o nounset
set -o xtrace
PWD="$(pwd)"
die () {
echo >&2 "$@"
cd "${PWD}"
exit 1
}
# Colors
GREEN='\033[0;32m'
NC='\033[0m' # No Color
__path="${1:-}"
# verify a path was provided
[ -n "$__path" ] || die "path is required"
# verify the path exists
[ -f "$__path" ] || die "path ($__path) is not a file"
__dir="$(dirname "${__path}")"
__file="$(basename "${__path}")"
__base="$(basename "${__path}" ".ts")"
# Debbuging path variables
printf "${GREEN}path:${NC} ${__path}\ndir: ${__dir}\nbase: ${__base}\n"
# Try to find local version of ffmpeg, defaults to the path used in docker if not found
__ffmpeg="$(which ffmpeg || echo '/usr/lib/jellyfin-ffmpeg/ffmpeg')"
# Change to the directory containing the recording
cd "${__dir}"
# Extract closed captions to external SRT file
printf "[post-process.sh] %bExtracting subtitles...%b\n" "$GREEN" "$NC"
#$__ffmpeg -f lavfi -i movie="${__file}[out+subcc]" -map 0:1 "${__base}.srt"
$__ffmpeg -f lavfi -i movie="$(printf %q "${__file//\'/\\\'}" | sed "s#\:#\\\\\\\\:#")[out+subcc]" -map 0:1 "${__base}.srt"
# Decide whether we need to transcode video or audio
VCODEC=$(ffprobe -loglevel error -select_streams v -show_entries stream=codec_name -of default=nw=1:nk=1 "${__file}" | sort -u)
ACODEC=$(ffprobe -loglevel error -select_streams a -show_entries stream=codec_name -of default=nw=1:nk=1 "${__file}" | sort -u)
ENCODE_ARGS=""
if [[ "$VCODEC" != "h264" ]]; then
ENCODE_ARGS="$ENCODE_ARGS -c:v libx264"
else
ENCODE_ARGS="$ENCODE_ARGS -c:v copy"
fi
if [[ "$ACODEC" != "ac3" ]]; then
ENCODE_ARGS="$ENCODE_ARGS -c:a aac"
else
ENCODE_ARGS="$ENCODE_ARGS -c:a copy"
fi
# Transcode to mp4, crf parameter can be adjusted to change output quality
printf "[post-process.sh] %bTranscoding file..%b\n" "$GREEN" "$NC"
#$__ffmpeg -i "${__file}" -vcodec libx264 -vf yadif=parity=auto -crf 20 -preset veryslow "${__base}.mp4"
$__ffmpeg -i "${__file}" -map 0 $ENCODE_ARGS "${__base}.mkv"
# Remove the original recording file
#printf "[post-process.sh] %bRemoving original file...%b\n" "$GREEN" "$NC"
#rm "${__file}"
# Return to the starting directory
cd "${PWD}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment