forked from lanbinleo/bili2text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
46 lines (44 loc) · 1.59 KB
/
utils.py
File metadata and controls
46 lines (44 loc) · 1.59 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
import os
import re
import subprocess
import glob # 新增导入
def ensure_folders_exist(output_dir):
if not os.path.exists("bilibili_video"):
os.makedirs("bilibili_video")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
if not os.path.exists("outputs"):
os.makedirs("outputs")
def download_video(bv_number):
"""
使用you-get下载B站视频。
参数:
bv_number: 字符串形式的BV号(不含"BV"前缀)或完整BV号
返回:
文件路径
"""
if not bv_number.startswith("BV"):
bv_number = "BV" + bv_number
video_url = f"https://www.bilibili.com/video/{bv_number}"
output_dir = f"bilibili_video/{bv_number}" # 下载视频到 bilibili_video/{bv_number} 目录
ensure_folders_exist(output_dir)
print(f"使用you-get下载视频: {video_url}")
try:
result = subprocess.run(["you-get", "-l", "-o", output_dir, video_url], capture_output=True, text=True)
if result.returncode != 0:
print("下载失败:", result.stderr)
else:
print(result.stdout)
print(f"视频已成功下载到目录: {output_dir}")
video_files = glob.glob(os.path.join(output_dir, "*.mp4"))
if video_files:
# 删除xml文件
xml_files = glob.glob(os.path.join(output_dir, "*.xml"))
for xml_file in xml_files:
os.remove(xml_file)
else:
file_path = ""
except Exception as e:
print("发生错误:", str(e))
file_path = ""
return bv_number