Skip to content

Instantly share code, notes, and snippets.

@Forceflow
Last active September 4, 2021 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Forceflow/92ad0db7aec9ae1aa66774e5a4070824 to your computer and use it in GitHub Desktop.
Save Forceflow/92ad0db7aec9ae1aa66774e5a4070824 to your computer and use it in GitHub Desktop.
Video EAC3 to AC3
#!/bin/bash
# This is a script to convert one or more videos with EAC3 audio to AC3 audio. EAC3 audio triggers audio conversion,
# which leads to this Plex bug: https://forums.plex.tv/t/subtitles-disappear-too-fast-or-line-not-showing-when-transcoding-audio/727924/3
# Requirements: ffmpeg, ffprobe and some space in your /tmp folder, which will be used to store converted files.
#
# This script will only convert files if they contain "eac3" in their ffprobe audio stream output.
# Your original files will be REPLACED if the conversion takes place.
#
# Usage: toAC3.sh /path/to/file1 /path/tofile2 (etc.)
#-----------------------------------------------------------
# LOGGER FUNCTION
SCRIPTNAME="toAC3"
getDATE () { date '+%F %T'; }
SCRIPTNAME=""; NCB='\033[0m'; BLUEBG='\033[44m\033[1m'; GREENBG='\033[42m\033[1m'; REDBG='\033[41m\033[1m'; YELLOWBG='\033[43m\033[1m'
logNice() { if [ "$1" == "ok" ]; then echo -e "${GREENBG}->${NCB} $(getDATE) ($SCRIPTNAME): $2"
elif [ "$1" == "warn" ]; then echo -e "${YELLOWBG}->${NCB} $(getDATE) ($SCRIPTNAME): $2"
elif [ "$1" == "fail" ]; then echo -e "${REDBG}x>${NCB} $(getDATE) ($SCRIPTNAME): $2" >&2
elif [ "$1" = "start" ] || [ "$1" = "stop" ]; then echo -e "${BLUEBG}-> $(getDATE) ($SCRIPTNAME): $2${NCB}"
else echo -e "${BLUEBG}->${NCB} $(getDATE) ($SCRIPTNAME): $2"; fi }
# ----------------------------------------------------------
# Script starts here
# No arguments: return
if [ $# -eq 0 ]; then logNice "fail" "No files given, exiting"; exit 1; fi
# If more than one argument: list all the files we're going to process
if [ $# -gt 1 ]; then
logNice "info" "Running script on $# files: "
for arg in "$@"
do
echo "- $arg"
done
fi
# Loop over files and convert to AC3 where necessary
for arg in "$@"
do
FILEPATH="$arg"
BASE=$(basename "$FILEPATH")
TEMPFILE="/tmp/$BASE.converting.mkv"
# Check if the file contains eac3 audio
ffprobe -loglevel quiet -show_streams -select_streams a "$FILEPATH" | grep -q "eac3"
CHECK=$?
if [ $CHECK -eq 1 ]; then
logNice "warn" "Not converting: $FILEPATH does not contain EAC3 audio"
continue
else
logNice "ok" "Converting: $FILEPATH contains EAC3 audio"
fi
logNice "info" "Converting: Output to $TEMPFILE"
ffmpeg -hide_banner -loglevel quiet -stats -i "$FILEPATH" -map 0 -c copy -c:a aac -c:a ac3 -b:a 640k "$TEMPFILE"
if [ $? -eq 0 ]; then
rm "$FILEPATH"
mv "$TEMPFILE" "$FILEPATH"
else
logNice "fail" "An error occured, not removing input file $FILEPATH, but removing temp file $TEMPFILE"
rm "$TEMPFILE"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment