There is such a task:
Download all videos from the YOUTUBE server to your system at once (in one click), but not just, but with the binding of the names of the urls of these videos to the names of the downloaded files. And at the exit, you need to have on your side all the video files from a certain YOUTUBE channel/playlist + information on each of these files - its name and url.
For example, you have your own website/resource and videos are embedded on its pages not through direct links from your server (when files are stored on your hosting), but by integrating external urls through an iframe or other third-party service (fv player, etc.) into the code of your page.
That is, there is a dependence on the server where your videos are stored, and there is a need to download them all from there and re-upload them to your hosting (to broadcast them from your home), followed by renaming the existing urls in the database of your site to new ones, by linking links YOUTUBE = the name of the downloaded file.
Due to the possible blocking of the YOUTUBE service, for certain countries of the post-Soviet space, this parser (script) may be relevant for some.
Let's solve this problem using the server-side programming language php and some functionality js.
So, Let's go.
Let's say you need to download all the videos from the channel -https://www.youtube.com/@EnglishEasyPractice/videos
Go to the page of this channel.
We will need 2 scripts in js.
The 1st one will help us to open the entire page with all the videos through the browser console at the beginning, by automatically and gradually vertically scrolling the browser bar to its end (so that the entire DOM code is displayed on the page as the video is loaded).
2nd - it will directly help in the browser console itself to parse (output) the names of all urls, title and other auxiliary information contained in the code for each video.
Go to the developer console of our browser(Google Chome).
Insert the 1st script into the console and press Enter.
After completing the 1st script (when the entire page opens), insert the 2nd script into the console and press Enter.
After the completed procedures, we copy all the issued code from the console and paste it into a regular Excel file.
We have the following.
The Excel file has a structure with the fields we need - the title of the video and the url of the video.
Save xlsx to your computer under any name.
Now let's move on to writing a php script, which should solve our problem.
Everything will be implemented in two created php files:
- test_youtube.php
- video-downloader.php
File contents test_youtube.php
The contents of the file video-downloader.php
What does all this code describe
In test_youtube.php creating an html form, with which we will first upload our xlsx file to the server, and the "Download everything at once" button, clicking on which the entire list of urls paired and displayed on the same page will be downloaded.
That is, initially we will need to upload our xlsx file to the server, where we will work with it.
When uploading a file to the server, the script pre-validates it for security (name, extensions, download format), after which it parses and converts it into an array of such a plan using the connected external PHPExcel library.
We will connect the PHPExcel library via require_once 'phpexcel/Classes/PHPExcel.php '.
Because we have php 7.4 installed, and the more recent and supported PHPExcel library has been working since php 8.0, so let's not bother, but download a slightly outdated version from the repository archive and connect it via require_once, and not via composer. In this case, it does not matter.
Next, using the YOUTUBE API and the php library curl (for interacting with the API), as well as the foreach loop, we will iterate through all the urls of our array and generate a list on the page (all in the same file test_youtube.php ) necessary links for downloading (downloading) videos.
We will assign get parameter urln to each link, the value of which will name (assign a name) the corresponding file for the download. Let's call them how all the standard links of any youtube video are identified in the form of a prefix after the = sign.
After the links are generated and displayed on the page, clicking on any of them will send get with a request for our data to the 2nd script video-downloader.php , which is already being created directly using http-The header and the readfile function will read the file and write it to the output (download) buffer.
Since we want to download videos not one at a time (by clicking on each link), but with one click and all at once, we will write the js function download_everybody() in our source file(test_youtube.php), which will be linked to the event of clicking our "Download all at once" button.
This script will simply run through the list of all videos in the DOM of the page, with an interval of 5 seconds (in order to prevent possible blocking of both YOUTUBE and its hosting, due to the simultaneous sending of a large number of requests from one IP), clicking on each of them, thereby sending it to download.
As a result, all our files, as they are downloaded, will end up in the folder necessary for us, specified through the browser settings for downloading files by default.
In order for the browser to "not go to bed", it is advisable to divide the original xlsx file into several and upload them in several stages so as not to clog the system's RAM, as well as to protect yourself from possible blocking due to the large number of one-time requests to the server from the same IP. It is also necessary to take into account YOUTUBE's quota for using its API for these purposes using the issued key. How to get a key for the YOUTUBE API, there is enough information on the network.
As a result, with the help of this script, we can upload/download all files from any youtube channel/playlist, assigning them unique names (downloaded files), and thereby get the necessary binding of the url of the youtube video = the name of the downloaded file, which in the future when If necessary, it can simplify the task of renaming youtube links in the melon database of your site to new ones.