node.js - 如何在 NodeJS 中使用 fluent-ffmpeg 将图像转换为视频

标签 node.js ffmpeg fluent-ffmpeg

我想使用 fluent-ffmpeg 创建一个视频在 NodeJS .我的目标是使用一张图片制作视频。到目前为止,我所做的如下:

var express = require('express')
var router = express.Router()

const ffmpeg = require('fluent-ffmpeg');

router.get('/test', function (req, res) {
    // make sure you set the correct path to your video file
    var proc = ffmpeg('http://localhost:3000/images/image.jpg')

        // loop for 5 seconds
        .loop(5)
        // using 25 fps
        .fps(25)
        // setup event handlers
        .on('end', function () {
            console.log('file has been converted succesfully');
        })
        .on('error', function (err) {
            console.log('an error happened: ' + err.message);
            console.log(' error code is : ' + err.code);
        })
        // save to file
        .save('http://localhost:3000/video/image-cdo.mp4');
})

module.exports = router;

当我运行它时,我得到以下结果并出现错误:
GET /mixer/ 200 6.364 ms - 13
GET /images/image.jpg 206 3.355 ms - 311484
GET /images/image.jpg 206 4.041 ms - 311484
GET /images/image.jpg 206 3.509 ms - 311484
GET /images/image.jpg 206 1.225 ms - 311484
GET /images/image.jpg 206 0.742 ms - 311484
GET /images/image.jpg 206 0.655 ms - 311484
GET /images/image.jpg 206 0.695 ms - 311484
GET /images/image.jpg 206 0.691 ms - 311484
GET /images/image.jpg 206 0.676 ms - 311484
GET /images/image.jpg 206 0.648 ms - 311484
GET /images/image.jpg 206 0.663 ms - 311484
GET /images/image.jpg 206 0.886 ms - 311484
GET /images/image.jpg 206 0.598 ms - 311484
GET /images/image.jpg 206 0.532 ms - 311484
GET /images/image.jpg 206 0.547 ms - 311484
GET /images/image.jpg 206 0.630 ms - 311484
GET /images/image.jpg 206 0.560 ms - 311484
GET /images/image.jpg 206 0.536 ms - 311484
POST /video/image-cdo.mp4 404 30.270 ms - 1212

an error happened: ffmpeg exited with code 1: Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument Error initializing output stream 0:0 -- Conversion failed!

 error code is : undefined


谁能帮我吗 :)
"dependencies": {
    "fluent-ffmpeg": "^2.1.2",
  }

和 NodeJS v12.13.0

最佳答案

module.exports = async (req, res, next) => {

   
    var images = [
        req.file.path,////repeat the number of image for the number of seconds you //want the calculation is loop multiply by number of images gives the length of the //video
        req.file.path,
        req.file.path,

    ]
    var videoOptions = {
        fps: 25,
        transition: true,
        transitionDuration: 1, // seconds
        videoBitrate: 1024,
        videoCodec: 'libx264',
        size: '640x?',
        audioBitrate: '128k',
        audioChannels: 2,
        format: 'mp4',
        pixelFormat: 'yuv420p',
        loop: 3, // seconds,
    
        
    }


    const lastDot = req.file.filename.lastIndexOf('.') just to remove the file extention for renaming
    const prev_file_folder = req.file.path
    const new_upload = Date.now() + req.file.filename.substr(0, lastDot)+".mp4"
    console.log(new_upload)
  
    videoshow(images, videoOptions)
        .save(path.join('./public/' + new_upload))
        // store_upload.single(new_upload)
        // .on('start', (command) => {
        //     console.log('ffmpeg' + command)
        // })
        // .on('error', (err) => {

        //     console.log(err)
        //     res.status(400)
        // })
        .on('end', (command) => {
            console.log(command)

            res.locals = {new_upload, prev_file_folder}
            console.log("fm")
            console.log(res.locals)
            next()
        })

}
它看起来不知何故,因为它来自我当前的项目,但如果您需要更多说明,请告诉我

关于node.js - 如何在 NodeJS 中使用 fluent-ffmpeg 将图像转换为视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58857251/

相关文章:

Ffmpeg 将视频拆分为多个 block ,但持续时间长度不同

node.js - 无法让 FFMPEG buildpack 在 Heroku Node.js 服务器中工作(使用 Fluent-FFMPEG)

javascript - 每 n 秒运行一次异步函数,如果异步函数花费的时间超过 n 秒则等待

mysql - 如何解决使用node.js向mysql插入数据时出现的错误

node.js - npm 错误! cb.apply 不是函数

ffmpeg错误: Unable to find a suitable output format for 'scale=1500:1000'

c# - 在 C# 中为字符串加上引号

video - FFMpeg 生成静态视频

javascript - 函数返回带参数的函数

javascript - fluent-ffmpeg:如何在 node.js 中将立体声 mp3 拆分为 2 个单声道文件?