Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,71 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
# coding: utf-8
|
3 |
+
# Your code here!
|
4 |
|
5 |
+
import requests
|
6 |
+
from bs4 import BeautifulSoup
|
7 |
+
import html
|
8 |
+
import os
|
9 |
+
import json
|
10 |
|
11 |
+
|
12 |
+
def dlvid(vid):
|
13 |
+
url = f"https://www.nicovideo.jp/watch/{vid}"
|
14 |
+
headers = {
|
15 |
+
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0',
|
16 |
+
}
|
17 |
+
|
18 |
+
# 发送请求
|
19 |
+
response = requests.get(url, headers=headers)
|
20 |
+
if response.status_code == 200:
|
21 |
+
# 解析 HTML
|
22 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
23 |
+
# 获取 data-api-data 的值
|
24 |
+
data_api_data = soup.find('div', id='js-initial-watch-data').get('data-api-data')
|
25 |
+
# 对值进行 HTML 解码并转换为 JSON
|
26 |
+
json_data = json.loads(html.unescape(data_api_data))
|
27 |
+
# 在此处你可以将 JSON 数据加载并进行显示
|
28 |
+
|
29 |
+
# 解析 json_data 为字典
|
30 |
+
client_watchTrackId = json_data['client']['watchTrackId']
|
31 |
+
accessRightKey = json_data['media']['domand']['accessRightKey']
|
32 |
+
|
33 |
+
url = f"https://nvapi.nicovideo.jp/v1/watch/{vid}/access-rights/hls?actionTrackId={client_watchTrackId}"
|
34 |
+
|
35 |
+
payload = json.dumps({
|
36 |
+
"outputs": [
|
37 |
+
[
|
38 |
+
"video-h264-720p",
|
39 |
+
"audio-aac-192kbps"
|
40 |
+
]
|
41 |
+
]
|
42 |
+
})
|
43 |
+
headers = {
|
44 |
+
'origin': 'https://www.nicovideo.jp',
|
45 |
+
'referer': 'https://www.nicovideo.jp/',
|
46 |
+
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0',
|
47 |
+
'x-access-right-key': accessRightKey,
|
48 |
+
'x-frontend-id': '6',
|
49 |
+
'x-frontend-version': '0',
|
50 |
+
'x-request-with': 'https://www.nicovideo.jp'
|
51 |
+
}
|
52 |
+
|
53 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
54 |
+
|
55 |
+
hlsjson=response.json()
|
56 |
+
hlsuri=hlsjson['data']['contentUrl']
|
57 |
+
# 删除可能存在的旧的 test.mp4 文件
|
58 |
+
if os.path.exists("test.mp4"):
|
59 |
+
os.remove("test.mp4")
|
60 |
+
|
61 |
+
# 使用 ffmpeg 将 m3u8 转换为 mp4
|
62 |
+
mp3link = hlsuri # 假设你的 m3u8 地址存储在 mp3link 中
|
63 |
+
ffmpeg_command = f'ffmpeg -protocol_whitelist "concat,file,http,https,tcp,tls,crypto" -i "{mp3link}" -c copy "test.mp4"'
|
64 |
+
os.system(ffmpeg_command)
|
65 |
+
return 'test.mp4'
|
66 |
+
else:
|
67 |
+
print("请求失败:", response.status_code)
|
68 |
+
|
69 |
+
|
70 |
+
demo = gr.Interface(fn=dlvid, inputs="text", outputs="video")
|
71 |
demo.launch()
|