Search code examples
javascriptflashvideo-streaminghtml5-videoaudio-streaming

download ONLY audio from a streaming video?


I understand that I can make a range request to download a specific time range for a streaming web video.

I'm wondering now if it is possible to request only the MP3 or AAC audio stream from a video, so that on my server, I only download audio.

From what I've heard this is possible, but I've yet to find any legitimate solution or method for doing this on the web, so I don't really know where to start.


Solution

  • Assuming you are talking about streams over HTTP, this isn't really feasible. I really wouldn't even recommend trying it, unless you have a very specific need and a lot of time on your hands.

    Is it possible? Maybe. First off, you have to have an HTTP server that supports range requests. If it does, then you have to know something about the container format. For that, you have to start downloading the file and figure that out.

    Most containers interleave streams together. That is, you will have a chunk of bytes for the video, a chunk of bytes for the audio, and chunks for any other streams present. For demonstration purposes, suppose a video only has an audio stream and video stream, and each chunk contains 1 second worth of content. The interleaving looks like this:

    HEADERS A V A V A V A V A V A V A V ...

    HEADERS is everything the container has for setting up the stream. A is an audio stream chunk, and V is a video stream chunk.

    If you want to download just one stream, you have to be able to parse that container format yourself, download the container headers and any other data you need for the container, and then make range requests for all of the chunks of the particular stream you want. Depending on the interleaving, in the end, this may not be very efficient at all!

    As you are downloading your data, you then need to write it back to a file you can actually use, which means you will likely need to write some custom code for doing so, or trick FFMPEG into writing it for you.