Search code examples
node.jsexpressamazon-s3ffmpegfluent-ffmpeg

Fluent ffmpeg how to user save callback


I am using fluent-ffmpeg GIT I want to further process the saved file. But save do not have any callback. how can I use the saved file with promise. My code is

ffmpeg(filename)
    .toFormat('mp3')
    .on('error', (err) => {
       console.log('An error occurred: ' + err.message);
    })
    .on('progress', (progress) => {
        console.log('Processing: ' + progress.targetSize + ' KB converted');
})
.on('end', () => {
        console.log('Processing finished !');
})
.save(`./${newname}.mp3`)

My problem is "save" function do not have a callback. so how could I save the output on S3 again?


Solution

  • save method does not need its own callback. As the documentation explains,

    save(filename): save the output to a file

    Starts ffmpeg processing and saves the output to a file.

    Note: the save() method is actually syntactic sugar for calling both output() and run().

    <...>

    The end event is emitted when processing has finished. Listeners receive ffmpeg standard output and standard error as arguments, except when generating thumbnails (see below), in which case they receive an array of the generated filenames.

    The stream can be promisified as any other stream:

    new Promise((resolve, reject) => {
        ffmpeg(filename)
        .toFormat('mp3')
        .on('error', reject)
        .on('progress', (progress) => {
            console.log('Processing: ' + progress.targetSize + ' KB converted');
        })
        .on('end', resolve)
        .save(`./${newname}.mp3`)
    });