Last active
December 19, 2022 17:23
-
-
Save Wikinaut/a0abb02c8ad635333ab30a67d0cf2921 to your computer and use it in GitHub Desktop.
Extract audio from video and strip all metadata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/bash | |
# 20221216 init | |
# published as https://berlin.social/@wikinaut/109541495757480127 | |
compressionquality="-q:a 7 -joint_stereo 1" | |
mono="-ac 1" | |
red="\e[1;31m" | |
yellow="\e[1;33m" | |
nocol="\e[0m" | |
if [ $# -lt 1 ]; then | |
echo "Extract audio from video and strip metadata" | |
echo | |
echo "Usage:" | |
echo "extract-audio-strip-metadata.sh infile.mp4" | |
echo | |
echo -e "1. Generate quality-controlled (${red}${compressionquality}${nocol}) stereo and mono audio-only versions: ${yellow}mp3, mono.mp3${nocol}" | |
echo -e "2. Strip meta data of video and audio versions: ${yellow}stripped.mp4, stripped.mp3, mono.stripped.mp3${nocol}" | |
exit | |
fi | |
# https://stackoverflow.com/questions/9913032/how-can-i-extract-audio-from-video-with-ffmpeg | |
# https://trac.ffmpeg.org/wiki/Encode/MP3 | |
# | |
# lame option Average kbit/s Bitrate range kbit/s ffmpeg option | |
# -b 320 320 320 CBR (non VBR) example -b:a 320k (NB this is 32KB/s, or its max) | |
# -V 0 245 220-260 -q:a 0 (NB this is VBR from 220 to 260 KB/s) | |
# -V 1 225 190-250 -q:a 1 | |
# -V 2 190 170-210 -q:a 2 | |
# -V 3 175 150-195 -q:a 3 | |
# -V 4 165 140-185 -q:a 4 | |
# -V 5 130 120-150 -q:a 5 | |
# -V 6 115 100-130 -q:a 6 | |
# -V 7 100 80-120 -q:a 7 | |
# -V 8 85 70-105 -q:a 8 | |
# -V 9 65 45-85 -q:a 9 | |
ext=${1#*.} | |
video=0 | |
if [ "${ext,,}" = "mp4" ] ; then | |
# echo ${1#*.} | |
video=1 | |
fi | |
stripmeta="-c copy -fflags +bitexact -flags:v +bitexact -flags:a +bitexact -write_xing 0 -id3v2_version 0" | |
# Create a meta-data stripped version of the input file | |
ffmpeg -i "$1" $stripmeta "${1%.*}.stripped.${1#*.}" | |
# Create an audio-only file of a certain quality | |
# and create a meta-data stripped version | |
ffmpeg -i "$1" $compressionquality -map a "${1%.*}.mp3" | |
ffmpeg -i "${1%.*}.mp3" $stripmeta "${1%.*}.stripped.mp3" | |
# Create mono version | |
# downmix both channels to avoid that any out of phase stereo signal will cancel out. | |
# The following filtergraph brings out of phase stereo in phase prior to downmixing: | |
# https://trac.ffmpeg.org/wiki/AudioChannelManipulation | |
downmix="-af asplit[a],aphasemeter=video=0,ametadata=select:key=lavfi.aphasemeter.phase:value=-0.005:function=less,pan=1c|c0=c0,aresample=async=1:first_pts=0,[a]amix" | |
ffmpeg -i "$1" $downmix $compressionquality $mono -map a "${1%.*}.mono.mp3" | |
ffmpeg -i "${1%.*}.mono.mp3" $stripmeta "${1%.*}.mono.stripped.mp3" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment