Sometimes I like to take a television news show and listen to just the audio while exercising or mowing the yard. I have a card in my PC that can capture non-encrypted cable or over the air (OTA) transmissions. Here is an example recorded program:
$ mplayer News.mpg ... VIDEO MPEG2(pid=33) AUDIO A52(pid=34) NO SUBS (yet)! PROGRAM N. 0 VIDEO: MPEG2 1920x1080 (aspect 3) 29.970 fps 90000.0 kbps (11250.0 kbyte/s) ... AUDIO: 48000 Hz, 2 ch, s16le, 384.0 kbit/25.00% (ratio: 48000->192000) Selected audio codec: [ffac3] afm: ffmpeg (FFmpeg AC-3)
So how do I strip the audio and convert it to an MP3 file? I wrote this script to do this very thing:
#!/bin/bash FIFO=audio.$$ mkfifo $FIFO mplayer -dumpaudio -dumpfile $FIFO $1 & ffmpeg -i $FIFO -acodec libmp3lame -ar 48000 -ab 128k -ac 2 $(basename $1 .mpg).mp3 rm $FIFO
The trick is to have an OS that has a mplayer and ffmpeg program that understands the codecs of the input files. That, however, is for a different post.
Notice the use of the FIFO file. If you are unaccustomed to using FIFO files, then may I suggest you spend some time with them. They are pretty handy. In a nutshell, the mplayer command dumps the audio part of the media file to $FIFO, a first in first out pipe. If there is no program reading the $FIFO pipe, then mplayer simply waits. Not until ffmpeg starts reading the $FIFO (via the -i option), does mplayer start playing.