Sunday, November 16, 2008

Extract mp3 from flv

Tonight I tried to rip the audio track from a flv file. First I wanted to convert it into ogg but as the audio track is a mp3 stream itself I decided just to rip the mp3 data into a file (same quality as in the video file. A higher samplerate would only increase filesize).

UPDATE: Today the audiostream is often aac (not mp3 anymore). That means, you have to use mp4 instead of mp3 with ffmpeg ... -f mp4 ... .mp4).

One of those commands will do the job:
mplayer -dumpaudio in-video.flv -dumpfile out-audio.mp3
ffmpeg -i input.flv -f mp3 -vn -acodec copy output.mp3

To convert all files (*.flv to *.mp3) in the directory use one of the following commands:
for i in *.flv; do mplayer -dumpaudio -dumpfile "${i/.flv}".mp3 "$i"; done
for i in *.flv; do ffmpeg -i "$i" -f mp3 -vn -acodec copy "${i/.flv}".mp3; done

#ffmpeg: -f mp3 => force mp3 format, -vn => novideo, -acodec copy => stream-copy
# ${i/.flv} is cutting the filename $i from ".flv"

UPDATE: As the files are not always in the same format and my portable music player couldn't play mp4 I decided to convert them to ogg.
ffmpeg -i input.flv/mp4 -vn -acodec vorbis output.ogg

This script will transform all flv files in your directory into mp3 files.

1. Create a new file e.g. with kate and name it flv2mp3.sh

#!/bin/bash
for i in *.flv
do
echo "$i"
# Alternative 1
# mplayer -dumpaudio -dumpfile "${i/.flv}".mp3 "$i"
#Alternative 2
ffmpeg -i "$i" -f mp3 -vn -acodec copy "${i/.flv}".mp3
done

2. Change the right of this file:
chmod +x flv2mp3.sh

3. Goto the directory and run the script