Skip to content

Instantly share code, notes, and snippets.

@tytydraco
Last active October 6, 2022 04:42
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 tytydraco/83fd7f0c0ac13415c8a150d1610a6cd5 to your computer and use it in GitHub Desktop.
Save tytydraco/83fd7f0c0ac13415c8a150d1610a6cd5 to your computer and use it in GitHub Desktop.
A bash script to extract audio from a container.
#!/usr/bin/env bash
trap 'echo Ignoring' INT
get_codec_name() {
ffprobe "$1" \
-v quiet \
-show_streams \
-select_streams a \
-show_entries stream=codec_name \
| grep codec_name \
| awk -F'=' '{ print $2 }' \
|| exit 1
}
ext_from_codec() {
case "$1" in
"aac")
echo "m4a"
;;
"vorbis")
echo "ogg"
;;
*)
echo "$1"
;;
esac
}
codec_name="$(get_codec_name "$1")"
ext="$(ext_from_codec "$codec_name")"
filename="$(basename -- "$1")"
filenameNoExt="${filename%.*}"
dir="$(dirname "$1")"
new="$dir/$filenameNoExt.$ext"
[[ -f "$new" ]] && exit 0
echo "Extracting audio for $1"
ffmpeg -hide_banner -v error -nostats -nostdin -i "$1" -c:a copy -x -o "$new" || exit 1
rm "$1"
@tytydraco
Copy link
Author

Designed to be used in https://github.com/tytydraco/meow_dart. Pass in like this:

meow_dart ... -c ./extract_audio

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment