This blog is only for my spare time to record articles , Publish to , Only for users to read , If there is any infringement , Please let me know , I'll delete it .
In recent days, , A little friend said he wanted to read an article about video download , see , take bilibili Here we go .
Because there is no deep right bilibili Conduct research , Lead to poor readability of the article , No joy, no spray. .
Although it's just a taste . But the goal of downloading video can be achieved .
About video clarity :
Method | describe |
---|---|
Not logged in | Captured video and audio url Parameters :mid=0 |
The login status | Captured video and audio url Parameters :mid=xxx( A string of numbers ) |
If it's not logged in , There's no clarity .
If you open a video when you're logged in , Choose HD , that !! Use your string mid The requested videos are all HD ( The video itself has no HD except ).
Let's talk about the whole idea here !
Don't talk much , Let's start with !!!
The video in the picture below is an example . In this paper, for Firefox browser , Convenient to show the effect of bag grabbing .
The first is to open the developer tool to grab the package . Here's the picture :
See the grab page The media classification , All are mp4 Data packets in format , The packet size is from 107 Bytes to 2.30MB Unequal . There is an interesting rule as follows :
In that case , Let's use the code to request a look .
After code execution , It generates a 1kb Of test.mp4 file , Obviously , Wrong opening
Why is it wrong ?? I think the file is too small , Modify the request header Range The value is 0-1024000, Run the code again .
This time a 1001kb Of mp4 file , And it can play !! But there's no sound in the video , confirmed 了 bilibili Video files are separated from audio and video .
It says , Most of the packets caught are two duplicates URL, Now ask for another URL have a look .
The code just will URL Made modifications , The request header has not changed .
( No audio in the picture , But there are voices ), such , So I finished this job .
Here we are. , The job of grabbing the bag has been finished , The origin of video and audio is also clear , That's the question , Such a long string URL Where did it come from ?
!!! Look at the source code .
puzzled , But forget the simplest way , View source code of webpage ( It took a long time to discover , The original source code contains !!!)
video Of URL:
audio Of URL:
Here we are. , The rest use regular matching source code inside video and audio Of URL It's not a problem !!!
The remaining questions :
ffmpeg Official website :https://ffmpeg.zeranoe.com/builds/
Click in to download the corresponding computer version .
Download the file and unzip it into the following figure :
And then bin The directory of this file is added to System environment variable that will do . Here's the picture :
Explain a little bit about what's used here ffmpeg The meaning of the command is :
Method | describe |
---|---|
-c copy | Copy all the streams |
-i | Input file |
-loglevel | Logging level used |
quiet | With the loglevel Back , No log output |
Interested partners can click ffmepeg Chinese document , Deepen understanding .
I also use subprocess modular , Use to generate subprocesses , And they can be piped into their input / Output / error , And get their return values .
import subprocess
def merge_video_and_audio(video_name):
"""
Audio and video merging function , utilize ffmpeg Merge audio and video
:param video_name: Pass in the title
:return:
"""
cmd = f'ffmpeg -i "{video_name}.m4s" -i "{video_name}.mp3" -c copy "{video_name}.mp4" -loglevel quiet'
subprocess.Popen(command, shell=True)
print(f'{video_name}.mp4 merger !!!')
What it looks like after executing the code :
ps: Other videos are used here as examples .
1. Download good audio and video :
2. Open the format factory :
3. Audio and video streaming :
Last , Wait for the mixed model to finish .
Above code , Download... Here .
Code self access : https://github.com/SunriseCai/spiderCode
If you guys are interested , Can improve the code . Some suggestions are as follows :
Friends, if you improve the code, remember to send me a copy !!!