Skip to content

Instantly share code, notes, and snippets.

@QuynhVir
Created December 8, 2023 13:00
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 QuynhVir/02c9be525f933e55f3453fe48d74147c to your computer and use it in GitHub Desktop.
Save QuynhVir/02c9be525f933e55f3453fe48d74147c to your computer and use it in GitHub Desktop.
The script will convert files containing the EAC3, DTS, or DTS-HD audio codecs to use the AAC codec through the AudioToolbox encoder specified as aac_at. The converted files will get the suffix -AAC_at.mkv to distinguish them from the original files
#!/bin/bash
# Function to check and convert the file if necessary
check_and_convert() {
local file=$1
[ -d "$file" ] && return # Skip directories
# Use ffprobe to get the codec type for each audio stream
codec_info=$(ffprobe -v error -select_streams a -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$file")
# Check if the file uses the EAC3 or DTS/DTS-HD codec
if echo "$codec_info" | grep -E -q '^(eac3|dts|dtshd)$'; then
# Prepare the output file name
output_basename=$(basename "$file" "${file##*.}") # Remove file extension
output="${output_basename}AAC_at.mkv"
echo "Converting file '$file' to use AAC audio codec with AudioToolbox."
# Run ffmpeg to convert the file with the incompatible audio codecs to AAC using AudioToolbox
ffmpeg -i "$file" -map 0 -c:v copy -c:a aac_at -b:a 768k -ac 6 -c:s copy "$output"
else
echo "Skipping file '$file' because it does not use EAC3 or DTS/DTS-HD codec."
fi
}
# Enable nullglob so unmatched patterns are removed from the word list
shopt -s nullglob
# Process .mkv and .mp4 files
for f in *.mkv *.mp4; do
check_and_convert "$f"
done
# Disable nullglob option if needed
shopt -u nullglob
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment