-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathnew_downloader.py
More file actions
230 lines (188 loc) · 6.44 KB
/
Copy pathnew_downloader.py
File metadata and controls
230 lines (188 loc) · 6.44 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import json
import os
import re
import shutil
import sys
import time
import base64
import platform
import requests
import yt_dlp
from PIL import Image
import title_unsearch
OWNER_NAME = "盛佳冉"
REMOVE_FILE = True # 是否删除投稿后的视频文件
LineN = "qn" # 线路 cos bda2 qn ws kodo
def get_double(s):
return '"' + s + '"'
def cover_webp_to_jpg(webp_path, jpg_path):
"""
将webp格式的图片转换为jpg格式的图片
:param webp_path: webp格式的图片路径
:param jpg_path: jpg格式的图片路径
:return: None
"""
im = Image.open(webp_path).convert("RGB")
im.save(jpg_path, "jpeg")
im.close()
def download(youtube_url, folder_name):
ydl_opts = {
# outtmpl 格式化下载后的文件名,避免默认文件名太长无法保存
"outtmpl": "./videos/"
+ str(folder_name)
+ "/%(id)s.mp4"
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([youtube_url])
def get_info(url):
ydl_opts = {
#'proxy': 'http://127.0.0.1:10809',
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=False)
return info
def getVideoPath(id_):
path = "./videos/" + str(id_)
for root, dirs, files in os.walk(path):
for file in files:
if file.find(id_) != -1:
return os.path.join(root, file)
def download_image(url, id_):
r = requests.get(url, stream=True)
f = open("./videos/" + str(id_) + "/cover.webp", "wb")
# chunk是指定每次写入的大小,每次只写了100kb
for chunk in r.iter_content(chunk_size=102400):
if chunk:
f.write(chunk)
def judge_chs(title):
for i in title:
if "\u4e00" <= i <= "\u9fa5":
return True
return False
def get_base64(string):
return str(base64.b64encode(string.encode("utf-8")).decode("utf-8"))
def get_base64_twice(string):
i = 0
while i < 2:
string = get_base64(string)
i += 1
return string
def get_chs_title(title):
while True:
publish_title = get_base64(title)
if len(publish_title) > 80:
title = title[:-1]
continue
else:
return publish_title
def get_chs_title_twice(title):
i = 0
while i < 2:
title = get_chs_title(title)
i += 1
return title
def cut_tags(tags):
i = 0
while len(tags) > i:
if len(tags[i]) > 20:
tags[i] = tags[i][:20]
i += 1
return tags
def main(vUrl, TID, plain_title=True):
info = get_info(vUrl)
title = info["title"]
dynamic_title = title
author = info["uploader"]
id_ = info["id"]
description = info["description"]
tags = info["tags"]
cover = info["thumbnail"]
tags.append(author)
tags.append(OWNER_NAME)
# init youtube video info
# Ensure videos directory exists
videos_dir = "videos"
if not os.path.exists(videos_dir):
os.mkdir(videos_dir)
# Create sub-directory for the specific video
sub_dir = os.path.join(videos_dir, str(id_))
try:
os.mkdir(sub_dir)
except FileExistsError:
shutil.rmtree(sub_dir)
os.mkdir(sub_dir)
download(vUrl, id_)
download_image(cover, id_)
cover_webp_to_jpg(os.path.join(sub_dir, "cover.webp"), os.path.join(sub_dir, "cover.jpg"))
# if plain_title:
# if not judge_chs(title): # 不包含中文
# title = title_unsearch.plain_title(title)
# else:
# title = get_chs_title_twice(title)
# title=re.sub(u"([^\u4e00-\u9fa5\u0030-\u0039\u0041-\u005a\u0061-\u007a\u3040-\u31FF\uFF00-\uFFA0\u0020\u3000])", '', title)
if len(title) > 80:
title = title[:80]
if len(description) > 250:
description = description[:250]
if len(tags) > 10:
tags = tags[:10]
tags = cut_tags(tags)
strTags = ",".join(tags)
videoPath = getVideoPath(id_)
if plain_title:
vUrl = "youtube.com"
# description = "-"
# Ensure paths are correct
videoPath = os.path.normpath(videoPath) # 规范化视频文件路径
coverPath = os.path.normpath(os.path.join(sub_dir, "cover.jpg")) # 规范化封面路径
# Determine command based on OS
if platform.system() == "Windows":
cmd_prefix = os.path.abspath(r".\biliup.exe") # 使用绝对路径
else:
cmd_prefix = "./biliup"
CMD = (
cmd_prefix
+ " upload "
+ videoPath
+ " --desc "
+ get_double(description)
+ " --copyright 2 "
+ "--tag "
+ get_double(strTags)
+ " --tid "
+ str(TID)
+ " --source "
+ get_double(vUrl)
+ " --line "
+ LineN
# + "--dynamic "
# + get_double("原标题的base64编码(*2):" + get_base64_twice(dynamic_title))
+ " --title "
+ get_double(title)
+ " --cover "
+ os.path.join(sub_dir, "cover.jpg")
)
print("[🚀 Original title]: ", title)
print("[🚀 Start to using biliup, with these CMD commend]:\n", CMD)
biliupOutput = "".join(os.popen(CMD).readlines())
if biliupOutput.find("投稿成功") == -1:
if biliupOutput.find("标题相同") == -1:
print(biliupOutput)
print(
"👻 投稿失败.\n👻 解决问题参考 https://github.com/oiov/u2b/issues or https://github.com/ForgQi/biliup-rs/issues "
)
exit(1)
else:
print("👻 视频标题已存在")
if REMOVE_FILE:
shutil.rmtree(sub_dir)
print("\n🎉🎉🎉 投稿成功,感谢使用哔哩哔哩投稿姬!")
print("⭐⭐⭐ 如果你觉得小姬姬还不错,那就点个赞吧:https://github.com/oiov/u2b\n")
if REMOVE_FILE:
shutil.rmtree(sub_dir)
if __name__ == "__main__":
# 在此命令行调用该脚本,参数1为视频链接,如:https://www.youtube.com/watch?v=xxxxxxxx
# 参数2为视频分区 TID,如:21,参考:https://biliup.github.io/tid-ref.html
# 知识区: 201,科学科普 124,社科·法律·心理 228,人文历史 207,财经商业 208,校园学习 209,职业职场 229,设计·创意 122,野生技能协会
main(sys.argv[1], sys.argv[2])
exit(0)