| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- /*
- * Copyright (c) 2019 Taner Sener
- *
- * This file is part of FlutterFFmpeg.
- *
- * FlutterFFmpeg is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * FlutterFFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with FlutterFFmpeg. If not, see <http://www.gnu.org/licenses/>.
- */
- import 'dart:async';
- import 'package:flutter/services.dart';
- class FlutterFFmpeg {
- static const MethodChannel _methodChannel = const MethodChannel('flutter_ffmpeg');
- static const EventChannel _eventChannel = const EventChannel('flutter_ffmpeg_event');
- Function(int level, String message) logCallback;
- Function(int time, int size, double bitrate, double speed, int videoFrameNumber, double videoQuality, double videoFps) statisticsCallback;
- FlutterFFmpeg() {
- logCallback = null;
- statisticsCallback = null;
- print("Loading flutter-ffmpeg.");
- _eventChannel.receiveBroadcastStream().listen(_onEvent, onError: _onError);
- enableLogs();
- enableStatistics();
- enableRedirection();
- getPlatform().then((name) => print("Loaded flutter-ffmpeg-$name."));
- }
- void _onEvent(Object event) {
- if (event is Map<dynamic, dynamic>) {
- final Map<String, dynamic> eventMap = event.cast();
- final Map<dynamic, dynamic> logEvent = eventMap['FlutterFFmpegLogCallback'];
- final Map<dynamic, dynamic> statisticsEvent = eventMap['FlutterFFmpegStatisticsCallback'];
- if (logEvent != null) {
- int level = logEvent['level'];
- String message = logEvent['log'];
- if (this.logCallback == null) {
- if (message.length > 0) {
- // PRINT ALREADY ADDS NEW LINE. SO REMOVE THIS ONE
- if (message.endsWith('\n')) {
- print(message.substring(0, message.length - 1));
- } else {
- print(message);
- }
- }
- } else {
- this.logCallback(level, message);
- }
- }
- if (statisticsEvent != null) {
- if (this.statisticsCallback != null) {
- int time = statisticsEvent['time'];
- int size = statisticsEvent['size'];
- double bitrate = _doublePrecision(statisticsEvent['bitrate'], 2);
- double speed = _doublePrecision(statisticsEvent['speed'], 2);
- int videoFrameNumber = statisticsEvent['videoFrameNumber'];
- double videoQuality = _doublePrecision(statisticsEvent['videoQuality'], 2);
- double videoFps = _doublePrecision(statisticsEvent['videoFps'], 2);
- this.statisticsCallback(time, size, bitrate, speed, videoFrameNumber, videoQuality, videoFps);
- }
- }
- }
- }
- void _onError(Object error) {
- print('Event error: $error');
- }
- double _doublePrecision(double value, int precision) {
- if (value == null) {
- return 0;
- } else {
- return num.parse(value.toStringAsFixed(precision));
- }
- }
- /// Returns FFmpeg version bundled within the library.
- Future<String> getFFmpegVersion() async {
- try {
- final Map<dynamic, dynamic> result = await _methodChannel.invokeMethod('getFFmpegVersion');
- return result['version'];
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- return null;
- }
- }
- /// Returns platform name where library is loaded.
- Future<String> getPlatform() async {
- try {
- final Map<dynamic, dynamic> result = await _methodChannel.invokeMethod('getPlatform');
- return result['platform'];
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- return null;
- }
- }
- /// Executes FFmpeg with [commandArguments] provided.
- Future<int> executeWithArguments(List<String> arguments) async {
- try {
- final Map<dynamic, dynamic> result = await _methodChannel.invokeMethod('executeWithArguments', {'arguments': arguments});
- return result['rc'];
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- return -1;
- }
- }
- /// Executes FFmpeg [command] provided. Command is split into arguments using provided [delimiter].
- Future<int> execute(String command, [String delimiter = ' ']) async {
- try {
- final Map<dynamic, dynamic> result = await _methodChannel.invokeMethod('execute', {'command': command, 'delimiter': delimiter});
- return result['rc'];
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- return -1;
- }
- }
- /// Cancels an ongoing operation.
- Future<void> cancel() async {
- try {
- await _methodChannel.invokeMethod('cancel');
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- }
- }
- /// Enables redirection
- Future<void> enableRedirection() async {
- try {
- await _methodChannel.invokeMethod('enableRedirection');
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- }
- }
- /// Disables log and statistics redirection. By default redirection is enabled in constructor.
- /// When redirection is enabled FFmpeg logs are printed to console and can be routed further to a callback function.
- /// By disabling redirection, logs are redirected to stderr.
- /// Statistics redirection behaviour is similar. Statistics are not printed at all if redirection is not enabled.
- /// If it is enabled then it is possible to define a statistics callback function but if you don't, they are not
- /// printed anywhere and only saved as codelastReceivedStatistics data which can be polled with
- /// [getLastReceivedStatistics()].
- Future<void> disableRedirection() async {
- try {
- await _methodChannel.invokeMethod('disableRedirection');
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- }
- }
- /// Returns log level.
- Future<int> getLogLevel() async {
- try {
- final Map<dynamic, dynamic> result = await _methodChannel.invokeMethod('getLogLevel');
- return result['level'];
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- return -1;
- }
- }
- /// Sets log level.
- Future<void> setLogLevel(int logLevel) async {
- try {
- await _methodChannel.invokeMethod('setLogLevel', {'level': logLevel});
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- }
- }
- /// Enables log events
- Future<void> enableLogs() async {
- try {
- await _methodChannel.invokeMethod('enableLogs');
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- }
- }
- /// Disables log functionality of the library. Logs will not be printed to console and log callback will be disabled.
- /// Note that log functionality is enabled by default.
- Future<void> disableLogs() async {
- try {
- await _methodChannel.invokeMethod('disableLogs');
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- }
- }
- /// Enables statistics events.
- Future<void> enableStatistics() async {
- try {
- await _methodChannel.invokeMethod('enableStatistics');
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- }
- }
- /// Disables statistics functionality of the library. Statistics callback will be disabled but the last received
- /// statistics data will be still available.
- /// Note that statistics functionality is enabled by default.
- Future<void> disableStatistics() async {
- try {
- await _methodChannel.invokeMethod('disableStatistics');
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- }
- }
- /// Sets a callback to redirect FFmpeg logs. [newCallback] is a new log callback function, use null to disable a previously defined callback
- void enableLogCallback(Function(int level, String message) newCallback) {
- try {
- this.logCallback = newCallback;
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- }
- }
- /// Sets a callback to redirect FFmpeg statistics. [newCallback] is a new statistics callback function, use null to disable a previously defined callback
- void enableStatisticsCallback(Function(int time, int size, double bitrate, double speed, int videoFrameNumber, double videoQuality, double videoFps) newCallback) {
- try {
- this.statisticsCallback = newCallback;
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- }
- }
- /// Returns the last received statistics data stored in bitrate, size, speed, time, videoFps, videoFrameNumber and
- /// videoQuality fields
- Future<Map<dynamic, dynamic>> getLastReceivedStatistics() async {
- try {
- final Map<dynamic, dynamic> result = await _methodChannel.invokeMethod('getLastReceivedStatistics');
- return result;
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- return null;
- }
- }
- /// Resets last received statistics. It is recommended to call it before starting a new execution.
- Future<void> resetStatistics() async {
- try {
- await _methodChannel.invokeMethod('resetStatistics');
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- }
- }
- /// Sets and overrides fontconfig configuration directory.
- Future<void> setFontconfigConfigurationPath(String path) async {
- try {
- await _methodChannel.invokeMethod('setFontconfigConfigurationPath', {'path': path});
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- }
- }
- /// Registers fonts inside the given [fontDirectory], so they are available to use in FFmpeg filters.
- Future<void> setFontDirectory(String fontDirectory, Map<String, String> fontNameMap) async {
- var parameters;
- if (fontNameMap == null) {
- parameters = {'fontDirectory': fontDirectory};
- } else {
- parameters = {'fontDirectory': fontDirectory, 'fontNameMap': fontNameMap};
- }
- try {
- await _methodChannel.invokeMethod('setFontDirectory', parameters);
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- }
- }
- /// Returns FlutterFFmpeg package name.
- Future<String> getPackageName() async {
- try {
- final Map<dynamic, dynamic> result = await _methodChannel.invokeMethod('getPackageName');
- return result['packageName'];
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- return null;
- }
- }
- /// Returns supported external libraries.
- Future<List<dynamic>> getExternalLibraries() async {
- try {
- final List<dynamic> result = await _methodChannel.invokeMethod('getExternalLibraries');
- return result;
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- return null;
- }
- }
- /// Returns return code of last executed command.
- Future<int> getLastReturnCode() async {
- try {
- final Map<dynamic, dynamic> result = await _methodChannel.invokeMethod('getLastReturnCode');
- return result['lastRc'];
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- return -1;
- }
- }
- /// Returns log output of last executed command. Please note that disabling redirection using
- /// [disableRedirection()] method also disables this functionality.
- Future<String> getLastCommandOutput() async {
- try {
- final Map<dynamic, dynamic> result = await _methodChannel.invokeMethod('getLastCommandOutput');
- return result['lastCommandOutput'];
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- return null;
- }
- }
- /// Returns media information for given [path] using optional [timeout]
- Future<Map<dynamic, dynamic>> getMediaInformation(String path, [int timeout = 10000]) async {
- try {
- return await _methodChannel.invokeMethod('getMediaInformation', {'path': path, 'timeout': timeout});
- } on PlatformException catch (e) {
- print("Plugin error: ${e.message}");
- return null;
- }
- }
- }
|