简体   繁体   中英

How to convert image to video using fluent-ffmpeg in NodeJS

I would like to create a video using fluent-ffmpeg in NodeJS . My objective is to create a video using one image. And what I have done so far is the following:

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;

When I run this I have been getting the following result with an error:

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

Can anyone help me out:)

"dependencies": {
    "fluent-ffmpeg": "^2.1.2",
  }

And 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()
        })

}

it looks somehow because it is from my current project, but if you need more clarification let me know

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM