-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupload.py
316 lines (268 loc) · 8.56 KB
/
upload.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import json
import os
import re
import shutil
import sys
import time
import base64
import shlex
import requests
import yt_dlp
from PIL import Image
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
OWNER_NAME = "username"
REMOVE_FILE = True # 是否删除投稿后的视频文件
LineN = "qn" # 线路 cos bda2 qn ws kodo
DEFAULT_TID = 21
PROXY = 'http://127.0.0.1:20171'
COOKIES_FROM_BROWSER = ("firefox",)
URL_LIST_FILE = "url_list.json"
def escape_description(description):
return shlex.quote(description)
def get_double(s):
return '"' + s + '"'
def cover_webp_to_jpg(webp_path, jpg_path):
im = Image.open(webp_path).convert("RGB")
im.save(jpg_path, "jpeg")
im.close()
def download(youtube_url, folder_name):
ydl_opts = {
"outtmpl": "./videos/" + str(folder_name) + "/%(id)s.mp4",
"cookiesfrombrowser": COOKIES_FROM_BROWSER,
'proxy': PROXY
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([youtube_url])
def get_info(url):
ydl_opts = {
"cookiesfrombrowser": COOKIES_FROM_BROWSER,
'proxy': PROXY
}
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_):
proxies = {
"http": PROXY,
"https": PROXY,
}
r = requests.get(url, stream=True, proxies=proxies)
f = open("./videos/" + str(id_) + "/cover.webp", "wb")
for chunk in r.iter_content(chunk_size=102400):
if chunk:
f.write(chunk)
f.close()
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 biliup_upload(vUrl, TID, title, dynamic_title, description, tags, videoPath, cover):
strTags = ",".join(tags)
CMD = (
".\\biliup upload "
+ videoPath
+ " --desc "
+ "此为转载视频"
+ " --copyright 2 "
+ "--tag "
+ get_double(strTags)
+ " --tid "
+ str(TID)
+ " --source "
+ get_double(vUrl)
+ " --line "
+ LineN
+ " --title "
+ get_double(title)
+ " --cover "
+ str(cover)
)
print("[🚀 origin title]: ", title)
print("[🚀 Start to using biliup, with these CMD commend]:\n", CMD)
biliupOutput = "".join(os.popen(CMD).readlines())
return "投稿成功" in biliupOutput or "标题相同" in biliupOutput
def process_video(vUrl, TID):
try:
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)
try:
os.mkdir(path="./videos/" + str(id_))
except FileExistsError:
shutil.rmtree("./videos/" + str(id_))
os.mkdir(path="./videos/" + str(id_))
download(vUrl, id_)
download_image(cover, id_)
cover_webp_to_jpg("./videos/" + str(id_) + "/cover.webp", "./videos/" + str(id_) + "/cover.jpg")
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)
videoPath = getVideoPath(id_)
cover_path = "./videos/" + str(id_) + "/cover.jpg"
success = biliup_upload(vUrl, TID, title, dynamic_title, description, tags, videoPath, cover_path)
if success and REMOVE_FILE:
shutil.rmtree("./videos/" + str(id_))
return success
except Exception as e:
print(f"Error processing video {vUrl}: {e}")
return False
def load_url_list():
if os.path.exists(URL_LIST_FILE):
with open(URL_LIST_FILE, "r", encoding="utf-8") as f:
return json.load(f)
return []
def save_url_list(url_list):
with open(URL_LIST_FILE, "w", encoding="utf-8") as f:
json.dump(url_list, f, ensure_ascii=False, indent=4)
def mode_single_video():
url = input("请输入视频URL: ")
tid = input("请输入分区代码 (默认21): ")
if not tid:
tid = DEFAULT_TID
else:
tid = int(tid)
process_video(url, tid)
def mode_video_list():
url = input("请输入视频列表或频道URL: ")
os.system(f'yt-dlp --flat-playlist --dump-single-json --cookies-from-browser firefox --proxy {PROXY} {url} > output.json')
with open('output.json', 'r', encoding='utf-8') as f:
data = json.load(f)
url_list = [{"url": entry["url"], "status": "no", "count": 0} for entry in data["entries"] if entry["url"].startswith("https://www.youtube.com")]
save_url_list(url_list)
print("以下是需要上传的视频URL列表:")
for entry in url_list:
print(entry["url"])
confirm = input("请确认以上URL是否正确 (yes/no): ")
if confirm.lower() != "yes":
print("操作已取消。")
return
tid = input("请输入分区代码 (默认21): ")
if not tid:
tid = DEFAULT_TID
else:
tid = int(tid)
for entry in url_list:
if entry["status"] == "no":
for _ in range(2): # 尝试2次
success = process_video(entry["url"], tid)
if success:
entry["status"] = "yes"
break
entry["count"] += 1
save_url_list(url_list)
def mode_resume_upload():
tid = input("请输入分区代码 (默认21): ")
if not tid:
tid = DEFAULT_TID
else:
tid = int(tid)
url_list = load_url_list()
for entry in url_list:
if entry["status"] == "no":
for _ in range(2): # 尝试2次
success = process_video(entry["url"], tid)
if success:
entry["status"] = "yes"
break
entry["count"] += 1
save_url_list(url_list)
def mode_manual_playlist():
urls = []
while True:
url = input("请输入视频URL (输入'完毕'结束): ")
if url.lower() == '完毕':
break
urls.append({"url": url, "status": "no", "count": 0})
if not urls:
print("没有输入任何URL。")
return
save_url_list(urls)
print("以下是需要上传的视频URL列表:")
for entry in urls:
print(entry["url"])
confirm = input("请确认以上URL是否正确 (yes/no): ")
if confirm.lower() != "yes":
print("操作已取消。")
return
tid = input("请输入分区代码 (默认21): ")
if not tid:
tid = DEFAULT_TID
else:
tid = int(tid)
for entry in urls:
if entry["status"] == "no":
for _ in range(2): # 尝试2次
success = process_video(entry["url"], tid)
if success:
entry["status"] = "yes"
break
entry["count"] += 1
save_url_list(urls)
def main():
print("请选择模式:")
print("1: 单视频上传模式")
print("2: 视频列表或频道模式")
print("3: 断点续传模式")
print("4: 手动输入多个单视频链接模式")
mode = input("请输入模式编号: ")
if mode == "1":
mode_single_video()
elif mode == "2":
mode_video_list()
elif mode == "3":
mode_resume_upload()
elif mode == "4":
mode_manual_playlist()
else:
print("无效的模式编号。")
if __name__ == "__main__":
main()