Home  >  Article  >  Backend Development  >  Using Python to implement video download function example code

Using Python to implement video download function example code

高洛峰
高洛峰Original
2017-03-16 09:34:562617browse

The short video business has been booming in the past year or two, and each video website has its own short video content. If there was a program that could download the latest videos released by popular users of major video websites, it would not only facilitate viewing, but also allow you to post copyright-free videos on personal social networking sites to increase your own popularity. How great would it be?

The short video business has been booming in the past year or two, and each video website has its own short video content. Wouldn't it be nice if there was such a program that could download the latest videos released by popular users of major video websites? Not only would it be convenient for you to watch, but you could also post copyright-free videos on personal social networking sites to increase your popularity. ?

parker is such a project (project address: https://github.com/LiuRoy/parker), which uses the celeryframework to regularly crawl user video lists and compile the latest released videos Distributed deployment can be easily implemented through you-get asynchronous download. Because the page layout and interface of each website are updated frequently, in order to ensure the high availability of the program, Statsd monitoring is specially added to facilitate timely detection of errors.

CodeArchitecture

Using Python to implement video download function example code

##Currently in parker Only downloads from Bilibili and Miaopai are implemented. As can be seen from the frame diagram, two asynchronous interfaces need to be implemented for each type of website: parsing the playback address of the published video from the user's video homepage, and downloading the video based on the playback address. Therefore, when adding a website type, you do not need to modify the original code. You only need to add a new parsing and downloading interface. I have not implemented the subsequent operations after the video download is completed. You can freely implement it according to your own needs.

When running, celery will regularly send the configured high-quality user list to the parsing interface of the corresponding website for asynchronous execution, filter out the latest video playback address, and hand it over to the corresponding download interface for asynchronous downloading. After completion, call subsequent operations asynchronously. Therefore, it is necessary to start a celery beat process to send scheduled tasks, and several celery asynchronous tasks to perform parsing and downloading operations. For relatively large videos, downloading will be quite time-consuming. It is recommended to reasonably allocate the number of asynchronous tasks according to the size of the task list.

Program operation

It has been verified that this program can run normally under ubuntu and mac. Because celery under local windows cannot It starts normally, so it has not been verified in

windows environment.

Dependent librariesInstallation##The python version is 3.5. After entering the project directory, execute:

pip install -r requirements.txt


Create database

TableBuild it in the database in advance Two tables (sql: https://github.com/LiuRoy/parker/blob/master/spider/models/tables.sql)

Parameter configuration logging.yaml, params.yaml, and sites.yaml under the config path correspond to log configuration, running parameter configuration, and popular user configuration respectively.

Log configurationIn debug mode, the log will be output directly to the standard output stream. In release mode, the log content will be output to a file. Therefore, the output log file needs to be configured.

Run configuration

    mode debug
  • Debug mode

    , in this mode the log points to the standard output , and there is no monitoring data; in release mode, the log is output to the specified file, and there is monitoring data.

  • broker_url corresponds to celery's BROKER_URL, which can be configured as
  • redis

    or rabbitmq

  • mysql

    _url Database address, two tables need to be built in advance

  • download_path Video download path
  • statsd_address Monitoring address
  • video_number_per_page How many video playback addresses are parsed from the user's video homepage each time? Because most users publish a small number of videos each time, it only needs to be set to a very small value. During the first run, a large number of long-standing videos will not be downloaded.

  • download_timeout Timeout for video download

Popular user configuration

parker will generate a list of celery beat schedulers based on this configuration.

  • name rule is -, parker will use this as scheduler task name

  • url user Publish video homepage

  • task Corresponding celery parsing asynchronous task

  • minute How many minutes to check the user video list

Start the task

Enter the project directory and execute the following command to start the celery worker


celery -A spider worker


Execute the following command to start the celery beat scheduled task


celery -A spider beat


Monitoring

Strongly use a docker image to configure the monitoring environment in one minute. After that, you only need to add the data of successful execution and abnormal execution, and you can easily monitor whether the program is running normally.

Using Python to implement video download function example code


The above is the detailed content of Using Python to implement video download function example code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn