云迈博客

您现在的位置是:首页 > 其他 > 正文

其他

使用nodejs 下载 m3u8并且合并成mp4

吴志云2022-08-31其他70
所需第三方模块mimeffmpeg.wasm.jsmime获取文件类型ffmpeg.wasm.js可运行到浏览器和nodejs运行环境编写代码安装依赖npmi@ffm

所需 第三方模块 mime ffmpeg.wasm.js
mime 获取文件类型
ffmpeg.wasm.js 可运行到浏览器 和nodejs运行环境
编写代码
安装 依赖 npm i @ffmpeg/ffmpeg @ffmpeg/core mime

const https = require("https");
const path = require("path");
const fs = require("fs");
const {
    createFFmpeg,
    fetchFile
} = require('@ffmpeg/ffmpeg');
const ffmpeg = createFFmpeg({
    log: true
});
https.get(
    "https://www.xmfans.me/tmm3p/index.m3u8?path=%2fttm3p%2fttm3p%2dtsdm%2d%e6%9f%90%e7%a7%91%e5%ad%a6%e7%9a%84%e8%b6%85%e7%94%b5%e7%a3%81%e7%82%aeT%5f%e7%a7%91%e5%ad%a6%e8%b6%85%e7%94%b5%e7%a3%81%e7%82%aeT%2d%e7%ac%ac2%e9%9b%86.m3u8&getplay_url=/_getplay?aid=19151&playindex=1&epindex=1&r=0.47047290588763313&vlt_l=0&vlt_r=0'",
    (res) => {
        if (res.statusCode === 200) {
            const buf = []
            res.on("data", chunk => {
                buf.push(chunk)
            })
            res.on("end", () => {
                const data = buf.toString();
                const str = data.replace(/#.+,?/g, "");
                start(str.match(/\S+/g))
            })
        }
    })

let count = 0;
async function start(list) {
    try {
        count++
        await download(list[count].replace(",", ""));
        if (count < list.length) {
            start(list)
        }else{
            console.log("下载完成了");
            readVideos();
        }
    } catch (e) {
        if (count < list.length) {
            start(list)
        }else{
            console.log("下载完成了");
            readVideos();
        }
    }
}

function download(url) {
    return new Promise((resolve, reject) => {
        https.get(url, res => {
            if (res.statusCode == 200) {
                const wr = fs.createWriteStream(path.resolve(__dirname, "m3u8", new Date().getTime() +
                    ".ts"));
                let i = 0;
                res.on("data", data => {
                    if (i == 0) {
                        wr.write(data.slice(212, data.length));
                    } else {
                        wr.write(data);
                    }
                    i++;
                })
                res.on("end", () => {
                    resolve("ok")
                })
                res.on("error", (err) => {
                    reject(err)
                })
            } else {
                reject(res)
            }
        })
    })
}
async function readVideos() {
    const dir = path.resolve(__dirname, "m3u8");
     await ffmpeg.load();
    fs.readdir(dir, (err, files) => {
        writeVideo(files);
    });
    let i = 0;
    async function writeVideo(files) {
        console.log(path.resolve(dir, files[i]));
        ffmpeg.FS('writeFile', files[i], await fetchFile(path.resolve(dir, files[i])));
        i++
        if (i < files.length) {
            writeVideo(files)
        } else {
            merge(files)
        }
    }
}

async function merge(videos) {
    await ffmpeg.run("-i", `concat:${videos.join('|')}`,"-c","copy","-bsf:a","aac_adtstoasc","-movflags","+faststart","output.mp4");
    await fs.promises.writeFile('./output.mp4', ffmpeg.FS('readFile', 'output.mp4'));
    process.exit(0);
}

发表评论

评论列表

  • 这篇文章还没有收到评论,赶紧来抢沙发吧~