通过python实现bilibili缓存视频转为mp4格式

需要提前下好ffmpeg

python 复制代码
import os
import fnmatch
import subprocess

'''
Bilibili缓存的视频,*280.m4s结尾的是音频文件,*050.m4s结尾的是视频,删除16进制下前9个0,即为正常音/视频
使用os.walk模块,遍历每一个目录,对每个子目录下存在的以上两个文件进行修改
去掉前九个0以后,输出到新的目录output
利用ffmpeg合成新的MP4视频到output目录的outputVideo中,作为最终转换的视频
'''


def conVert(path,outpath):
    #需要进行合成的m4s文件列表
    mp3lt = []
    mp4lt = []
    for root,dirs,files in os.walk(path):#对目录进行遍历
        #root表示当前的根目录,dirs表示当前目录下所有文件夹,files表示当前目录下所有文件
        dirs[:] = [d for d in dirs if d!="output"]#跳过output目录的遍历
        if '.a' in files:
            #为了提高效率,已经转换过的视频,在视频所处目录下添加一个".a"的空文件,作为标记
            #不存在".a"文件的目录,两个m4s后缀的文件分别加入mp3lt和mp4lt中,代表这个视频还没有进行过视频转换,反之亦然
            print("目录{}已进行过视频格式转换".format(root))
            dirs[:] = []#files是当前目录下的所有文件的列表,若列表中存在".a",则表示不需要进行视频转换,dirs清空,直至遍历下一个目录时重置
            continue    #跳过
        for f in files:#对此次遍历所处的目录下的所有文件进行遍历
            if fnmatch.fnmatch(f,'*280.m4s'):#若存在音频文件
                mp3lt.append(f)#添加进转换列表中
                fn_mp3 = os.path.join(root,f)   #去除9个字节后的文件保存路径
                with open(fn_mp3,'rb') as fmp3:
                    fmp3.read(9)    #读取前9个字节
                    remaining_content = fmp3.read() #保留剩余的字节
                with open(os.path.join(outpath,f),'wb') as fmp3:
                    fmp3.write(remaining_content)   #将剩余的字节写入新的文件中

            if fnmatch.fnmatch(f,'*050.m4s'):#若存在视频文件
                mp4lt.append(f)#添加进转换列表中
                fn_mp4 = os.path.join(root,f)   #去除9个字节后的文件保存路径
                with open(fn_mp4,'rb') as fmp4:
                    fmp4.read(9)    #读取前9个字节
                    remaining_content = fmp4.read() #保留剩余的字节
                with open(os.path.join(outpath,f),'wb') as fmp4:
                    fmp4.write(remaining_content) #将剩余的字节写入新的文件中
                with open(os.path.join(root,'.a'),'wb') as a:#创建空文件作为标记
                    pass
                print(f"成功创建标记{os.path.join(root,'.a')}")
    return mp3lt,mp4lt#返回列表
def delete_file(path):#删除".a"标记
    delete_counted = 0
    for root,dirs,files in os.walk(path):
        dirs[:] = [d for d in dirs if d!="output"]
        if '.a' in files:
            os.remove(os.path.join(root,'.a'))
            delete_counted += 1
            print(f"已删除:{os.path.join(root,'.a')}")
            dirs[:] = []
            continue
        else:
            print(f"{os.path.join(root,'.a')}文件不存在")
            continue
    print(f"已删除数量:{delete_counted}")
def convert_video(mp3lt,mp4lt,outpath):#对m4s文件进行合成
    conVert_conuts = len(mp4lt)
    output = os.path.join(outpath+'\outputVideo')#最终的mp4视频写入outputVideo文件夹中
    if conVert_conuts!=0 :#存在需要进行转换的视频时
        for m in range(conVert_conuts):#ffmpeg合成视频的命令
            command = [
                "ffmpeg",
                "-i",os.path.join(outpath,mp4lt[m]),
                "-i",os.path.join(outpath,mp3lt[m]),
                "-codec",
                "copy",
                os.path.join(output,mp4lt[m].split(".")[0]+'.mp4')
            ]
            #print(command)
            subprocess.run(command)
    else:
        print("当前没有需要转换的视频")
def convert(path,outpath):
    mp3lt,mp4lt = conVert(path,outpath)
    print(mp3lt,mp4lt)
    convert_video(mp3lt,mp4lt,outpath)
#需要转换时执行convert方法,需要删除".a"标记重新转换视频时,先执行delete_file方法再执行convert方法
def main():
    path="D:\BiliDownload\VIdeosBiliBili" #bilibili缓存路径
    outpath = "D:\BiliDownload\VIdeosBiliBili\output" #转换的视频保存路径
    convert(path,outpath)
    #delete_file(path)
main()

转换结束后可以把output目录下的m4s全部删除掉,或者在python代码中实现


相关推荐
EasyDSS2 分钟前
安防监控视频管理平台EasyCVR助力建筑工地施工4G/5G远程视频监管方案
大数据·网络·网络协议·音视频
TYUT_xiaoming9 分钟前
python setup.py学习
python
A懿轩A38 分钟前
2025年十六届蓝桥杯Python B组原题及代码解析
python·算法·蓝桥杯·idle·b组
程序媛徐师姐44 分钟前
Python Django基于协同过滤算法的招聘信息推荐系统【附源码、文档说明】
python·django·协同过滤算法·招聘信息推荐系统·招聘信息·python招聘信息推荐系统·python招聘信息
2401_890665861 小时前
免费送源码:Java+ssm+MySQL 基于PHP在线考试系统的设计与实现 计算机毕业设计原创定制
java·hadoop·spring boot·python·mysql·spring cloud·php
xuemenghan1 小时前
Numba 从零基础到实战:解锁 Python 性能新境界
开发语言·python
明月看潮生1 小时前
青少年编程与数学 02-016 Python数据结构与算法 22课题、并行算法
开发语言·python·青少年编程·并行计算·编程与数学
明月看潮生2 小时前
青少年编程与数学 02-016 Python数据结构与算法 20课题、几何算法
python·算法·青少年编程·编程与数学
limengshi1383922 小时前
使用Python+xml+shutil修改目标检测图片和对应xml标注文件
xml·python·目标检测
计算机徐师兄3 小时前
Python基于Django的房屋信息可视化及价格预测系统(附源码,文档说明)
python·房屋信息可视化·房屋价格预测系统·房屋价格预测·房屋分析·python房屋信息可视化系统·python房屋价格预测系统