[英]How to convert image to video using fluent-ffmpeg in NodeJS
I would like to create a video using fluent-ffmpeg
in NodeJS
.我想在NodeJS
中使用fluent-ffmpeg
创建一个视频。 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! 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
和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它看起来不知何故,因为它来自我当前的项目,但如果您需要更多说明,请告诉我
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.