This trick of ten years of Python's power and high-quality video download means, I don't know if you can stand it

Brothers, Happy April Fools' Day!

Did you get molested today?


After get off work, I was bored and browsed videos everywhere, and finally came to this place. As expected of it, the quality is excellent! So I'm going to use Python to download all the JK sister papers!

It's not that I don't want to talk about it, I can't pass it, but you have to believe me, it's really good!

It's better to act than heartbeat. Let's start the whole thing directly. When we're done, call my cousin to watch it together~

cousin:

Life is too short - how to do it without python

environment module

development environment

Here we install Python3, 3.6-3.8 is recommended, and the editor is still pycharm, but you can download pycharm 2021. The new version comes with a Chinese plug-in, which can be localized by direct installation.

It's good news for those who don't speak English!

Modules
The two third-party modules used here need to be installed by pip

requests   # 数据请求模块
tqdm      #进度条配置

If there is no software, no software will be installed, no modules will be installed, an error will be reported, etc., you can see the previous articles on the top of my homepage.

Or scan the QR code on the left side of the homepage

Let me briefly show you a downloaded video
cousin: Bah, just one? That's all? Can you put more!

Implementation steps

The steps of the worm have been described so many times, I don’t know if you remember it~

First, let's open the landing page and search for the video we like

As for how to enter this website, listen to me slowly~

To declare, I only say how to download, as for how to enter this website, this is not something I can teach, everyone Baidu, I am a little panic~


emmm ~

Let's not talk about this, let's go back to searching for our favorite videos, such as

cough , don't care what I search, this is not the point!

Then let's take this video as an example, let's still be a bit plain.
Let's click on the video, then press F12 to open the developer tools, then refresh the web page to find our data package content.

After the refresh, it will slowly load the data package. We can find a lot of content in it. I will not write it in detail. You can watch my video. The detailed steps are recorded.

I heard that the video of Miss JK on the largest video website on earth is of high quality, let's download it with python and see!

Why don't you write it? It's mainly because you write too much. People don't like to read it. They just like to read the code. Harm~

Code display

import module

These are all the modules that need to be used. The ones that need to be installed are all built-in modules and do not need to be installed.

import requests   # 数据请求模块
import re     # 正则表达式模块
import json  # 数据类型处理模块
from tqdm import tqdm   #进度条配置
import os    # 处理文件和目录

send request to get data

headers = {
    
    
    'cookie': 'VISITOR_INFO1_LIVE=9qZVrzB27uI; PREF=f4=4000000&tz=Asia.Shanghai; _ga=GA1.2.621834420.1648121145; _gcl_au=1.1.1853038046.1648121145; NID=511=Zc1APdmEbCD-iqVNVgI_vD_0S3LVI3XSfl-wUZEvvMU2MLePFKsQCaKUlUtchHSg-kWEVMGOhWUbxpQMwHeIuLjhxaslwniMh1OsjVfmOeTfhpwcRYpMgqpZtNQ7qQApY21xEObCvIez6DCMbjRhRQ5P7siOD3X87QX0CFyUxmY; OTZ=6430350_24_24__24_; GPS=1; YSC=0E115KqM_-I; GOOGLE_ABUSE_EXEMPTION=ID=d02004902c3d0f4d:TM=1648620854:C=r:IP=47.57.243.77-:S=YmZXPW7dxbu83bDuauEpXpE; CONSISTENCY=AGDxDeNysJ2boEmzRP4v6cwgg4NsdN4-FYQKHCGhA0AeW1QjFIU1Ejq1j8l6lwAc6c-pYTJiSaQItZ1M6QeI1pQ3wictnWXTOZ6_y8EKlt0Y_JdakwW6srR39-NLuPgSgXrXwtS0XTUGXpdnt4k3JjQ',
    'referer': 'https://www.youtube.com/results?search_query=jk%E7%BE%8E%E5%A5%B3',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36'
}
url = 'https:///watch?v=ImoXcSpR_io'
# 网站自己补全一下(提示:地球上最大的视频网站,不知道的百度)
response = requests.get(url=url, headers=headers)
print(response.text)

match json data

json_str = re.findall('var ytInitialPlayerResponse = (.*?);var', response.text)[0]

Convert json string to dictionary

json_data = json.loads(json_str)

Extract video link

video_url = json_data['streamingData']['adaptiveFormats'][0]['url']

Extract audio link

audio_url = json_data['streamingData']['adaptiveFormats'][-2]['url']

extract title

title = json_data['videoDetails']['title']	

Replace spaces in titles

title = title.replace(' ', '')

Replace illegal characters in the title

title = re.sub(r'[\/:|?*"<>]', '', title)
print(video_url)
print(audio_url)
print(title)

Send a request to a video link

video = requests.get(video_url, stream=True)

get video size

file_size = int(video.headers.get('Content-Length'))

Initialize progress bar size

video_pbar = tqdm(total=file_size)

start saving video

with open(f'{
      
      title}.mp4', mode='wb') as f:
    # 把视频分成 1024 * 1024 * 2 为等分的大小 进行遍历
    for video_chunk in video.iter_content(1024*1024*2):
        # 写入数据
        f.write(video_chunk)
        # 更新进度条
        video_pbar.set_description(f'正在下载{
      
      title}视频中......')
        # 更新进度条长度
        video_pbar.update(1024*1024*2)
    # 下载完毕
    video_pbar.set_description('下载完成!')
    # 关闭进度条
    video_pbar.close()

audio analogy

audio = requests.get(audio_url, stream=True)
file_size = int(audio.headers.get('Content-Length'))
audio_pbar = tqdm(total=file_size)
with open(f'{
      
      title}.mp3', mode='wb') as f:
    for audio_chunk in audio.iter_content(1024*1024*2):
        f.write(audio_chunk)
        audio_pbar.set_description(f'正在下载{
      
      title}音频中......')
        audio_pbar.update(1024*1024*2)
    audio_pbar.set_description('下载完成!')
    audio_pbar.close()

Merge with FFmpeg

def merge(title):
    ffmpeg = r'D:\Download\ffmpeg\bin\ffmpeg.exe -i ' + title + '.mp4 -i ' + title + '.mp3 -acodec copy -vcodec copy ' + title + '-out.mp4'
    os.popen(ffmpeg)

merge(title)

The effect is so, so, so,

brothers, try it now!
Remember to like and comment, love you!
insert image description here

Guess you like

Origin blog.csdn.net/fei347795790/article/details/123907060