-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyt_dlp_server.py
188 lines (159 loc) · 6.43 KB
/
yt_dlp_server.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
import os
import sys
import argparse
import subprocess
import threading
import queue
import logging
import time
import re
import http.cookiejar
from flask import Flask, render_template, request, render_template_string
from flask_socketio import SocketIO
from urllib.parse import urlparse
def convert_to_netscape():
# 从文件中读取 Cookie 数据
with open('saved_cookies.txt', 'r') as file:
cookie_text = file.read()
# 提取 Cookie 数据
cookies = re.findall(r'([\w-]+)=([^;]+);', cookie_text)
domain = re.search(r'Host: (.+)', cookie_text).group(1)
# 将 Cookie 转换为 Netscape 格式并保存到文件
jar = http.cookiejar.MozillaCookieJar('cookies.txt')
for cookie_name, cookie_value in cookies:
cookie = http.cookiejar.Cookie(
version=0,
name=cookie_name,
value=cookie_value,
port=None,
port_specified=False,
domain=domain,
domain_specified=True,
domain_initial_dot=False,
path='/',
path_specified=True,
secure=True,
expires=None,
discard=False,
comment=None,
comment_url=None,
rest={},
rfc2109=False,
)
jar.set_cookie(cookie)
jar.save(ignore_discard=True, ignore_expires=True)
def setup_logging():
# 设置日志记录
push_logger = logging.getLogger('push')
push_logger.setLevel(logging.INFO)
push_handler = logging.FileHandler('yt_dlp_push.log', encoding='utf-8')
push_handler.setFormatter(logging.Formatter('%(asctime)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S'))
push_logger.addHandler(push_handler)
return push_logger
args = None
app = Flask(__name__)
socketio = SocketIO(app)
push_logger = setup_logging()
index_template = '''
<!doctype html>
<html>
<head>
<title>YT-DLP 服务器</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 0.9;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<h1>YT-DLP 服务器</h1>
<p>当前监听地址:{{ host }}</p>
<p>当前监听端口:{{ port }}</p>
<p>当前下载目录:{{ download_dir }}</p>
<p>通过 GET 方式提交下载:http://{{ request_host }}:{{ port }}/download?url=https://www.example.com/video-url</p>
</body>
</html>
'''
@app.route('/')
def index():
request_host = request.headers.get('Host').split(':')[0]
return render_template_string(index_template, host=args.host, request_host=request_host, port=args.port, download_dir=args.download_dir)
def download_video(url, cookie, socketio, output_directory):
try:
if not os.path.exists(output_directory):
os.makedirs(output_directory)
host = urlparse(url).netloc
if cookie:
with open("saved_cookies.txt", "w") as cookie_file:
cookie_file.write(f"Host: {host}\n")
cookie_file.write(f"Cookie: {cookie}\n")
convert_to_netscape()
cmd = ['yt-dlp', '-o', f'{output_directory}/%(title)s-%(id)s.%(ext)s', url, '--newline', '--concurrent-fragments', '16', '--cookies', 'cookies.txt']
else:
cmd = ['yt-dlp', '-o', f'{output_directory}/%(title)s-%(id)s.%(ext)s', url, '--newline', '--concurrent-fragments', '16']
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
for line in iter(process.stdout.readline, ''):
socketio.emit('output', line.strip())
print(line.strip())
process.wait()
except Exception as e:
print(f"下载出错:{e}")
finally:
if cookie:
os.remove("saved_cookies.txt")
os.remove("cookies.txt")
socketio.sleep(0)
socketio.emit("download complete", namespace="/")
download_queue = queue.Queue()
def download_thread(socketio, output_directory):
while True:
url, cookie, socketio = download_queue.get()
push_logger.info(f"开始下载:{url}")
download_video(url, cookie, socketio, output_directory)
@app.route('/download', methods=['GET', 'POST'])
def download():
url = request.values.get('url')
cookie = request.values.get('cookie')
if not url:
return '需要提供网址', 400
push_logger.info(f"添加下载任务:{url}")
download_queue.put((url, cookie, socketio))
return '下载任务已添加', 200
def main():
global args
parser = argparse.ArgumentParser()
parser.add_argument('-l', '--host', default='0.0.0.0', help='设置监听地址(默认:0.0.0.0)')
parser.add_argument('-p', '--port', type=int, default=7777, help='设置监听端口(默认:7777)')
parser.add_argument('-d', '--download-dir', default='downloads', help='设置下载目录(默认:downloads)')
parser.add_argument('-6', '--ipv6', action='store_true', help='使用IPv6监听(默认使用IPv4)')
parser.add_argument('--diy', default='', help='允许添加自定义yt-dlp启动参数。例如,可以通过增加"--all-subs"参数来下载所有字幕。')
args = parser.parse_args()
# 根据IPv6选项设置监听地址
if args.ipv6:
args.host = '::'
else:
args.host = '0.0.0.0'
print("YT-DLP 服务器使用说明:")
print(" -l, --host 设置监听地址(默认:0.0.0.0)")
print(" -p, --port 设置监听端口(默认:7777)")
print(" -d, --download-dir 设置下载目录(默认:downloads)")
print(" --diy 允许添加自定义yt-dlp启动参数。例如,可以通过增加'--all-subs'参数来下载所有字幕。")
print()
print("常规启动示例:")
print(" python yt_dlp_server.py -l 0.0.0.0 -p 7777 -d downloads")
print()
print("带自定义yt-dlp参数启动示例(如下载所有字幕):")
print(" python yt_dlp_server.py -l 0.0.0.0 -p 7777 -d downloads --diy=\"--all-subs\"")
print(f"\n当前监听地址:{args.host}")
print(f"当前监听端口:{args.port}")
print(f"当前下载目录:{args.download_dir}")
# 如果用户指定了自定义参数,显示这些参数
if args.diy:
print(f"自定义参数:{args.diy}")
threading.Thread(target=download_thread, args=(socketio, args.download_dir), daemon=True).start()
socketio.run(app, host=args.host, port=args.port)
if __name__ == '__main__':
main()