cocos creator 集成ffmpeg

1

npm install @ffmpeg/ffmpeg@0.10.1 @ffmpeg/core@0.10.0

2
node_modules@ffmpeg\ffmpeg\package.json添加

"exports": {
        "./dist/ffmpeg.min.js": "./dist/ffmpeg.min.js"
},

3
导入使用:

import { createFFmpeg, fetchFile } from "@ffmpeg/ffmpeg/dist/ffmpeg.min.js";

4
FFmpegCore.js SharedArrayBuffer is not defined

浏览器快捷方式的目标最后面加 --enable-features=SharedArrayBuffer

5
示例

import { createFFmpeg, fetchFile } from "@ffmpeg/ffmpeg/dist/ffmpeg.min.js";


module.exports = {
    logs: '',
    init() {
        window.ffmpeg = createFFmpeg({
            log: true, logger: (...args) => {
                for (let i = 0; i < args.length; i++) {
                    this.logs = this.logs + JSON.stringify(args[i])
                }
            },
        });
    },
    async compressVideo(fileurl, callback) {
        this.logs = ''

        let ffmpeg = window.ffmpeg

        if (!ffmpeg.isLoaded()) {
            await ffmpeg.load();
        }


        let filename = `${new Date().getTime()}.mp4`

        ffmpeg.FS('writeFile', filename, await fetchFile(fileurl));

        await ffmpeg.run('-i', filename);

        const durationMatch = this.logs.match(/Duration: (\d+:\d+:\d+\.\d+)/);
        const bitrateMatch = this.logs.match(/bitrate:\s+(\d+)\s+kb\/s/);

        const duration = durationMatch ? this.parseDuration(durationMatch[1]) : 0;
        const bitrate = bitrateMatch ? parseInt(bitrateMatch[1]) * 1000 : 0; // 转换为 bps


        console.log(`视频时长: ${duration}`);
        console.log(`原编码率: ${bitrate} bps`);


        ffmpeg.FS('unlink', filename);

    },
    parseDuration(durationString) {
        const parts = durationString.split(':');
        return (parseInt(parts[0]) * 3600) + (parseInt(parts[1]) * 60) + parseFloat(parts[2]);
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值