search
HomeBackend DevelopmentPython TutorialUsing Python to implement video download function example code

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
What is Python Switch Statement?What is Python Switch Statement?Apr 30, 2025 pm 02:08 PM

The article discusses Python's new "match" statement introduced in version 3.10, which serves as an equivalent to switch statements in other languages. It enhances code readability and offers performance benefits over traditional if-elif-el

What are Exception Groups in Python?What are Exception Groups in Python?Apr 30, 2025 pm 02:07 PM

Exception Groups in Python 3.11 allow handling multiple exceptions simultaneously, improving error management in concurrent scenarios and complex operations.

What are Function Annotations in Python?What are Function Annotations in Python?Apr 30, 2025 pm 02:06 PM

Function annotations in Python add metadata to functions for type checking, documentation, and IDE support. They enhance code readability, maintenance, and are crucial in API development, data science, and library creation.

What are unit tests in Python?What are unit tests in Python?Apr 30, 2025 pm 02:05 PM

The article discusses unit tests in Python, their benefits, and how to write them effectively. It highlights tools like unittest and pytest for testing.

What are Access Specifiers in Python?What are Access Specifiers in Python?Apr 30, 2025 pm 02:03 PM

Article discusses access specifiers in Python, which use naming conventions to indicate visibility of class members, rather than strict enforcement.

What is __init__() in Python and how does self play a role in it?What is __init__() in Python and how does self play a role in it?Apr 30, 2025 pm 02:02 PM

Article discusses Python's \_\_init\_\_() method and self's role in initializing object attributes. Other class methods and inheritance's impact on \_\_init\_\_() are also covered.

What is the difference between @classmethod, @staticmethod and instance methods in Python?What is the difference between @classmethod, @staticmethod and instance methods in Python?Apr 30, 2025 pm 02:01 PM

The article discusses the differences between @classmethod, @staticmethod, and instance methods in Python, detailing their properties, use cases, and benefits. It explains how to choose the right method type based on the required functionality and da

How do you append elements to a Python array?How do you append elements to a Python array?Apr 30, 2025 am 12:19 AM

InPython,youappendelementstoalistusingtheappend()method.1)Useappend()forsingleelements:my_list.append(4).2)Useextend()or =formultipleelements:my_list.extend(another_list)ormy_list =[4,5,6].3)Useinsert()forspecificpositions:my_list.insert(1,5).Beaware

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool