FFmpeg enables on-device media processing for mobile apps to compress, transcode, trim, and extract data from video or audio files without relying on external cloud services. This is critical for workflows such as preparing recorded lectures before upload, generating thumbnails, or converting formats to reduce bandwidth usage.

Flutter does not provide built-in support for such complex media operations. By integrating FFmpeg through a plugin, Dart code can invoke native FFmpeg commands, enabling efficient, offline media processing while keeping a unified Flutter codebase.

Choosing an FFmpeg Package for Flutter

The recommended plugin is ffmpeg_kit_flutter, which supports Android and iOS, offers access to FFmpeg"s core functionality, and is actively maintained. Older plugins such as flutter_ffmpeg have been deprecated and merged into this package.

The plugin uses Flutter"s platform channels to send FFmpeg command strings from Dart to the native layer, where prebuilt binaries execute them. Execution results and logs are then returned to Dart, enabling Flutter apps to use native performance within a unified codebase.

Installing and Configuring FFmpeg Kit

Add the plugin to your project in pubspec.yaml:

code
dependencies:
ffmpeg_kit_flutter: ^6.0.3

Run flutter pub get to install dependencies. Depending on your use case, packages like path_provider may also be required to manage file paths.

Banner for Media Assets

Platform-specific Setup Instructions

Android Devices:

Set the minSdkVersion to 24 or higher in android/app/build.gradle. On devices running Android below version 11, declare READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE in the manifest. From Android 11 onward, use scoped storage or the MediaStore API to comply with new storage restrictions.

iOS Devices:

Set the deployment target to iOS 12 or later (or minimum SDK version required by the plugin, often iOS 14). Update Info.plist to include permissions for camera, microphone, and file access as needed.

For example,

code
<key>NSPhotoLibraryUsageDescription</key>
<string>This app requires access to photos for media processing.</string>
<key>NSMicrophoneUsageDescription</key>
<string>This app requires microphone access for audio capture.</string>
<key>NSCameraUsageDescription</key>
<string>This app requires camera access for video recording.</string>

Ensure that your Xcode project links all necessary system frameworks (such as AVFoundation) for media handling.

Running FFmpeg Commands in Flutter

FFmpeg commands can be executed directly from Dart. Example:

code
await FFmpegKit.execute(
'-i ${inputFile.path} -vf scale=1280:720 ${outputFile.path}'
);

Handling Files and Storage Permissions

Use path_provider to obtain valid directories such as the app"s documents directory, and always provide absolute paths to FFmpeg commands to avoid resolution errors. On Android, ensure runtime permissions are requested when working with external storage. Proper file handling is necessary for FFmpeg operations to run correctly.

Asynchronous Execution and Progress Tracking

Asynchronous commands allow progress monitoring and log tracking while tasks run in the background. Example:

Example: Extract Audio from Video and Track Progress

code
FFmpegKit.executeAsync(
'-i ${input.path} -vn ${output.path}',
(session) {
print('Completed with return code ${session.getReturnCode()}');
},
(log) {
print(log.getMessage());
},
(statistics) {
print('Time: ${statistics.getTime()}');
}
);

Using asynchronous execution ensures that the application interface remains responsive while heavy operations are performed.