Skip to content

Instantly share code, notes, and snippets.

@riyad
Last active November 17, 2018 22:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riyad/503dd97b8276905b53fe57b10a842abc to your computer and use it in GitHub Desktop.
Save riyad/503dd97b8276905b53fe57b10a842abc to your computer and use it in GitHub Desktop.
Extract audio from any media file ffmpeg can play into a new one (without converting or reencoding!)
#!/bin/sh
#
# Author: Riyad Preukschas <riyad@informatik.uni-bremen.de>
# License: Mozilla Public License 2.0
#
# Extract audio from any file ffmpeg can play (without converting or reencoding!).
if [[ $# -ne 3 ]]; then
echo "Error: wrong number of arguments"
echo "Usage: $0 -f <format> <input_file>"
echo " will extract (without converting or reencoding!) the audio from"
echo " <input_file> to a new file in the specified <format>."
echo ""
echo "OPTIONS"
echo " -f <format>"
echo " specify the format of the output file (e.g. mp3, ogg, opus, m4a)."
echo " NOTE: not all input and output formats are compatible."
exit 1
fi
if [[ "$1" == "-f" ]]; then
# $@ == -f format input_file
readonly format="$2"
readonly input_file="$3"
else
# $@ == input_file -f format
readonly format="$3"
readonly input_file="$1"
fi
readonly output_file="${input_file}.${format}"
# if the input file doesn't exists ffmpeg will complain anyway
# if the output file exists ffmpeg will ask whether it should be overwritten
ffmpeg -i "${input_file}" -vn -c:a copy "${output_file}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment