当前位置: 首页 > article >正文

脚本-把B站缓存m4s文件转换成mp4格式

js脚本,自动处理视频

  • 1. 需求简介
    • 1.1 pc安装b站客户端
    • 1.2 设置视频缓存目录
    • 1.3 找个视频缓存
    • 1.4 打开缓存文件夹![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/0eb346a84d5f42a7908f1d39bf410c3b.png)
    • 1.5 用notepad++编辑后缀m4s文件,删除文件内开头9个0,把文件小的保存为mp3格式,文件大的保存为mp4格式
    • 1.6 用视频软件或blender把视频和音频合并
    • 1.7 根据需要视频转换分辨率
  • 2. 脚本使用
    • 2.1 需要安装nodejs [https://nodejs.org/zh-cn](https://nodejs.org/zh-cn)
    • 2.2 需要下载ffmpeg.exe
    • 2.3 运行脚本
    • 2.4 结果

背景:需要从b站下载视频,有方法,需要手动一步步操作,繁琐,作为程序员,自然选择自动化。

1. 需求简介

自动化就是把人做的事情用工具或代码自动实现,所以需要先了解需求。

1.1 pc安装b站客户端

1.2 设置视频缓存目录

在这里插入图片描述

1.3 找个视频缓存

1.4 打开缓存文件夹在这里插入图片描述

1.5 用notepad++编辑后缀m4s文件,删除文件内开头9个0,把文件小的保存为mp3格式,文件大的保存为mp4格式

在这里插入图片描述

1.6 用视频软件或blender把视频和音频合并

1.7 根据需要视频转换分辨率

2. 脚本使用

2.1 需要安装nodejs https://nodejs.org/zh-cn

2.2 需要下载ffmpeg.exe

2.3 运行脚本

node .\main.js E:\bilibili "C:\Program Files (x86)\FormatFactory\ffmpeg.exe"
  • 参数1表示b站视频缓存目录,一般这里会有很多文件夹,每一个文件夹就是一个缓存视频
  • 在这里插入图片描述
  • 参数2表示ffmpeg.exe目录,注意路径有空格的需要用引号包起来
    main.js内容
// main.js

const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');

let myfiledataall = [];


// 获取文件大小,返回 Promise
function getFileSize(filePath) {
    return new Promise((resolve, reject) => {
        fs.stat(filePath, (err, stats) => {
            if (err) {
                reject(`获取文件大小失败: ${err}`);
            } else if (stats.isFile()) {
                resolve(stats.size);
            } else {
                resolve(0);
            }
        });
    });
}

// 遍历目录
async function traverseDirectory(directory) {
    try {
        const files = await fs.promises.readdir(directory);
        let myfiledata = [];
        for (const file of files) {
            const filePath = path.join(directory, file);
            const stats = await fs.promises.stat(filePath);
            if (stats.isDirectory()) {
                console.log(`文件夹: ${filePath}`);
                await traverseDirectory(filePath);
            } else if (filePath.endsWith('.m4s')) {
                const filesize = await getFileSize(filePath);
                myfiledata.push({ directory: directory, filename: filePath, filesize });
            }
        }
        myfiledataall.push(myfiledata);
    } catch (err) {
        console.error(`无法读取目录: ${err}`);
    }
}

// 读取文件
async function readFile(filePath) {
    try {
        return await fs.promises.readFile(filePath);
    } catch (err) {
        console.error(`读取文件失败: ${err}`);
        return '';
    }
}

// 写入文件
async function writeFile(filePath, content) {
    try {
        await fs.promises.writeFile(filePath, content);
        console.log('文件写入成功!');
    } catch (err) {
        console.error(`写入文件失败: ${err}`);
    }
}

// 获取命令行参数
const args = process.argv.slice(2);

let folderPath = args[0];
let ffmpegPath = args[1];
args.forEach(async (folderPath, index) => {
    console.log(`参数 ${index + 1}: ${folderPath}`);

});


async function handle() {
    await traverseDirectory(folderPath);
    // console.log("myfiledata:", myfiledataall);

    for (let myfiledata of myfiledataall) {
        console.log(myfiledata);
        if (myfiledata.length === 2) {
            const [first, second] = myfiledata;
            const largerFile = first.filesize > second.filesize ? first : second;
            const smallerFile = first === largerFile ? second : first;

            const videoFilePath = largerFile.filename.replace(".m4s", ".mp4");
            const audioFilePath = smallerFile.filename.replace(".m4s", ".mp3");

            await writeFile(videoFilePath, (await readFile(largerFile.filename)).slice(9));
            await writeFile(audioFilePath, (await readFile(smallerFile.filename)).slice(9));

            const outputFilePath = path.join(largerFile.directory, "output.mp4");
            console.log(outputFilePath);
            const outputFilePath2 = path.join(largerFile.directory, "output2.mp4");
            console.log(outputFilePath2);

            const command = `"${ffmpegPath}" -i "${videoFilePath}" -i "${audioFilePath}" -c:v copy -c:a aac -strict experimental "${outputFilePath}"`;
            console.log(command);
            exec(command, (error, stdout, stderr) => {
                if (error) {
                    console.error(`执行错误: ${error}`);
                    return;
                }
                console.log(`stdout: ${stdout}`);
                console.error(`stderr: ${stderr}`);
                console.log('合成完成!');

                const scaleCommand = `"${ffmpegPath}" -i "${outputFilePath}" -vf scale=960:544 "${outputFilePath2}"`;
                console.log(scaleCommand);
                exec(scaleCommand, (error, stdout, stderr) => {
                    if (error) {
                        console.error(`执行错误: ${error}`);
                        return;
                    }
                    console.log(`stdout: ${stdout}`);
                    console.error(`stderr: ${stderr}`);
                    console.log('分辨率转换完成!');
                });
            });
        }
    }

    console.log('处理完成!');
}

handle();

脚本最后执行2次ffmpeg,一次把mp4和mp3合成一个视频,一次把视频转换分辨率为960x544。

2.4 结果

在这里插入图片描述

  • output.mp4是第一次合成的原视频
  • output2.mp4 是分辨率为960x544的视频

如果视频很大,处理比较慢

在这里插入图片描述


http://www.kler.cn/news/368147.html

相关文章:

  • 温湿传感器(学习笔记下)
  • git命令笔记(速查速查)
  • fetch: 取消请求、读取流、获取下载进度...
  • 从蚂蚁金服面试题窥探STW机制
  • 系统架构设计师教程 第2章 2.6 计算机语言 笔记
  • ArcGIS计算落入面图层中的线的长度或面的面积
  • vue通过JSON文件生成KML文件源码
  • There is no screen to be resumed matching xxx【解决方案、screen、原因分析】
  • 《2024中国泛娱乐出海洞察报告》解析,垂直且多元化方向发展!
  • linux驱动—注册驱动分析
  • 使用Python计算相对强弱指数(RSI)进阶
  • HarmonyOS NEXT 应用开发实战(八、知乎日报List列表下拉刷新及上滑加载更多分页的实现)
  • Vue引入高德地图自定义信息窗体绑定点击事件无效解决方案
  • anaconda 创建环境失败 解决指南
  • 【刷题10】2024.10.26
  • Spark 广播变量(Broadcast Variable)原理及源码分析
  • 绝了,这款播放器让发烧友疯狂种草,堪称音乐神器
  • 力扣876:链表的中间结点
  • 安全知识见闻-网络安全热门证书
  • SpringBoot技术栈在宠物用品交易网站中的应用
  • php后端学习,Java转php
  • 智能合约开发中的LP分红系统
  • 第四期书生大模型实战营(【入门岛】- 第1关 | Linux基础知识)
  • python基础知识点笔记(全)
  • 一个开源的跨平台UI框架,可使用Web技术构建跨平台桌面应用程序
  • 鼠标移入高亮边框效果