FFmpeg Flutter

Handling Video with FFmpeg in Flutter

Video processing is one of the most resource-intensive tasks, especially in terms of RAM and CPU usage. Without proper optimization, your application can easily crash. During development, I encountered issues when using third-party libraries from Pub.dev to:

  • Convert videos into GIFs.

  • Convert a list of images into a video.

These libraries often take a long time to process and produce results that do not meet the desired quality. However, with FFmpeg, you can efficiently solve these issues.

Introduction to FFmpeg

FFmpeg is a powerful open-source library designed for audio and video processing. FFmpeg supports various tasks such as:

  • Format conversion (MP4, MKV, MP3, etc.).

  • Video and audio processing: Adding watermarks, trimming, merging, and compressing.

  • Creating GIFs from videos.

  • Streaming and media optimization.

The ffmpeg_kit_flutter_full_gpl package brings the full capabilities of FFmpeg to Flutter. Additionally, you can explore other packages at FFmpeg Kit Packages.

Installing the FFmpeg Toolkit

Note: This guide is based on Flutter 3.24.5 and ffmpeg_kit_flutter_full_gpl 6.0.3. It appears that newer versions of Flutter may not work well with this library.

Installation

Add the dependency to your pubspec.yaml file:

Run the following command:

Android Configuration

Add the following to your android/app/build.gradle file:

Update permissions in AndroidManifest.xml:

iOS Configuration

Add the following to your Info.plist file:

Ensure that your ios/Podfile specifies the platform version:

Convert a Sequence of Images to Video

Below is a function to convert a list of images into a video. My idea is to store all images in a directory, and FFmpegKit will read them to create a video.

Example Usage:

Ensure that the directory path/to/images contains images named with sequential numbers and in .png format, as illustrated in the example:

Convert a Sequence of Images to Video

The output file path/to/videoOutput.mp4 must have a unique name to avoid errors during FFmpeg processing. In the command above, the images will be automatically resized to match an even-numbered resolution. You can modify the command as needed.

Add a Watermark to a Video

Function to Add Watermark

Example Usage

  • x, y: Coordinates of the watermark.

  • width, height: Dimensions of the watermark (optional).

Reduce Video Quality

Function to Reduce Video Quality

Example Usage

  • qualityPercentage: The percentage of the original quality to retain. A lower percentage means higher compression.

  • crfValue: Controls the quality, where lower values mean better quality and higher values mean more compression.

Create GIF from Video

Function to Create GIF

Example Usage

  • fps: Frame rate of the GIF (higher values make the GIF smoother but increase file size).

  • quality: Lower values mean better quality but larger file size.

  • scale: The width of the GIF; the height is adjusted automatically to maintain the aspect ratio.

âš  Note:

  • The larger the scale, the closer the GIF quality is to the original video.

  • Be mindful of RAM usage when using this function, as GIF creation can be memory-intensive.

If you want a ready-to-use solution, try my plugin: https://pub.dev/packages/my_maker_video.

Buy Me a Coffee | Support Me on Ko-fi

Last updated