javascript – FFmpeg doesn’t produce a ‘progress’ event when converting to videos of certain file types

I am writing an app in electron, and one of the functionalities is that you can create a copy of the currently selected file with a different type. I do this by having a dropdown in the html that allows you to select the type. I then use a function in preload.js that calls an ffmpeg function in main.js

Preload:

const { contextBridge, ipcRenderer } = require('electron')

contextBridge.exposeInMainWorld('changeFiles', {
    //This is the function that is called
    convert: (filePath, newFormat) => ipcRenderer.send('change:file:convert-format', filePath, newFormat)
})

Main:

//electron
const { app, BrowserWindow, ipcMain} = require('electron')

//node
const path = require('path')


//ffmpeg setup
const ffmpegStatic = require('ffmpeg-static')
const ffmpeg = require('fluent-ffmpeg')
ffmpeg.setFfmpegPath(ffmpegStatic)

//imports an array that has video formats ['.mp4', '.webm', '.avi'] and so on
const { videoFormat } = require('./file formats.js')

//Preload sends to this
ipcMain.on('change:file:convert-format', (_event, filePath, newFormat) => {
    //There is an if statement to another function because I want to be able to convert images in the future
    if(videoFormats.includes(newFormat)) {
        videoConvert(filePath, newFormat, _event)
        return
    }
})

const videoConvert = (file, newFormat, _event) => {
    //file is the filepath for the file you want to change

    ffmpeg()
        .input(file)
        .saveToFile(`${path.resolve(path.dirname(file), path.parse(file).name + newFormat)}`)
        .on('progress', (progress) => {
             console.log(progress)
        })
        .on('error', (error) => {
            console.log(error)
        })
}

For some reason, when the format for the video is ‘.mp4’, the .on(‘progress’) fires, but if the format is ‘.webm’ or ‘.avi’ then it is not fired. There are also some issues I have noticed, where if these formats are chosen, then the files may partially be copied over to the new file.

I don’t know if this is because of how I am handling this, a mistake I have made in the .saveToFile(), or something else. No part of this code produces any errors anywhere, the only errors being that the progress doesn’t fire and that the files are not always copied over correctly into the new format.

What should I do?

Read more here: Source link