mirror of
https://github.com/ClovertaTheTrilobita/SanYeCao-Nonebot.git
synced 2026-04-01 22:04:51 +00:00
抛出视频无法播放的异常
This commit is contained in:
parent
e27c956dda
commit
2e6ba61a4f
2 changed files with 33 additions and 6 deletions
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
import time
|
import time
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
import nonebot.adapters.qq.exception
|
||||||
import requests
|
import requests
|
||||||
from nonebot import on_command
|
from nonebot import on_command
|
||||||
from nonebot.rule import to_me
|
from nonebot.rule import to_me
|
||||||
|
|
@ -69,7 +71,13 @@ async def get_video_file(message: MessageEvent):
|
||||||
video_url = biliVideos.get_video_file_url(keyword[0], cid)
|
video_url = biliVideos.get_video_file_url(keyword[0], cid)
|
||||||
biliVideos.video_download(video_url, cid)
|
biliVideos.video_download(video_url, cid)
|
||||||
# biliVideos.transcode_video(f"{cid}.mp4",f"{cid}-o.mp4")
|
# biliVideos.transcode_video(f"{cid}.mp4",f"{cid}-o.mp4")
|
||||||
await bili_bv_search.send(Message(MessageSegment.file_video(Path(f"{cid}.mp4"))))
|
|
||||||
|
try:
|
||||||
|
await bili_bv_search.send(Message(MessageSegment.file_video(Path(f"./src/videos/file/{cid}.mp4"))))
|
||||||
|
except nonebot.adapters.qq.exception.ActionFailed:
|
||||||
|
await bili_bv_search.finish("发送失败惹,可能是视频过长,请尽量搜索1分钟以内的视频吧。")
|
||||||
|
|
||||||
|
biliVideos.delete_video(cid)
|
||||||
|
|
||||||
elif len(keyword) >= 2:
|
elif len(keyword) >= 2:
|
||||||
|
|
||||||
|
|
@ -94,4 +102,11 @@ async def get_video_file(message: MessageEvent):
|
||||||
video_url = biliVideos.get_video_file_url(keyword[0], cid)
|
video_url = biliVideos.get_video_file_url(keyword[0], cid)
|
||||||
biliVideos.video_download(video_url, cid)
|
biliVideos.video_download(video_url, cid)
|
||||||
|
|
||||||
await bili_bv_search.send(Message(MessageSegment.file_video(Path(f"{cid}.mp4"))))
|
try:
|
||||||
|
await bili_bv_search.send(Message(MessageSegment.file_video(Path(f"./src/videos/file/{cid}.mp4"))))
|
||||||
|
except nonebot.adapters.qq.exception.ActionFailed:
|
||||||
|
await bili_bv_search.finish("发送失败惹,可能是视频过长,请尽量搜索1分钟以内的视频吧。")
|
||||||
|
|
||||||
|
biliVideos.delete_video(cid)
|
||||||
|
|
||||||
|
await bili_bv_search.finish()
|
||||||
|
|
|
||||||
|
|
@ -100,7 +100,7 @@ def get_video_file_url(bvid, cid):
|
||||||
params = {
|
params = {
|
||||||
'bvid': bvid,
|
'bvid': bvid,
|
||||||
'cid': cid,
|
'cid': cid,
|
||||||
'qn': '16',
|
'qn': '64',
|
||||||
'platform': 'html5'
|
'platform': 'html5'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -121,7 +121,7 @@ def video_download(file_url, cid):
|
||||||
# 检查请求是否成功
|
# 检查请求是否成功
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
# 将视频保存到本地文件
|
# 将视频保存到本地文件
|
||||||
with open(f'{cid}.mp4', 'wb') as file:
|
with open(f'./src/videos/file/{cid}.mp4', 'wb') as file:
|
||||||
for chunk in response.iter_content(chunk_size=1024):
|
for chunk in response.iter_content(chunk_size=1024):
|
||||||
if chunk:
|
if chunk:
|
||||||
file.write(chunk)
|
file.write(chunk)
|
||||||
|
|
@ -129,6 +129,18 @@ def video_download(file_url, cid):
|
||||||
else:
|
else:
|
||||||
print(f"下载失败,状态码:{response.status_code}")
|
print(f"下载失败,状态码:{response.status_code}")
|
||||||
|
|
||||||
|
def delete_video(cid):
|
||||||
|
# 指定要删除的文件路径
|
||||||
|
file_path = f"src/videos/file/{cid}.mp4"
|
||||||
|
|
||||||
|
# 检查文件是否存在
|
||||||
|
if os.path.exists(file_path):
|
||||||
|
# 删除文件
|
||||||
|
os.remove(file_path)
|
||||||
|
print(f"文件 {file_path} 已被删除。")
|
||||||
|
else:
|
||||||
|
print(f"文件 {file_path} 不存在。")
|
||||||
|
|
||||||
def transcode_video(input_file, output_file):
|
def transcode_video(input_file, output_file):
|
||||||
try:
|
try:
|
||||||
# 转码视频为 H.264 编码
|
# 转码视频为 H.264 编码
|
||||||
|
|
@ -143,6 +155,6 @@ def transcode_video(input_file, output_file):
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(get_video_info('海南某211台风过后现状111'))
|
print(get_video_info('海南某211台风过后现状111'))
|
||||||
print(get_video_file_url('BV1y7411Q7Eq', '171776208'))
|
print(get_video_file_url('BV1p2PDeEENs', '28194047730'))
|
||||||
|
|
||||||
print(get_video_pages_info('BV1y7411Q7Eq'))
|
print(get_video_pages_info('BV1p2PDeEENs'))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue