Background
I recently wanted to download a podcast that was being served via a RTMP stream. RTMP (Real Time Messaging Protocol) was initially a proprietary protocol developed by Macromedia for streaming audio, video and data over the Internet, between a Flash player and a server. Macromedia is now owned by Adobe, which has released the specification of the protocol for public use.
Here’s how I was able to download the RTMP stream to a .flv file and convert it to a .mp3 file. Read on for the details.
Solution
step #1 – download the stream
I used the tool rtmpdump to accomplish this. Like so:
NOTE: the tool rtmpdump was available in my Distro’s repository
1 2 3 4 5 6 7 8 9 10 | # I was able to get the rtmp url from looking at the page's source! % rtmpdump -r rtmp://url/to/some/file.mp3 -o /path/to/file.flv RTMPDump v2.3 (c) 2010 Andrej Stepanchuk, Howard Chu, The Flvstreamer Team; license: GPL Connecting ... INFO: Connected... Starting download at: 0.000 kB 28358.553 kB / 3561.61 sec Download complete |
step #2 – convert the flv file to mp3
OK, so now you’ve got a local copy of the stream, file.flv. You can use ffmpeg to interrogate the file further and also to extract just the audio portion.
1 2 3 4 5 6 7 | % ffmpeg -i file.flv .... [flv @ 0x25f6670]max_analyze_duration reached [flv @ 0x25f6670]Estimating duration from bitrate, this may be inaccurate Input #0, flv, from 'file.flv': Duration: 00:59:21.61, start: 0.000000, bitrate: 64 kb/s Stream #0.0: Audio: mp3, 44100 Hz, 1 channels, s16, 64 kb/s |
From the above output we can see that the file.flv contains a single stream, just audio, and it’s in mp3 format, and it’s a single channel. To extract it to a proper mp3 file you can use ffmpeg again:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | % ffmpeg -i file.flv -vn -acodec copy file.mp3 .... [flv @ 0x22a6670]max_analyze_duration reached [flv @ 0x22a6670]Estimating duration from bitrate, this may be inaccurate Input #0, flv, from 'file.flv': Duration: 00:59:21.61, start: 0.000000, bitrate: 64 kb/s Stream #0.0: Audio: mp3, 44100 Hz, 1 channels, s16, 64 kb/s Output #0, mp3, to 'file.mp3': Metadata: TSSE : Lavf52.64.2 Stream #0.0: Audio: libmp3lame, 44100 Hz, 1 channels, 64 kb/s Stream mapping: Stream #0.0 -> #0.0 Press [q] to stop encoding size= 27826kB time=3561.66 bitrate= 64.0kbits/s video:0kB audio:27826kB global headers:0kB muxing overhead 0.000116% |
The above command will copy the audio stream into a file, file.mp3. You could also have extracted it to a wav file like so:
1 | % ffmpeg -i file.flv -vn -acodec pcm_s16le -ar 44100 -ac 2 file.wav |
This page was useful in determining how to convert the flv file to other formats.
References
links
- Real Time Messaging Protocol – wikipedia
- Rtmpdump – wikipedia
- Using ffmpeg to manipulate audio and video files – howto-pages.org
local copies
NOTE: For further details regarding my one-liner blog posts, check out my one-liner style guide primer.