Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.
/ convert-ytb-mp3 Public archive

A (bad) converter taking a Youtube link and turning it into .mp3 without the use of external google api

License

Notifications You must be signed in to change notification settings

Ex0D/convert-ytb-mp3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Youtube link to MP3 file converter (deprecated)

A (bad) converter taking a Youtube link and turning it into .mp3 without the use of external google api

How to use it (Web view) ?

  1. Copy this repo
git clone https://github.com/Ex0D/convert-ytb-mp3.git
  1. Installation with pnpm (you can use npm as well but it's better with pnpm)
pnpm install
  1. Execution (You can use PM2 for hosting, or just use node)
node server.js

or

pnpm run start #npm run start work too
  1. Go to you'r localhost page (port 3000 by default) and type the Youtube URL you want to download.

  2. Wait ~ 5 seconds

  3. Done ! Check your audio folder !

How does it work ?

Text explanation

Well, in itself it is not very complicated but let's go:

First of all, you have to know that to "receive" the video/audio stream, Youtube uses a kind of player that is virtualized from a "blob" protocol, so it is rather complicated to get this stream through this blob (see impossible)

However, to "inject" the audio and video data stream, Youtube uses requests as the video is playing.

So let's try to trace these requests in the "Network" tab in the Devtool (F12 of course)

It's normal to get lost or just lose time looking at each query one by one, so I found the queries corresponding to the video/audio stream that google servers import.

videoplayback_requests

All the queries framed in red are the ones we are interested in, especially the one with the response header "audio/webm".

Yes, because Youtube cuts audio and video into 2 distinct types of requests

video_response audio_response

Now how do we proceed? We just have to filter through this response header and download the file?

Not really, if you try to access the URL (the request url) you will quickly realize the problem: the audio / video is fragmented into several pieces of files (basically we have only a few seconds of audio / video).

Well NO, after a few minutes of searching, the file is right in front of you, intact, but something is "blocking" your browser from being able to play the file entirely.

And for that you have to look at the URL more closely: https://rr1---sn-5hne6ns6.googlevideo.com/videoplayback?expire=1678914640&...&range=629778949-631076654

Yes, you see it too: in the URL there is a "section" named "range" with several numbers delimited by a "-".

Ok, what if we try to edit the range of the audio / video ? (example : "range=0-9999999999999").

In short, YouTube does not load the entire video/audio file directly (which is logical) and therefore uses this "range" parameter to regulate traffic (the video/audio synchronization is totally managed by the youtube player) Let's go ! We have the whole file! Just have to download it !

Here is how to download the audio from a youtube link.

Packages :

  • Express - Web framework that allow me to render view & make my stuff (fastify is better, but for some reason, i use express (5.0 inc ? 👀))
  • puppeteer - Puppeteer is a Node.js library which provides a high-level API to control Chrome/Chromium over the DevTools Protocol. (Fighting fire with fire)
  • ejs - Embedded JavaScript templating (Contrary to Jade or pug, EJS does not touch too much the HTML structure and I find that very nice, in spite of the frontend)
  • body-parser - Parse incoming request bodies in a middleware before your handlers, available under the req.body property (Unfortunately, it is mandatory to use it)
  • express-session - (wtf ? see bellow)
  • connect-flash - The flash is a special area of the session used for storing messages. Messages are written to the flash and cleared after being displayed to the user. The flash is typically used in combination with redirects, ensuring that the message is available to the next page that is to be rendered. (that's why express-session is required) Unless the internet protocols change, you are in fact more powerful than a simple user on a browser, the browser is a gigantic debugger, it remains to know how to use it well.

On these beautiful philosophical words, have a good day if you go through this repo, whether you are from google, amazon or any other

o/

About

A (bad) converter taking a Youtube link and turning it into .mp3 without the use of external google api

Topics

Resources

License

Stars

Watchers

Forks