Skip to content

Instantly share code, notes, and snippets.

@ThomasDalla
Created January 28, 2022 16:42
Show Gist options
  • Save ThomasDalla/4568a8b439a33cd1a8bb264ecefa308a to your computer and use it in GitHub Desktop.
Save ThomasDalla/4568a8b439a33cd1a8bb264ecefa308a to your computer and use it in GitHub Desktop.
Convert the audio of MKV videos in a folder into AAC, retaining original audio as well
#!/bin/bash
#Example: ~/tvshows.sh (from within the desired folder)
ext=( *.mkv )
for e in "${ext[@]}"
do
for f in "$e"
do
if [ -f "$f" ]; then
episode=`echo "$f" | grep -oP 'S[0-9]{2}E[0-9]{2}'`
echo "Processing $episode"
extension="${f##*.}"
filename="${f%.*}"
out="${filename}.mp4"
out="${out/EAC3/AAC}"
out="${out/AC3/AAC}"
echo "${f} --> ${out}"
ffmpeg -hide_banner -i "$f" -map 0:v -c:v copy -map 0:a -c:a copy -map 0:a:0 -c:a:0 aac -map 0:s -c:s mov_text -map_metadata 0 -movflags +faststart "$out" && \
rm "$f" && echo "Removed ${f}" && \
rename -v 's/EAC3/AAC/' *$episode* && \
rename -v 's/AC3/AAC/' *$episode*
#read -n 1 -p Continue?
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment