Improve audio extraction

This commit is contained in:
2023-08-05 17:45:37 -04:00
parent 62fa34ca20
commit fb2773ca19
6 changed files with 82 additions and 76 deletions

View File

@@ -1,5 +1,10 @@
#!/usr/bin/env bash
# Extracts audio from a video. It expects you to know what audio codecs the video container has, e.g.
# it's an mp4 video with aac and m4a audio. Just set the format to the appropriate extension.
# If you want to convert to a different format or you want to change the bit rate, channels, trim the audio, etc.
# then use extract-audio-from-video-and-transcode
if which tput >/dev/null 2>&1; then
ncolors=$(tput colors)
fi
@@ -23,13 +28,11 @@ else
NORMAL=""
fi
transcode="$1"
filename=$(basename -- "$2")
format="$3"
bitrate="$4"
filename=$(basename -- "$1")
format="$2"
if [[ $1 == "" || $2 == "" || $3 == "" ]]; then
printf "${BOLD}${RED}Usage: $0 <transcode? 0|1 (needed when container doesn't contain format)> <filename> <format (mp3, m4a, aac, etc)> <optional: bitrate. Uses 64k when not specified, 0 = variable (e.g. 0, 64, 128, etc)>${NORMAL}\n"
if [[ $1 == "" || $2 == "" ]]; then
printf "${BOLD}${RED}Usage: extract-audio-from-video <filename> <format (mp3, m4a, aac, etc)>${NORMAL}\n"
exit 1
fi
@@ -37,19 +40,9 @@ extension="${filename##*.}"
filename="${filename%.*}"
output_name="$filename.$format"
if [[ $bitrate == "" ]]; then
bitrate="64"
fi
printf "\n${YELLOW}${BOLD}Extracting audio from $filename.$extension | output: $output_name${NORMAL}\n"
printf "\n${YELLOW}${BOLD}Extracting audio from $filename.$extension | bitrate: ${bitrate}k | output: $output_name${NORMAL}\n"
if [[ $transcode == "1" ]]; then
# Transcode audio
ffmpeg -i "$filename.$extension" -b:a ${bitrate}k -ac 2 -ar 44100 -map a "$output_name"
else
# Grab the audio stream from the video.
ffmpeg -i "$filename.$extension" -vn -acodec copy "$output_name"
fi
ffmpeg -y -stats -loglevel level+error -i "$filename.$extension" -vn -acodec copy "$output_name"
printf "\n${GREEN}${BOLD}Done extracting audio from $filename.$extension | output name '$output_name'${NORMAL}\n\n"