Download ONLY audio from a youtube video

46,076

Solution 1

After a lot of research I found out that this is not possible and developed an alternative approach:

  1. Download the mp4 header
  2. Parse the header and get the locations of the audio bytes
  3. Download the audio bytes with http range requests and offsets
  4. Assemble the audio bytes and wrap them in a simple ADTS container to produce a playing m4a file

That way only bandwidth for the audio bytes is used. If you find a better approach of doing it please let me know.

For a sample Android APP and implementation check out:

https://github.com/feribg/audiogetter/blob/master/audiogetter/src/main/java/com/github/feribg/audiogetter/tasks/download/VideoTask.java

Solution 2

FFmpeg is capable of accepting an URL as input. If the URL is seekable, then FFmpeg could theoretically skip all the video frames, and thus it would need to download only the data for the audio stream.

Try using

ffmpeg -i http://myvideo.avi out.mp3

and see if it takes less bandwidth.

Share:
46,076
Feras
Author by

Feras

hate that part :D

Updated on June 11, 2020

Comments

  • Feras
    Feras about 4 years

    I know that there are a million ways to download a video from youtube and then convert it to audio or do further processing on it. But recently I was surprised to see an app called YoutubeToMp3 on mac actually showing "Skipping X mb of video" and supposedly only downloading the audio from the video, without the need to use bandwith to download the entire video and then convert it. I was wondering if this is actually correct and possible at all because I cant find any way to do that. Do you have any ideas ?

    EDIT: After some tests here is some additional information on the topic. The video which I tried to get the audio from is just a sample mp4 file from the internet:

    http://download.wavetlan.com/SVV/Media/HTTP/MP4/ConvertedFiles/MediaCoder/MediaCoder_test6_1m9s_XVID_VBR_306kbps_320x240_25fps_MPEG1Layer3_CBR_320kbps_Stereo_44100Hz.mp4

    I tried

    ffmpeg -i "input" out.mp3

    ffmpeg -i "input" -vn out.mp3

    ffmpeg -i “input” -vn -ac 2 -ar 44100 -ab 320k -f mp3 output.mp3

    ffmpeg -i “input” -vn -acodec copy output.mp3

    Unfortunately non of these commands seems to be using less bandwith. They all download the entire video. Now that you have the video can you confirm if there is actually a command that downloads only the audio stream from it and lowers the bandwith usage? Thanks!

  • Feras
    Feras over 11 years
    Starts working and freezes at: libavutil 51. 73.101 / 51. 73.101 libavcodec 54. 59.100 / 54. 59.100 libavformat 54. 29.104 / 54. 29.104 libavdevice 54. 2.101 / 54. 2.101 libavfilter 3. 17.100 / 3. 17.100 libswscale 2. 1.101 / 2. 1.101 libswresample 0. 15.100 / 0. 15.100 libpostproc 52. 0.100 / 52. 0.100
  • sashoalm
    sashoalm over 11 years
    Well that is the way to do it using FFmpeg at least. You asked how do you convert only the audio. That's how. It's supposed to be working, but there could be bugs, of course. Try filing a bug in their mailing list or debugging it yourself.
  • slhck
    slhck over 11 years
    This will never work with YouTube. FFmpeg expects an actual file as input, not a YouTube URL. That's not what it's meant to parse. If the link points to a valid video stream or file, then it should work, but YouTube embeds the video as Flash, so there's no way for FFmpeg to parse that unless they added explicit support for YouTube. Tools such as youtube-dl do a much better job at this.
  • sashoalm
    sashoalm over 11 years
    He'll need to parse the HTML to get to the actual content URL of course, but there are plenty of free and open-source tools that does this - youtube-dl.py for example. He could rewrite them to just print out the content URL instead of downloading it. Besides, this was just tangential to his question, he was asking if it's possible to save bandwidth by downloading only the audiostream of a big video file, he gave YouTubeToMp3 only as an example of a tool that does it.
  • Jon Nordby
    Jon Nordby over 3 years
    Anyone know if youtube-dl, or any other tool does this properly now?