Search code examples
javascriptnode.jsffmpegvideo-processingfluent-ffmpeg

Convert a JPG file to MP4 video using Node


I need to convert a JPG to 3-4sec long mp4 using NodeJS. Everywhere I search I find information about ffmpeg but nothing works for me. Currently I'm trying with fluent-ffmpeg. Here is my code:

let ffmpegPath = require('@ffmpeg-installer/ffmpeg').path
let ffmpeg = require('fluent-ffmpeg')
ffmpeg.setFfmpegPath(ffmpegPath)
let command = ffmpeg()
command
  .input(imagePath)
  .inputFPS(1 / 5)
  .outputFPS(30)
  .videoCodec('libx264')
  .videoBitrate(1024)
  .size('640x?')
  .loop(5)
  .noAudio()
  .on('end', () => {
    resolve(saveTo)
  }).save(saveTo)

I'm open for other NodeJs solutions as well. I've tried VideoShow library but it throws errors when an image is being uploaded from Android Phone.


Solution

  • I found this working for me:

    let ffmpegPath = require('@ffmpeg-installer/ffmpeg').path
        let ffmpeg = require('fluent-ffmpeg')
        ffmpeg.setFfmpegPath(ffmpegPath)
        let command = ffmpeg(imagePath)
        command
          .inputFPS(1)
          .outputFPS(30)
          .videoCodec('libx264')
          .videoBitrate(1024)
          .size('640x?')
          .loop(3.5)
          .noAudio()
          .save(saveTo)