async def video_detail(id_, type_): try: if type_ == 'bv': v = video.Video(bvid=id_) else: v = video.Video(aid=int(id_)) info = await v.get_info() msg = [{ "type": "image", "data": { "file": info['pic'] } }, { "type": "text", "data": { "text": f'《{info["title"]}》\n' f'Up主: {info["owner"]["name"]}\n' f'URL: https://bilibili.com/video/av{info["aid"]}' } }] return msg except ResponseCodeException: msg = '没有找到视频信息' return msg except Exception as e: msg = "解析出错--Error: {}\n".format(type(e)) # msg += traceback.format_exc() return msg
async def get_graph_version(bvid: str, credential: Credential = None): """ 获取剧情图版本号,仅供 `get_edge_info()` 使用。 Args: bvid (str): bvid credential (Credential, optional): [description]. Defaults to None. Returns: int: 剧情图版本号 """ # 取得初始顶点 cid v = video.Video(bvid=bvid, credential=credential) page_list = await v.get_pages() cid = page_list[0]['cid'] # 获取剧情图版本号 api = 'https://api.bilibili.com/x/player/v2' params = { "bvid": bvid, "cid": cid } resp = await request('GET', api, params, credential=credential) return resp['interaction']['graph_version']
async def main(p=0): credential = Credential(sessdata=SESSDATA, bili_jct=BILI_JCT, buvid3=BUVID3) v = video.Video(bvid=BV, credential=credential) info = await v.get_info() url = await v.get_download_url(p) # 编号从0开始计数 video_url = url["dash"]["video"][0]['baseUrl'] audio_url = url["dash"]["audio"][0]['baseUrl'] video_name = info["pages"][p]["part"].replace(" ", "_") HEADERS = { "User-Agent": "Mozilla/5.0", "Referer": "https://www.bilibili.com/" } async with aiohttp.ClientSession() as sess: async with sess.get(video_url, headers=HEADERS) as resp: length = resp.headers.get('content-length') with open(f'video_temp_{p}.m4s', 'wb') as f: process = 0 while True: chunk = await resp.content.read(1024) if not chunk: break process += len(chunk) # print(f'下载视频流 {process} / {length}') f.write(chunk) async with sess.get(audio_url, headers=HEADERS) as resp: length = resp.headers.get('content-length') with open(f'audio_temp_{p}.m4s', 'wb') as f: process = 0 while True: chunk = await resp.content.read(1024) if not chunk: break process += len(chunk) # print(f'下载音频流 {process} / {length}') f.write(chunk) # 混流 # print('混流中') os.system( f'{FFMPEG_PATH} -i video_temp_{p}.m4s -i audio_temp_{p}.m4s -vcodec copy -acodec copy {PATH}/{p+1}_{video_name}.mp4' ) # 删除临时文件 os.remove(f"video_temp_{p}.m4s") os.remove(f"audio_temp_{p}.m4s") print(f'已下载为:{p+1}_{video_name}.mp4')
async def test_h_create_video_favorite_list(): # 创建临时收藏夹 rnd_name = random.randint(100000, 999999) data = await favorite_list.create_video_favorite_list( f"TESTING_{rnd_name}", "", False, credential=credential) global media_id media_id = data["id"] # 收藏两个视频供测试 for aid in aids: v = video.Video(aid=aid, credential=credential) await v.set_favorite([media_id]) return data
import asyncio import time from bilibili_api.utils.Danmaku import Danmaku from bilibili_api.exceptions.ResponseCodeException import ResponseCodeException from bilibili_api import video as video_m, exceptions from .common import get_credential import datetime BVID = "BV1xx411c7Xg" AID = 271 video = video_m.Video(BVID, credential=get_credential()) async def test_set_bvid(): # 设置正确 bvid video.set_bvid(BVID) assert video.get_bvid() == BVID, "bvid 应该被修改" assert video.get_aid() == AID, "aid 应该从 bvid 转换" # 设置错误 bvid try: video.set_bvid("BVajsdoiajinsodn") assert False, "设置了错误 bvid 但未报错" except exceptions.ArgsException: video.set_bvid(BVID) async def test_set_aid(): # 设置正确 aid video.set_aid(AID) assert video.get_aid() == AID, "aid 应该被修改"
async def after_all(): # 清理默认收藏夹中的视频 v = video.Video(aid=aids[0], credential=credential) await v.set_favorite(del_media_ids=[default_media_id])