-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_download.py
80 lines (69 loc) · 2.48 KB
/
video_download.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import requests
import time
import os
import json, csv, sys
from pytube import YouTube
# pip install pytube
# pip install requests
headers = {
'User-Agent': "your_device's_user_agent",
'Referer': 'https://www.bilibili.com/'
}
cookies = {'your_cookie_name': 'your_cookie_value'}
# Bilibili video download func
def request_cid(bid):
http = "https://api.bilibili.com/x/player/pagelist?"
response = requests.get(http + "bvid=" + bid, headers=headers, cookies=cookies)
data = response.json()
if(data["code"] == 0 ):
return data["data"][0]["cid"]
return "error"
def request_url(bid):
cid = request_cid(bid)
if(cid != "error"):
http ="https://api.bilibili.com/x/player/playurl?"
response = requests.get(http + "bvid=" +bid+ "&cid="+str(cid)+"&qn=32", headers=headers, cookies=cookies)
data = response.json()
if(data["code"] == 0 ):
return data["data"]["durl"][0]["url"]
return "error"
def download_video(url, file_path):
response = requests.get(url, stream=True, headers=headers, cookies=cookies)
if response.status_code == 200:
with open(file_path, 'wb') as file:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
file.write(chunk)
print("Download complete.")
else:
print(f"Failed to download video. Status code: {response.status_code}")
def request_video(bid, path):
url = request_url(bid)
if(url != "error"):
download_video(url, path)
def download_video_func(video_ids, platform, data_folder):
dir = data_folder + "/" + str(video_id)
for video_id in video_ids:
if(platform == "Bilibili"):
if not os.path.exists(dir):
os.mkdir(dir)
path = dir + "/video.mp4"
if not os.path.exists(path):
request_video(video_id, path)
print(video_id + " Download successful")
time.sleep(1)
elif(platform == "YouTube"):
if not os.path.exists(dir):
yt = YouTube('http://youtube.com/watch?v=' + str(video_id))
try:
yt.streams.filter(progressive=True, file_extension='mp4')\
.order_by('resolution').desc().first().download(output_path = dir)
print(video_id + " Download successful")
time.sleep(1)
except Exception as e:
print(str(e))
if __name__ == "__main__":
video_ids = []
platform = "YouTube" # Bilibili
data_folder = "the_folder_store_videos"
download_video_func(video_ids, platform)