使用Fluent-ffmpeg将屏幕截图上传到Google云存储桶

问题描述

我目前正在使用multer将视频上传到我的Google存储桶,并使用fluent-ffmpeg捕获视频的缩略图。视频已正确上传到存储桶中,但未正确上传ffmpeg的缩略图。如何将缩略图的位置更改为我的Google存储桶?

上传后端视频

require ('dotenv').config()
const express = require('express');
const router = express.Router();
const multer = require("multer");
var ffmpeg = require('fluent-ffmpeg');
const multerGoogleStorage = require('multer-google-storage');
const { Video } = require("../models/Video");
const {User} = require("../models/User")
const { auth } = require("../middleware/auth");


var storage = multer({
    destination: function (req,file,cb) {
        cb(null,'videos/')
    },filename: function (req,`${Date.now()}_${file.originalname}`)
    },fileFilter: (req,cb) => {
        const ext = path.extname(file.originalname)
        if (ext !== '.mp4' || ext !== '.mov' || ext !== '.m3u' || ext !== '.flv' || ext !== '.avi' || ext !== '.mkv') {
            return cb(res.status(400).end('Error only videos can be uploaded'),false);
        }
        cb(null,true)
    }
})
// Set location to google storage bucket
var upload = multer({ storage: multerGoogleStorage.storageEngine() }).single("file")

router.post("/uploadfiles",(req,res) => {

    upload(req,res,err => {
        if (err) {
            return res.json({sucess: false,err})
        }
        return res.json({ success: true,filePath: res.req.file.path,fileName: res.req.file.filename})
    })
});

后端缩略图上传

router.post("/thumbnail",res) => {

    let thumbsFilePath = "";
    let fileDuration = "";

    ffmpeg.ffprobe(req.body.filePath,function (err,metadata) {
        console.dir(metadata);
        console.log(metadata.format.duration);

        fileDuration = metadata.format.duration;
    })


    ffmpeg(req.body.filePath)
        .on('filenames',function (filenames) {
            console.log('Will generate ' + filenames.join(','))
            thumbsFilePath = "thumbnails/" + filenames[0];
        })
        .on('end',function () {
            console.log('Screenshots taken');
            return res.json({ success: true,thumbsFilePath: thumbsFilePath,fileDuration: fileDuration })
        })
        //Can this be uploaded to google storage?
        .screenshots({
            // Will take 3 screenshots
            count: 3,folder: '/thumbnails/',size: '320x240',//Names file w/o extension
            filename:'thumbnail-%b.png'
        });
});

前端视频上传

 const onDrop = (files) => {
        let formData = new FormData();
        const config = {
            header: {'content-type': 'multipart/form-data'}
        }
        console.log(files)
        formData.append("file",files[0])

        axios.post('/api/video/uploadfiles',formData,config)
            .then(response => {
                if (response.data.success) {
                    let variable = {
                        filePath: response.data.filePath,fileName: response.data.fileName
                    }
                    setFilePath(response.data.filePath)

                    //Thumbnail
                    axios.post('/api/video/thumbnail',variable)
                        .then(response => {
                            if (response.data.success) {
                                setDuration(response.data.fileDuration)
                                setThumbnail(response.data.thumbsFilePath)
                            } else {
                                alert("Failed to generate a thumbnail");
                            }
                        })

                } else {
                    alert('Failed to save video to the server')
                }
            })
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)