手机上Bilibili缓存的视频存在于Android/data/tv.danmaku.bili/download下面。然后可以拷贝出来到PC上,因为视频和音频是分开保存的,所以要进行合并的操作。根据自己缓存时候选择的分辨率修改下合并的代码中的路径(16,64这些数字代表的就是路径)。合并操作是调用的ffmpeg, 所以需要将ffmpeg放到cmd可以调用的地方,当前目录或者system32都行。

1. 合并成视频

import os
import json

video_path = r"\40861138"

def list_dir(src_path):
    list_name = []
    for file in os.listdir(src_path):
        file_path = os.path.join(src_path, file)
        if os.path.isdir(file_path):
            list_name.append(file_path)
    return list_name


if __name__ == '__main__':
    paths = list_dir(video_path)
    for path in paths:
        entry_json_file_path = os.path.join(path, "entry.json")
        with open(entry_json_file_path, 'r', encoding="utf-8") as load_f:
            text = load_f.read()
            dicts = json.loads(text)
            # print(dicts)
            tt = dicts.get("page_data").get("part")
            # ffmpeg.exe -i ".\%x\16\video.m4s" -i ".\%x\16\audio.m4s" -vcodec copy -acodec copy -f mp4 "%x.mp4"
            # 这里的64或者16代表的是视频的分辨率
            cmd = "ffmpeg -i " + path + "/64/video.m4s" + " -i " + path + "/64/audio.m4s" + " -vcodec copy -acodec copy -f mp4 " + '"' + os.path.join(
                video_path, tt + ".mp4") + '"'
            print(cmd)
            os.system(cmd)

2. 压缩视频

import os

video_path = r".\90465248"  # 原mp4文件的文件夹
dst_video_path = r".\fk"  # 压缩后视频的文件夹


def list_dir(src_path):
    list_name = []
    for file_name_path in os.listdir(src_path):
        file_path = os.path.join(src_path, file_name_path)
        if os.path.isdir(file_path) is False:
            list_name.append(file_path)
    return list_name


if __name__ == '__main__':
    files = list_dir(video_path)
    for file in files:
        filename = os.path.basename(file)
        # print(filename)
        cmd = "ffmpeg.exe -i " + ' "' + file + '" ' + " -r 10 -c:v libx264 -crf 28 -preset veryfast -c:a copy -movflags +faststart " + '"' + os.path.join(
            dst_video_path, filename) + '"'
        print(cmd)
        os.system(cmd)