, ,

ffmpeg – Extracting mp3 or AAC Audio from a FLV or MP4 Video

Extraction Script


#!/bin/bash
fullfilepath=$1
filename="${fullfilepath%.*}"
echo $filename
#determine file extension
extension=$(ffmpeg -i "$fullfilepath" 2>&1 | grep Audio|sed 's/.*Audio: \([a-z0-9]\+\).*/\1/ig')
echo $extension
audiofilename="$filename.$extension"
echo $audiofilename
ffmpeg -i "$fullfilepath" -vn -acodec copy "$audiofilename"
#ffmpeg -i "$fullfilepath" -vn -ab 160k -ac 2 -ar 44100 "$filename.mp3"

Extract Audio in Video as MP3


find . -name "*.flv" -type f -exec ffmpeg -n -i '{}' -ab 256k -ac 2 -ar 44100 -vn -copyts '{}.mp3' \;

 


# investigate video
ffmpeg -i video.mp4

# extract by copy aac codec
ffmpeg -i video.mp4 -vn -acodec copy audio.aac

# extract and convert audio to mp3
ffmpeg -i video.flv -ab 160k -ac 2 -ar 44100 -vn audio.mp3

-i indicates the input
-ab indicates the bit rate (in this example 160kb/sec)
-vn means no video ouput
-ac 2 means 2 channels
-ar 44100 indicates the sampling frequency.

ffmpeg -i video.flv -ab 256k -ac 2 -ar 44100 -vn audio.mp3

Extract mp3 audio from all video files in a directory


for f in "*.flv"; do ffmpeg -i "$f" -ab 256k -ac 2 -ar 44100 -vn "$f.mp3"; done

Test scripts

#extect with encodin
#ffmpeg -i a.mp4 -vn -acodec copy a.aac
#ffmpeg -i video.flv -ab 160k -ac 2 -ar 44100 -vn audio.mp3
# http://www.coolutils.com/formats/mp4

#http://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash
#filename_with_ext=$(basename "$fullfilepath")
#extension="${filename_with_ext##*.}"
#filename="${filename%.*}"
#filename="${fullfile##*/}"

#for f in *.flv *.mp4; do ffmpeg -i "$f" -ab 256k -ac 2 -ar 44100 -vn "$f.mp3"; done
#for f in *.flv *.mp4; do sh -x ./audio.sh "$f"; done


# extract with existing encodin
ffmpeg -i a.mp4 -vn -acodec copy a.aac

ffmpeg -i video.flv -ab 160k -ac 2 -ar 44100 -vn audio.mp3

a@a:$ ffmpeg -i a.flv > res.txt 2&1
a@a:$ grep Audio res.txt
Stream #0:1: Audio: aac, 44100 Hz, stereo, fltp, 131 kb/s
Stream #0:1: Audio: aac, 44100 Hz, stereo, fltp, 131 kb/s
a@a:~$ grep Audio res.txt

ffmpeg -i a.flv 2>&1 | grep Audio|sed 's/.*Audio: \([a-z0-9]\+\).*/\1/ig'

ffmpeg -i   SOURCE.mp4 -vn -acodec copy a.aac

#ffmpeg -i a.flv 2>&1  | grep Audio|sed 's/.*Audio: \([a-z0-9]\+\).*/\1/ig'

# ffmpeg print to stderr on default
ffmpeg -i a.flv 2>&1 | grep -i Duration
ffmpeg -i a.flv 2>&1 | grep -i Audio

References

1. http://linuxpoison.blogspot.de/2010/04/how-to-extract-audio-from-video-file.html
2. http://www.coolutils.com/formats/mp4
3. http://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash