Author Topic: Command-line way to convert video to audio; great for portable audio players  (Read 1011 times)

flan_suse

  • Guest
You have a video file. Maybe you downloaded it from YouTube or had it sitting on your drive. This video might contain catchy music or an interesting philosophical interview. The problem is, your portable audio player only supports audio, and the file size is larger than it should be, since it contains a video track that you are not using. Here's a tip on extracting and converting the audio track to a compatible format for your device from within the command-line.

What you will need:
- a video file (.avi, .mp4, .flv, etc)
- ffmpeg
- fingers ***

First, make sure the package ffmpeg is installed. Notice that I am also installing libffmpeg, which is required, but not automatically downloaded with ffmpeg as a dependency.
Code: [Select]
su -c "apt-get update"
su -c "apt-get install ffmpeg libffmpeg"

Next, whip up a terminal, and change into the directory that contains the video file.
Code: [Select]
cd ~/Videos
Finally, extract and convert the audio track from the video file, using ffmpeg.

Here is an example to convert it to an Ogg Vorbis audio file.
Code: [Select]
ffmpeg -i interview.avi -vn -acodec libvorbis -aq 50 interview.ogg
Here is an example to convert it to an MP3 audio file.
Code: [Select]
ffmpeg -i interview.avi -vn -acodec libmp3lame -ab 128k interview.mp3
Here is an example to simply extract the audio, without any recompression, assuming that the audio track is in MP3 format.
Code: [Select]
ffmpeg -i interview.avi -vn -acodec copy interview.mp3
- The -i option points ffmpeg to an input file, in this case it is interview.avi.
- The -vn option tells ffmpeg to ignore the video track. We only want the audio.
- The -acodec option tells ffmpeg which audio encoder to use, such as libvorbis or libmp3lame. For direct extraction, use copy as the "audio codec".
- For the Ogg Vorbis example, I used the quality option. In this case -aq 50 is the equivalent of -q5 in the Vorbis specifications, which is roughly 160 kbps. However, with Vorbis you may end up with a bitrate substantially lower than what you were aiming for. MP3 is more bitrate-oriented, whereas Vorbis is more quality-oriented. For example, an audio file encoded with Voris today with a quality setting of -q5 will have the same quality as an audio file encoded with Vorbis with -q5 six years from now, even though the latter file may be smaller in size. You can play around with values from -aq 0 to -aq 100. The chart on the right hand side of the Wikipedia page can come in handy: http://en.wikipedia.org/wiki/Vorbis#Technical_details
- For the MP3 example, I used the constant bitrate option. In this case -ab 128k yields an MP3 audio file with a bitrate of 128 kbps.

To view a list of supported codecs, use the following command, which will print out a long list. Anything with a captial "A" and "E" is an Audio Encoder, such as libvorbis and libmp3lame.
Code: [Select]
ffmpeg -codecs
Here's a sample of the list.
Code: [Select]
  EA    libfaac         libfaac AAC (Advanced Audio Codec)
  EA    libmp3lame      libmp3lame MP3 (MPEG audio layer 3)
  EA    libvorbis       libvorbis Vorbis

*** Fingers are optional. You can always type with your tongue or nose.
« Last Edit: May 23, 2010, 11:39:17 AM by flan_suse »

Offline coonhunter

  • Full Member
  • ***
  • Posts: 202
Another option:

mplayer -dumpaudio my_video_file.avi -dumpfile extracted_audio.mp3

This technique can be used to extract audio from AVI, MPG and FLV files.