flutter_ffmpeg.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Copyright (c) 2019 Taner Sener
  3. *
  4. * This file is part of FlutterFFmpeg.
  5. *
  6. * FlutterFFmpeg is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * FlutterFFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with FlutterFFmpeg. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. import 'dart:async';
  20. import 'package:flutter/services.dart';
  21. class FlutterFFmpeg {
  22. static const MethodChannel _methodChannel =
  23. const MethodChannel('flutter_ffmpeg');
  24. static const EventChannel _eventChannel =
  25. const EventChannel('flutter_ffmpeg_event');
  26. Function(int level, String message) logCallback;
  27. Function(
  28. int time,
  29. int size,
  30. double bitrate,
  31. double speed,
  32. int videoFrameNumber,
  33. double videoQuality,
  34. double videoFps) statisticsCallback;
  35. FlutterFFmpeg() {
  36. logCallback = null;
  37. statisticsCallback = null;
  38. print("Loading flutter-ffmpeg.");
  39. _eventChannel.receiveBroadcastStream().listen(_onEvent, onError: _onError);
  40. enableLogs();
  41. enableStatistics();
  42. enableRedirection();
  43. getPlatform().then((name) => print("Loaded flutter-ffmpeg-$name."));
  44. }
  45. void _onEvent(Object event) {
  46. if (event is Map<dynamic, dynamic>) {
  47. final Map<String, dynamic> eventMap = event.cast();
  48. final Map<dynamic, dynamic> logEvent =
  49. eventMap['FlutterFFmpegLogCallback'];
  50. final Map<dynamic, dynamic> statisticsEvent =
  51. eventMap['FlutterFFmpegStatisticsCallback'];
  52. if (logEvent != null) {
  53. int level = logEvent['level'];
  54. String message = logEvent['log'];
  55. if (this.logCallback == null) {
  56. if (message.length > 0) {
  57. // PRINT ALREADY ADDS NEW LINE. SO REMOVE THIS ONE
  58. if (message.endsWith('\n')) {
  59. print(message.substring(0, message.length - 1));
  60. } else {
  61. print(message);
  62. }
  63. }
  64. } else {
  65. this.logCallback(level, message);
  66. }
  67. }
  68. if (statisticsEvent != null) {
  69. if (this.statisticsCallback != null) {
  70. int time = statisticsEvent['time'];
  71. int size = statisticsEvent['size'];
  72. double bitrate = _doublePrecision(statisticsEvent['bitrate'], 2);
  73. double speed = _doublePrecision(statisticsEvent['speed'], 2);
  74. int videoFrameNumber = statisticsEvent['videoFrameNumber'];
  75. double videoQuality =
  76. _doublePrecision(statisticsEvent['videoQuality'], 2);
  77. double videoFps = _doublePrecision(statisticsEvent['videoFps'], 2);
  78. this.statisticsCallback(time, size, bitrate, speed, videoFrameNumber,
  79. videoQuality, videoFps);
  80. }
  81. }
  82. }
  83. }
  84. void _onError(Object error) {
  85. print('Event error: $error');
  86. }
  87. double _doublePrecision(double value, int precision) {
  88. if (value == null) {
  89. return 0;
  90. } else {
  91. return num.parse(value.toStringAsFixed(precision));
  92. }
  93. }
  94. /// Returns FFmpeg version bundled within the library.
  95. Future<String> getFFmpegVersion() async {
  96. try {
  97. final Map<dynamic, dynamic> result =
  98. await _methodChannel.invokeMethod('getFFmpegVersion');
  99. return result['version'];
  100. } on PlatformException catch (e) {
  101. print("Plugin error: ${e.message}");
  102. return null;
  103. }
  104. }
  105. /// Returns platform name where library is loaded.
  106. Future<String> getPlatform() async {
  107. try {
  108. final Map<dynamic, dynamic> result =
  109. await _methodChannel.invokeMethod('getPlatform');
  110. return result['platform'];
  111. } on PlatformException catch (e) {
  112. print("Plugin error: ${e.message}");
  113. return null;
  114. }
  115. }
  116. /// Executes FFmpeg with [commandArguments] provided.
  117. Future<int> executeWithArguments(List<String> arguments) async {
  118. try {
  119. final Map<dynamic, dynamic> result = await _methodChannel
  120. .invokeMethod('executeWithArguments', {'arguments': arguments});
  121. return result['rc'];
  122. } on PlatformException catch (e) {
  123. print("Plugin error: ${e.message}");
  124. return -1;
  125. }
  126. }
  127. /// Executes FFmpeg [command] provided.
  128. Future<int> execute(String command) async {
  129. try {
  130. final Map<dynamic, dynamic> result = await _methodChannel.invokeMethod(
  131. 'executeWithArguments', {'arguments': parseArguments(command)});
  132. return result['rc'];
  133. } on PlatformException catch (e) {
  134. print("Plugin error: ${e.message}");
  135. return -1;
  136. }
  137. }
  138. /// Cancels an ongoing operation.
  139. Future<void> cancel() async {
  140. try {
  141. await _methodChannel.invokeMethod('cancel');
  142. } on PlatformException catch (e) {
  143. print("Plugin error: ${e.message}");
  144. }
  145. }
  146. /// Enables redirection
  147. Future<void> enableRedirection() async {
  148. try {
  149. await _methodChannel.invokeMethod('enableRedirection');
  150. } on PlatformException catch (e) {
  151. print("Plugin error: ${e.message}");
  152. }
  153. }
  154. /// Disables log and statistics redirection. By default redirection is enabled in constructor.
  155. /// When redirection is enabled FFmpeg logs are printed to console and can be routed further to a callback function.
  156. /// By disabling redirection, logs are redirected to stderr.
  157. /// Statistics redirection behaviour is similar. Statistics are not printed at all if redirection is not enabled.
  158. /// If it is enabled then it is possible to define a statistics callback function but if you don't, they are not
  159. /// printed anywhere and only saved as codelastReceivedStatistics data which can be polled with
  160. /// [getLastReceivedStatistics()].
  161. Future<void> disableRedirection() async {
  162. try {
  163. await _methodChannel.invokeMethod('disableRedirection');
  164. } on PlatformException catch (e) {
  165. print("Plugin error: ${e.message}");
  166. }
  167. }
  168. /// Returns log level.
  169. Future<int> getLogLevel() async {
  170. try {
  171. final Map<dynamic, dynamic> result =
  172. await _methodChannel.invokeMethod('getLogLevel');
  173. return result['level'];
  174. } on PlatformException catch (e) {
  175. print("Plugin error: ${e.message}");
  176. return -1;
  177. }
  178. }
  179. /// Sets log level.
  180. Future<void> setLogLevel(int logLevel) async {
  181. try {
  182. await _methodChannel.invokeMethod('setLogLevel', {'level': logLevel});
  183. } on PlatformException catch (e) {
  184. print("Plugin error: ${e.message}");
  185. }
  186. }
  187. /// Enables log events
  188. Future<void> enableLogs() async {
  189. try {
  190. await _methodChannel.invokeMethod('enableLogs');
  191. } on PlatformException catch (e) {
  192. print("Plugin error: ${e.message}");
  193. }
  194. }
  195. /// Disables log functionality of the library. Logs will not be printed to console and log callback will be disabled.
  196. /// Note that log functionality is enabled by default.
  197. Future<void> disableLogs() async {
  198. try {
  199. await _methodChannel.invokeMethod('disableLogs');
  200. } on PlatformException catch (e) {
  201. print("Plugin error: ${e.message}");
  202. }
  203. }
  204. /// Enables statistics events.
  205. Future<void> enableStatistics() async {
  206. try {
  207. await _methodChannel.invokeMethod('enableStatistics');
  208. } on PlatformException catch (e) {
  209. print("Plugin error: ${e.message}");
  210. }
  211. }
  212. /// Disables statistics functionality of the library. Statistics callback will be disabled but the last received
  213. /// statistics data will be still available.
  214. /// Note that statistics functionality is enabled by default.
  215. Future<void> disableStatistics() async {
  216. try {
  217. await _methodChannel.invokeMethod('disableStatistics');
  218. } on PlatformException catch (e) {
  219. print("Plugin error: ${e.message}");
  220. }
  221. }
  222. /// Sets a callback to redirect FFmpeg logs. [newCallback] is a new log callback function, use null to disable a previously defined callback
  223. void enableLogCallback(Function(int level, String message) newCallback) {
  224. try {
  225. this.logCallback = newCallback;
  226. } on PlatformException catch (e) {
  227. print("Plugin error: ${e.message}");
  228. }
  229. }
  230. /// Sets a callback to redirect FFmpeg statistics. [newCallback] is a new statistics callback function, use null to disable a previously defined callback
  231. void enableStatisticsCallback(
  232. Function(int time, int size, double bitrate, double speed,
  233. int videoFrameNumber, double videoQuality, double videoFps)
  234. newCallback) {
  235. try {
  236. this.statisticsCallback = newCallback;
  237. } on PlatformException catch (e) {
  238. print("Plugin error: ${e.message}");
  239. }
  240. }
  241. /// Returns the last received statistics data stored in bitrate, size, speed, time, videoFps, videoFrameNumber and
  242. /// videoQuality fields
  243. Future<Map<dynamic, dynamic>> getLastReceivedStatistics() async {
  244. try {
  245. final Map<dynamic, dynamic> result =
  246. await _methodChannel.invokeMethod('getLastReceivedStatistics');
  247. return result;
  248. } on PlatformException catch (e) {
  249. print("Plugin error: ${e.message}");
  250. return null;
  251. }
  252. }
  253. /// Resets last received statistics. It is recommended to call it before starting a new execution.
  254. Future<void> resetStatistics() async {
  255. try {
  256. await _methodChannel.invokeMethod('resetStatistics');
  257. } on PlatformException catch (e) {
  258. print("Plugin error: ${e.message}");
  259. }
  260. }
  261. /// Sets and overrides fontconfig configuration directory.
  262. Future<void> setFontconfigConfigurationPath(String path) async {
  263. try {
  264. await _methodChannel
  265. .invokeMethod('setFontconfigConfigurationPath', {'path': path});
  266. } on PlatformException catch (e) {
  267. print("Plugin error: ${e.message}");
  268. }
  269. }
  270. /// Registers fonts inside the given [fontDirectory], so they are available to use in FFmpeg filters.
  271. Future<void> setFontDirectory(
  272. String fontDirectory, Map<String, String> fontNameMap) async {
  273. var parameters;
  274. if (fontNameMap == null) {
  275. parameters = {'fontDirectory': fontDirectory};
  276. } else {
  277. parameters = {'fontDirectory': fontDirectory, 'fontNameMap': fontNameMap};
  278. }
  279. try {
  280. await _methodChannel.invokeMethod('setFontDirectory', parameters);
  281. } on PlatformException catch (e) {
  282. print("Plugin error: ${e.message}");
  283. }
  284. }
  285. /// Returns FlutterFFmpeg package name.
  286. Future<String> getPackageName() async {
  287. try {
  288. final Map<dynamic, dynamic> result =
  289. await _methodChannel.invokeMethod('getPackageName');
  290. return result['packageName'];
  291. } on PlatformException catch (e) {
  292. print("Plugin error: ${e.message}");
  293. return null;
  294. }
  295. }
  296. /// Returns supported external libraries.
  297. Future<List<dynamic>> getExternalLibraries() async {
  298. try {
  299. final List<dynamic> result =
  300. await _methodChannel.invokeMethod('getExternalLibraries');
  301. return result;
  302. } on PlatformException catch (e) {
  303. print("Plugin error: ${e.message}");
  304. return null;
  305. }
  306. }
  307. /// Returns return code of last executed command.
  308. Future<int> getLastReturnCode() async {
  309. try {
  310. final Map<dynamic, dynamic> result =
  311. await _methodChannel.invokeMethod('getLastReturnCode');
  312. return result['lastRc'];
  313. } on PlatformException catch (e) {
  314. print("Plugin error: ${e.message}");
  315. return -1;
  316. }
  317. }
  318. /// Returns log output of last executed command. Please note that disabling redirection using
  319. /// [disableRedirection()] method also disables this functionality.
  320. Future<String> getLastCommandOutput() async {
  321. try {
  322. final Map<dynamic, dynamic> result =
  323. await _methodChannel.invokeMethod('getLastCommandOutput');
  324. return result['lastCommandOutput'];
  325. } on PlatformException catch (e) {
  326. print("Plugin error: ${e.message}");
  327. return null;
  328. }
  329. }
  330. /// Returns media information for given [path] using optional [timeout]
  331. Future<Map<dynamic, dynamic>> getMediaInformation(String path,
  332. [int timeout = 10000]) async {
  333. try {
  334. return await _methodChannel.invokeMethod(
  335. 'getMediaInformation', {'path': path, 'timeout': timeout});
  336. } on PlatformException catch (e) {
  337. print("Plugin error: ${e.message}");
  338. return null;
  339. }
  340. }
  341. /// Creates a new FFmpeg pipe and returns its path.
  342. Future<String> registerNewFFmpegPipe() async {
  343. try {
  344. final Map<dynamic, dynamic> result =
  345. await _methodChannel.invokeMethod('registerNewFFmpegPipe');
  346. return result['pipe'];
  347. } on PlatformException catch (e) {
  348. print("Plugin error: ${e.message}");
  349. return null;
  350. }
  351. }
  352. /// Parses the given [command] into arguments.
  353. List<String> parseArguments(String command) {
  354. List<String> argumentList = new List();
  355. StringBuffer currentArgument = new StringBuffer();
  356. bool singleQuoteStarted = false;
  357. bool doubleQuoteStarted = false;
  358. for (int i = 0; i < command.length; i++) {
  359. var previousChar;
  360. if (i > 0) {
  361. previousChar = command.codeUnitAt(i - 1);
  362. } else {
  363. previousChar = null;
  364. }
  365. var currentChar = command.codeUnitAt(i);
  366. if (currentChar == ' '.codeUnitAt(0)) {
  367. if (singleQuoteStarted || doubleQuoteStarted) {
  368. currentArgument.write(String.fromCharCode(currentChar));
  369. } else if (currentArgument.length > 0) {
  370. argumentList.add(currentArgument.toString());
  371. currentArgument = new StringBuffer();
  372. }
  373. } else if (currentChar == '\''.codeUnitAt(0) &&
  374. (previousChar == null || previousChar != '\\'.codeUnitAt(0))) {
  375. if (singleQuoteStarted) {
  376. singleQuoteStarted = false;
  377. } else if (doubleQuoteStarted) {
  378. currentArgument.write(String.fromCharCode(currentChar));
  379. } else {
  380. singleQuoteStarted = true;
  381. }
  382. } else if (currentChar == '\"'.codeUnitAt(0) &&
  383. (previousChar == null || previousChar != '\\'.codeUnitAt(0))) {
  384. if (doubleQuoteStarted) {
  385. doubleQuoteStarted = false;
  386. } else if (singleQuoteStarted) {
  387. currentArgument.write(String.fromCharCode(currentChar));
  388. } else {
  389. doubleQuoteStarted = true;
  390. }
  391. } else {
  392. currentArgument.write(String.fromCharCode(currentChar));
  393. }
  394. }
  395. if (currentArgument.length > 0) {
  396. argumentList.add(currentArgument.toString());
  397. }
  398. return argumentList;
  399. }
  400. }