ytdl-org / youtube-dl

Command-line program to download videos from YouTube.com and other video sites
http://ytdl-org.github.io/youtube-dl/
The Unlicense
131.96k stars 10.01k forks source link

Download only Audio bytes #9754

Open WiS3 opened 8 years ago

WiS3 commented 8 years ago

I want to download from YouTube the best Audio Quality possible. Since the Audio Only files are at low bitrate (usually 128k), i have to download the whole Audio+Video (format 22 usually has the best audio), and then extract the audio.

But i don't want to download the entire video file to only get the audio.

I know it is possible to download only Audio bytes by parsing the format header and skip all video frames to save bandwidth.

For example 4K Video Downloader does this with YouTube to get the best Audio quality from a video. It parses the MP4 header, and download only the audio bytes.

it is possible to implement this functionality?

yan12125 commented 8 years ago

I know it is possible to download only Audio bytes by parsing the format header and skip all video frames to save bandwidth.

Is there an existing command line tool for this? I don't think MP4 parsing is something trivial.

yan12125 commented 8 years ago

Related: #9302

WiS3 commented 8 years ago

I don't know if there is, anyway by searching a bit i found a project that seems to do that. But it's for Android and it's written in Java. Maybe someone can port it.

https://github.com/feribg/audiogetter/blob/master/audiogetter/src/main/java/com/github/feribg/audiogetter/tasks/download/VideoTask.java from http://stackoverflow.com/questions/15241076/download-only-audio-from-a-youtube-video

yan12125 commented 8 years ago

On https://github.com/feribg/audiogetter it says:

This is a work in progress, currently it doesn't compile or run

And I indeed got non-trivial compile errors. Do you have a working example?

WiS3 commented 8 years ago

https://github.com/gpac/gpac

Just tried and it is working. But it doesn't support files over HTTP. I tried with MP4 downloaded by youtube-dl

mp4box -single 2 "input.mp4" -out "output.m4a"

kidol commented 8 years ago

@WiS3 From what I can see in network sniffer, the 4K Video Downloader simply downloads the m4a file which you can get with youtube-dl too. It just uses multiple threads (1 range-request for each thread). I tested with a 4k video.

Regarding the idea:

I have downloaded a 10 minute mp4 from YT. When I open it in an app called Mp4Explorer to show the mp4 moov structure, it lists 1.269 "chunk box offsets" for the audio track.

Based on the audiogetter source code

long[] chunkOffsets = chunkOffsetBox.getChunkOffsets();
...
for (int i = 0; i < chunkOffsets.length; i++) {
   ...
   chunks.add(new Chunk(i, chunkStartOffset, chunkEndOffset, offsetInFile));
}

it looks like one has to do a http range-request for every chunk to download the audio data. That's about 2 range-requests for 1 second of audio (based on my test file with 1.269 chunks).

That would be overkill if correct and would probably get ip banned pretty fast.