Яндекс.Метрика
qr-код - Как-то так стрелка - Как-то так
Leading Economist + ... пиктограмма - Как-то Так THIS     Economic literacy and web solutions
All on the topic: what, how and why. Actual things - in their own words.
Turn your quality WITH ME - BUTW !
If you don't like what you get, change what you give. © Carlos Castaneda     KAKTOTAK.BY - economic design and web development services (php/js - telegram, WordPress, Laravel, Vue, SEO)    Cooperation - on an ongoing basis    Be in the topic   -    SUBSCRIBE      My notes are ECONOMICS +  и  WEB Developer's Guide | php+js+seo   в Telegram   telegram  

Writing a parser or how to download all videos from YOUTUBE in one click with the file name linked to the URL name using PHP + JS

Writing a parser or how to download all videos from YOUTUBE in one click with the file name linked to the URL name using PHP + JS

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

writing a parser or how to download all videos from YOUTUBE in one click with the binding of the file name to the URL name using PHP + JS - 1

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).

writing a parser or how to download all videos from YOUTUBE in one click with the file name linked to the URL using PHP + JS - 2

Insert the 1st script into the console and press Enter.

writing a parser or how to download all videos from YOUTUBE in one click with the binding of the file name to the URL name using PHP + JS - 3

After completing the 1st script (when the entire page opens), insert the 2nd script into the console and press Enter.

writing a parser or how to download all videos from YOUTUBE in one click with the binding of the file name to the URL name using PHP + JS - 4

After the completed procedures, we copy all the issued code from the console and paste it into a regular Excel file.

writing a parser or how to download all videos from YOUTUBE in one click with the binding of the file name to the URL name using PHP + JS - 6

We have the following.

writing a parser or how to download all videos from YOUTUBE in one click with the binding of the file name to the URL name using PHP + JS - 7

The Excel file has a structure with the fields we need - the title of the video and the url of the video.

writing a parser or how to download all videos from YOUTUBE in one click with the binding of the file name to the URL name using PHP + JS - 8

Save xlsx to your computer under any name.

writing a parser or how to download all videos from YOUTUBE in one click with the file name linked to the URL name using PHP + JS - 9

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.

writing a parser or how to download all videos from YOUTUBE in one click with the file name linked to the URL name using PHP + JS - 12

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.

writing a parser or how to download all videos from YOUTUBE in one click with the binding of the file name to the URL name using PHP + JS - 10

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.

writing a parser or how to download all videos from YOUTUBE in one click with the binding of the file name to the URL name using PHP + JS - 11

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.

writing a parser or how to download all videos from YOUTUBE in one click with the binding of the file name to the URL name using PHP + JS - 13

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.

writing a parser or how to download all videos from YOUTUBE in one click with the binding of the file name to the URL name using PHP + JS - 14

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.

writing a parser or how to download all videos from YOUTUBE in one click with the binding of the file name to the URL name using PHP + JS - 15

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.

writing a parser or how to download all videos from YOUTUBE in one click with the file name linked to the URL name using PHP + JS - 16

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.

автор - Михаленко Р.
M R. Автор - kaktotak.by Specialization: financial and economic design - modeling of business, investment projects of the real sector, analysis and evaluation of efficiency, optimization of the management decision system.

A wide range of web-based competencies for solving business problems.

Subscribe to my telegram channel - My notes are ECONOMICS +
Там я ничего не втюхиваю и не навязываю. Просто делюсь полезной информацией по экономической грамотности. Повышайте своё качество вместе со мной: что, как и почему в экономике на простом языке. Понятия, алгоритмы, процессы, микро- и макроаспекты и многое другое. Может, будет интересно !

And also - WEB Developer's Guide | php+js+seo
Notes and native solutions to simple local tasks in PHP, JS. Something about Laravel, WordPress, Vue and SEO.

  Personal assistance in economic design and web development:

  • Financial and economic modeling, analysis, accounting, business planning
  • Comprehensive web development/project support on the web (php/js, seo – Laravel, WordPress, Vue, telegram, administration, content, advertising in Yandex Direct

  telegram или форма обратной связи

Administrator
394
0
Name
E-mail
Rating
Review

Currency Converter
RUB RUB-icon
USD USD-icon
EUR EUR-icon
CNY CNY-icon
BYN BYN-icon
UAH UAH-icon
KZT KZT-icon
SHORT- what is it about
ECONOMIC LITERACY
  Simple online solutions to problems of economics and finance  
I Want To Know Everything. Useful Tips