tanersener / flutter-ffmpeg

FFmpeg plugin for Flutter. Not maintained anymore. Superseded by FFmpegKit.
GNU Lesser General Public License v3.0
643 stars 130 forks source link
android ffmpeg flutter flutter-plugin fontconfig freetype fribidi gnutls ios libass libtheora libvorbis libvpx opencore-amr speex vp8 vp9 wavpack x264 x265

flutter_ffmpeg

GitHub release

FFmpeg plugin for Flutter. Supports iOS and Android.

Not maintained anymore, superseded by FFmpegKit. See FlutterFFmpeg to FFmpegKit Migration Guide.

1. Features

2. Installation

Add flutter_ffmpeg as a dependency in your pubspec.yaml file.

dependencies:
    flutter_ffmpeg: ^0.4.2

2.1 Packages

ffmpeg includes built-in encoders for some popular formats. However, there are certain external libraries that needs to be enabled in order to encode specific formats/codecs. For example, to encode an mp3 file you need lame or shine library enabled. You have to install a flutter_ffmpeg package that has at least one of them inside. To encode an h264 video, you need to install a package with x264 inside. To encode vp8 or vp9 videos, you need a flutter_ffmpeg package with libvpx inside.

flutter_ffmpeg provides eight packages that include different sets of external libraries. These packages are named according to the external libraries included in them. Below you can see which libraries are enabled in each package.

min min-gpl https https-gpl audio video full full-gpl
external libraries - vid.stab
x264
x265
xvidcore
gmp
gnutls
gmp
gnutls
vid.stab
x264
x265
xvidcore
lame
libilbc
libvorbis
opencore-amr
opus
shine
soxr
speex
twolame
vo-amrwbenc
wavpack
fontconfig
freetype
fribidi
kvazaar
libaom
libass
libiconv
libtheora
libvpx
libwebp
snappy
fontconfig
freetype
fribidi
gmp
gnutls
kvazaar
lame
libaom
libass
libiconv
libilbc
libtheora
libvorbis
libvpx
libwebp
libxml2
opencore-amr
opus
shine
snappy
soxr
speex
twolame
vo-amrwbenc
wavpack
fontconfig
freetype
fribidi
gmp
gnutls
kvazaar
lame
libaom
libass
libiconv
libilbc
libtheora
libvorbis
libvpx
libwebp
libxml2
opencore-amr
opus
shine
snappy
soxr
speex
twolame
vid.stab
vo-amrwbenc
wavpack
x264
x265
xvidcore
android system libraries zlib
MediaCodec
ios system libraries zlib
AudioToolbox
AVFoundation
iconv
VideoToolbox
bzip2

Installation of FlutterFFmpeg using pub enables the default package, which is based on https package. It is possible to enable other flutter_ffmpeg packages using the following steps.

2.1.1 Android
2.1.3 iOS (Flutter >= 1.20.x) && (Flutter < 2.x)
2.1.4 iOS (Flutter < 1.20.x)
2.1.5 Package Names

The following table shows all package names defined for flutter_ffmpeg.

Package Main Release LTS Release
min min min-lts
min-gpl min-gpl min-gpl-lts
https https https-lts
https-gpl https-gpl https-gpl-lts
audio audio audio-lts
video video video-lts
full full full-lts
full-gpl full-gpl full-gpl-lts

2.2 Existing Applications

It is possible to add flutter_ffmpeg to existing applications using Add-to-App guide.

Please execute the following additional steps if you are integrating into an iOS application.

  1. Go to Build Phases of Pods -> FlutterPluginRegistrant target and add all frameworks under the Pods/mobile-ffmpeg-<package name> directory to the Link Binary With Libraries section

  2. Go to Build Phases of Pods -> FlutterPluginRegistrant target and add all system libraries/frameworks listed in Step 4 of Importing-Frameworks guide to the Link Binary With Libraries section

  3. Go to Build Phases of Pods -> FlutterPluginRegistrant target and add AVFoundation system framework to the Link Binary With Libraries section

2.3 LTS Releases

flutter_ffmpeg is published in two different variants: Main Release and LTS Release. Both releases share the same source code but is built with different settings. Below you can see the differences between the two.

In order to install the LTS variant, install the https-lts package using instructions in 2.1 or append -lts to the package name you are using.

Main Release LTS Release
Android API Level 24 16
Android Camera Access Yes -
Android Architectures arm-v7a-neon
arm64-v8a
x86
x86-64
arm-v7a
arm-v7a-neon
arm64-v8a
x86
x86-64
Xcode Support 10.1 7.3.1
iOS SDK 11.0 9.3
iOS AVFoundation Yes -
iOS Architectures arm64
x86-64
x86-64-mac-catalyst
armv7
arm64
i386
x86-64

3. Using

  1. Execute synchronous FFmpeg commands.

    • Use execute() method with a single command line
      
      import 'package:flutter_ffmpeg/flutter_ffmpeg.dart';

    final FlutterFFmpeg _flutterFFmpeg = new FlutterFFmpeg();

    _flutterFFmpeg.execute("-i file1.mp4 -c:v mpeg4 file2.mp4").then((rc) => print("FFmpeg process exited with rc $rc"));

    
    - Use executeWithArguments() method with an array of arguments  
    

    import 'package:flutter_ffmpeg/flutter_ffmpeg.dart';

    final FlutterFFmpeg _flutterFFmpeg = new FlutterFFmpeg();

    var arguments = ["-i", "file1.mp4", "-c:v", "mpeg4", "file2.mp4"]; _flutterFFmpeg.executeWithArguments(arguments).then((rc) => print("FFmpeg process exited with rc $rc"));

  2. Execute asynchronous FFmpeg commands.

    _flutterFFmpeg.executeAsync(ffmpegCommand, (CompletedFFmpegExecution execution) {
      print("FFmpeg process for executionId ${execution.executionId} exited with rc ${execution.returnCode}");
    }).then((executionId) => print("Async FFmpeg process started with executionId $executionId."));
  3. Execute FFprobe commands.

    • Use execute() method with a single command line
      
      import 'package:flutter_ffmpeg/flutter_ffmpeg.dart';

    final FlutterFFprobe _flutterFFprobe = new FlutterFFprobe();

    _flutterFFprobe.execute("-i file1.mp4").then((rc) => print("FFprobe process exited with rc $rc"));

    
    - Use executeWithArguments() method with an array of arguments  
    

    import 'package:flutter_ffmpeg/flutter_ffmpeg.dart';

    final FlutterFFprobe _flutterFFprobe = new FlutterFFprobe();

    var arguments = ["-i", "file1.mp4"]; _flutterFFprobe.executeWithArguments(arguments).then((rc) => print("FFprobe process exited with rc $rc"));

  4. Check execution output. Zero represents successful execution, 255 means user cancel and non-zero values represent failure.

    
    final FlutterFFmpegConfig _flutterFFmpegConfig = new FlutterFFmpegConfig();
    
    _flutterFFmpegConfig.getLastReturnCode().then((rc) => print("Last rc: $rc"));
    
    _flutterFFmpegConfig.getLastCommandOutput().then((output) => print("Last command output: $output"));
  5. Stop ongoing FFmpeg operations. Note that these two functions do not wait for termination to complete and return immediately.

    • Stop all executions
      _flutterFFmpeg.cancel();
    • Stop a specific execution
      _flutterFFmpeg.cancelExecution(executionId);
  6. Get media information for a file.

    • Print all fields
      
      final FlutterFFprobe _flutterFFprobe = new FlutterFFprobe();

    _flutterFFprobe.getMediaInformation("").then((info) => print(info));

    - Print selected fields

    final FlutterFFprobe _flutterFFprobe = new FlutterFFprobe();

    _flutterFFprobe.getMediaInformation("").then((info) { print("Media Information");

    print("Path: ${info.getMediaProperties()['filename']}");
    print("Format: ${info.getMediaProperties()['format_name']}");
    print("Duration: ${info.getMediaProperties()['duration']}");
    print("Start time: ${info.getMediaProperties()['start_time']}");
    print("Bitrate: ${info.getMediaProperties()['bit_rate']}");
    Map<dynamic, dynamic> tags = info.getMediaProperties()['tags'];
    if (tags != null) {
        tags.forEach((key, value) {
            print("Tag: " + key + ":" + value + "\n");
        });
    }
    
    if (info.getStreams() != null) {
        List<StreamInformation> streams = info.getStreams();
    
        if (streams.length > 0) {
            for (var stream in streams) {
                print("Stream id: ${stream.getAllProperties()['index']}");
                print("Stream type: ${stream.getAllProperties()['codec_type']}");
                print("Stream codec: ${stream.getAllProperties()['codec_name']}");
                print("Stream full codec: ${stream.getAllProperties()['codec_long_name']}");
                print("Stream format: ${stream.getAllProperties()['pix_fmt']}");
                print("Stream width: ${stream.getAllProperties()['width']}");
                print("Stream height: ${stream.getAllProperties()['height']}");
                print("Stream bitrate: ${stream.getAllProperties()['bit_rate']}");
                print("Stream sample rate: ${stream.getAllProperties()['sample_rate']}");
                print("Stream sample format: ${stream.getAllProperties()['sample_fmt']}");
                print("Stream channel layout: ${stream.getAllProperties()['channel_layout']}");
                print("Stream sar: ${stream.getAllProperties()['sample_aspect_ratio']}");
                print("Stream dar: ${stream.getAllProperties()['display_aspect_ratio']}");
                print("Stream average frame rate: ${stream.getAllProperties()['avg_frame_rate']}");
                print("Stream real frame rate: ${stream.getAllProperties()['r_frame_rate']}");
                print("Stream time base: ${stream.getAllProperties()['time_base']}");
                print("Stream codec time base: ${stream.getAllProperties()['codec_time_base']}");
    
                Map<dynamic, dynamic> tags = stream.getAllProperties()['tags'];
                if (tags != null) {
                  tags.forEach((key, value) {
                    print("Stream tag: " + key + ":" + value + "\n");
                  });
                }
            }
        }
    }

    });

  7. Enable log callback and redirect all FFmpeg/FFprobe logs to a console/file/widget.

    void logCallback(Log log) {
        print("${log.executionId}:${log.message}");
    }
    ...
    _flutterFFmpegConfig.enableLogCallback(this.logCallback);
  8. Enable statistics callback and follow the progress of an ongoing FFmpeg operation.

    void statisticsCallback(Statistics statistics) {
        print("Statistics: executionId: ${statistics.executionId}, time: ${statistics.time}, size: ${statistics.size}, bitrate: ${statistics.bitrate}, speed: ${statistics.speed}, videoFrameNumber: ${statistics.videoFrameNumber}, videoQuality: ${statistics.videoQuality}, videoFps: ${statistics.videoFps}");
    }
    ...
    _flutterFFmpegConfig.enableStatisticsCallback(this.statisticsCallback);
  9. Poll statistics without implementing statistics callback.

    _flutterFFmpegConfig.getLastReceivedStatistics().then((stats) => print(stats));
  10. List ongoing executions.

    _flutterFFmpeg.listExecutions().then((ffmpegExecutions) {
      ffmpegExecutions.forEach((execution) {
        ffprint(
            "Execution id:${execution.executionId}, startTime:${execution.command}, command:${execution.startTime}.");
      });
    });
  11. Set log level.

    _flutterFFmpegConfig.setLogLevel(LogLevel.AV_LOG_WARNING);
  12. Register your own fonts by specifying a custom fonts directory, so they are available to use in FFmpeg filters. Please note that this function can not work on relative paths, you need to provide full file system path.

    _flutterFFmpegConfig.setFontDirectory("<folder with fonts>");
  13. Use your own fontconfig configuration.

    _flutterFFmpegConfig.setFontconfigConfigurationPath("<fontconfig configuration directory>");
  14. Disable log functionality of the library. Logs will not be printed to console and log callback will be disabled.

    _flutterFFmpegConfig.disableLogs();
  15. Disable statistics functionality of the library. Statistics callback will be disabled but the last received statistics data will be still available.

    _flutterFFmpegConfig.disableStatistics();
  16. Create new FFmpeg pipe.

    _flutterFFmpegConfig.registerNewFFmpegPipe().then((path) {
         then((stats) => print("New ffmpeg pipe at $path"));
    });

4. Example Application

You can see how FlutterFFmpeg is used inside an application by running example application provided under the example folder. It supports command execution, video encoding, accessing https, encoding audio, burning subtitles, video stabilisation, pipe operations and concurrent command execution.

5. Tips

6. Updates

Refer to Changelog for updates.

7. License

This project is licensed under the LGPL v3.0. However, if installation is customized to use a package with -gpl postfix (min-gpl, https-gpl, full-gpl) then FlutterFFmpeg is subject to the GPL v3.0 license.

In test application; embedded fonts are licensed under the SIL Open Font License, other digital assets are published in the public domain.

8. Patents

It is not clearly explained in their documentation but it is believed that FFmpeg, kvazaar, x264 and x265 include algorithms which are subject to software patents. If you live in a country where software algorithms are patentable then you'll probably need to pay royalty fees to patent holders. We are not lawyers though, so we recommend that you seek legal advice first. See FFmpeg Patent Mini-FAQ.

9. Contributing

Feel free to submit issues or pull requests.

Please note that master branch includes only the latest released source code. Changes planned for the next release are implemented under the development branch. Therefore, if you want to create a pull request, please open it against the development.

10. See Also