While recording a live video on a browser, have you ever wondered how to convert videos from different browsers into one standard format? There are multiple challenges during audio-video compression, conversion, editing etc., especially with respect to quality and size of the file.

FFmpeg to convert videos

FFmpeg is a cross platform solution to record, convert and stream audio and video. In Node environment, there is a package that abstracts the complex command-line usage of FFmpeg into a fluent, easy to use Node.js module.

Here is a solution to convert Webm video files to MP4 format while ensuring the quality of video is intact.

Install FFmpeg library

Download executable files or compile the source code from here and follow the guide for installation.

For macOS

brew install ffmpeg

This will install the library as well as set all the necessary paths.

Install fluent-ffmpeg

npm i fluent-ffmpeg
OR
yarn add fluent-ffmpeg

Prerequisites: FFmpeg library

File conversion Using Fluent-ffmpeg

  • Input:
    • This library supports input via stream or a saved input file via a file path. It can detect the format of input by default as well as check the frameRates of the input file.
  • Output:
    • Users can select the output format, audio video codecs, bitrates as well as some advanced options to set filters, reduce noise, aspect ratio, fps etc.

Below is an example config for MP4 output:

  • Video codec - h264
  • Bitrate - 1000bit/sec
  • Size - 640 x ? (? - height of output video will be based on the input file)

To learn more about these aspects of a video content, visit here.

      ffmpeg()
      .input("input file path")
      .videoCodec("h264_videotoolbox")
      .output("output file path")
      .videoBitrate("1000")
      .size("640x?")
      .format("mp4")

Check video Conversion Progress

There are events defined for start, progress and end of conversion in fluent-ffmpeg. For example to upload the resultant file to a destination folder/server after conversion, use

       .on("end", function () {
        // file upload Code
       }

To get current transcoding progress like current frames processed, percentage, target size etc use the ‘progress’ event. To check the percentage of conversion done

     .on('progress', function(progress) {
      console.log('Processing: ' + progress.percent + '% done');
      });

Conclusion

The fluent-ffmpeg npm package based upon FFmpeg library can be used to convert audio-video files taking various settings as input parameters. So if you stumble upon any such use case feel free to use this library.