December 01, 2020
If you want to upload a video to youtube programmatically, you have to use the official YouTube API. But theres a problem with the given quota you are able to upload 3 videos a day, otherwise you will need request more quota from YouTube and that is not an easy process.
You only can upload 3 videos a day through the API, but you can upload videos from the browser and the upload limit on the browser 50 videos a day. So we just need to reverse engineer the YouTube API and create a way to upload videos from the backend. Well thats easier said than done, a way to do this is to launch a puppeteer instance and simulate the mouse clicks and key presses to upload the videos. That sounds easy but there are alot of problems. Google detects that you are using puppeteer and doesn't allow you to sign in or upload so you need to use the puppeteer-extra and puppeteer stealth plugin to bypass that. After that it's possible to make a script that will guide the puppeteer chrome instance to upload a video. But there is a big downside chrome uses a lot of memory and cpu resources not to mention time. A more performant solution but a lot harder one is to intercept the HTTP requests the browser is making to the unofficial YouTube API and just make in the backend. Now the hardest part reverse engineering the api the youtube studio javascript bundle. There are a lot of cookies that you need to send to make it work its not like there is a one cookie named sid that contains a session id not like that there is 5+ cookies that are all required to make the upload work. You also need to reverse engineer the functions that make random ids from youtube javascript bundle and it's not easy looking through 20 000 lines of minified code. So after all this work I have a solution that works but isn't complete. You can upload videos from your backend just by sending the HTTP requests that look like they are coming from the browser. But can only do this for a week, why? Look at section bellow.
Without this token everything fails and you can't upload videos. The token is a string that looks like a random base64 encoded string. The problem is that currently you need to get it manually from the browser then you are able to upload videos. For a week then it expires and you need to get it again. I've found the exact function in the youtube studio javascript bundle that creates the token but I haven't been able to replicate the function in the backend. There isn't an easy way to replicate a big generator function to the backend. It certainly is possible but really hard the only solution i can think of getting the token is converting the youtube studio javascript bundle into an AST(Abstract syntax tree) and then somehow extract the function and all the classes that it uses and references.
This project was created on 4 March 2021 and is not completely finished.
You can find the source code by scrolling up and clicking the GitHub icon or clicking here