|
| 1 | +import 'dart:async'; |
1 | 2 | import 'dart:developer'; |
2 | 3 | import 'dart:io'; |
3 | 4 |
|
4 | 5 | import 'package:ffmpeg_kit_flutter/ffmpeg_kit.dart'; |
5 | | -import 'package:ffmpeg_kit_flutter/ffmpeg_kit_config.dart'; |
6 | 6 | import 'package:ffmpeg_kit_flutter/ffmpeg_session.dart'; |
7 | 7 | import 'package:ffmpeg_kit_flutter/return_code.dart'; |
| 8 | +import 'package:ffmpeg_kit_flutter/session_state.dart'; |
8 | 9 | import 'package:ffmpeg_kit_flutter/statistics.dart'; |
| 10 | +import 'package:logging/logging.dart'; |
9 | 11 | import 'package:video_editor/video_editor.dart'; |
10 | 12 |
|
11 | 13 | class ExportService { |
| 14 | + static final _logger = Logger('ExportService'); |
| 15 | + |
12 | 16 | static Future<void> dispose() async { |
| 17 | + _logger.info('[FFmpeg] Disposing export service'); |
13 | 18 | final executions = await FFmpegKit.listSessions(); |
14 | | - if (executions.isNotEmpty) await FFmpegKit.cancel(); |
| 19 | + _logger.info('[FFmpeg] Found ${executions.length} active sessions'); |
| 20 | + if (executions.isNotEmpty) { |
| 21 | + _logger.info('[FFmpeg] Cancelling all sessions'); |
| 22 | + await FFmpegKit.cancel(); |
| 23 | + } |
15 | 24 | } |
16 | 25 |
|
17 | | - static Future<FFmpegSession> runFFmpegCommand( |
| 26 | + static Future<void> runFFmpegCommand( |
18 | 27 | FFmpegVideoEditorExecute execute, { |
19 | 28 | required void Function(File file) onCompleted, |
20 | 29 | void Function(Object, StackTrace)? onError, |
21 | 30 | void Function(Statistics)? onProgress, |
22 | | - }) { |
| 31 | + }) async { |
23 | 32 | log('FFmpeg start process with command = ${execute.command}'); |
24 | | - return FFmpegKit.executeAsync( |
25 | | - execute.command, |
26 | | - (session) async { |
27 | | - final state = |
28 | | - FFmpegKitConfig.sessionStateToString(await session.getState()); |
29 | | - final code = await session.getReturnCode(); |
30 | | - |
31 | | - if (ReturnCode.isSuccess(code)) { |
32 | | - onCompleted(File(execute.outputPath)); |
33 | | - } else { |
34 | | - if (onError != null) { |
35 | | - onError( |
36 | | - Exception( |
37 | | - 'FFmpeg process exited with state $state and return code $code.\n${await session.getOutput()}', |
38 | | - ), |
39 | | - StackTrace.current, |
| 33 | + |
| 34 | + final completer = Completer<void>(); |
| 35 | + FFmpegSession? activeSession; |
| 36 | + |
| 37 | + try { |
| 38 | + // Run FFmpeg with async callbacks |
| 39 | + activeSession = await FFmpegKit.executeAsync( |
| 40 | + execute.command, |
| 41 | + (session) async { |
| 42 | + // Session complete callback |
| 43 | + final returnCode = await session.getReturnCode(); |
| 44 | + final output = await session.getOutput(); |
| 45 | + |
| 46 | + if (returnCode != null && ReturnCode.isSuccess(returnCode)) { |
| 47 | + final outputFile = File(execute.outputPath); |
| 48 | + |
| 49 | + if (!outputFile.existsSync()) { |
| 50 | + _logger.warning( |
| 51 | + 'Output file does not exist at ${execute.outputPath}', |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + onCompleted(outputFile); |
| 56 | + } else { |
| 57 | + final errorCode = returnCode?.getValue() ?? -1; |
| 58 | + _logger.severe('FFmpeg process failed with return code $errorCode'); |
| 59 | + |
| 60 | + final error = Exception( |
| 61 | + 'FFmpeg process exited with return code $errorCode.\n$output', |
40 | 62 | ); |
| 63 | + |
| 64 | + if (onError != null) { |
| 65 | + onError(error, StackTrace.current); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (!completer.isCompleted) { |
| 70 | + completer.complete(); |
| 71 | + } |
| 72 | + }, |
| 73 | + null, // No log callback |
| 74 | + (statistics) { |
| 75 | + // Statistics callback for progress |
| 76 | + if (onProgress != null) { |
| 77 | + onProgress(statistics); |
| 78 | + } |
| 79 | + }, |
| 80 | + ); |
| 81 | + |
| 82 | + // Poll session state to ensure we wait for completion |
| 83 | + bool callbackTriggered = false; |
| 84 | + Timer.periodic(const Duration(milliseconds: 500), (timer) async { |
| 85 | + if (activeSession != null) { |
| 86 | + final state = await activeSession.getState(); |
| 87 | + final statisticsList = await activeSession.getStatistics(); |
| 88 | + |
| 89 | + if (statisticsList != null && |
| 90 | + statisticsList.isNotEmpty && |
| 91 | + onProgress != null) { |
| 92 | + final statistics = statisticsList.last; |
| 93 | + onProgress(statistics); |
| 94 | + } |
| 95 | + |
| 96 | + if (state == SessionState.completed) { |
| 97 | + timer.cancel(); |
| 98 | + |
| 99 | + // If callback hasn't been triggered yet, do it manually |
| 100 | + if (!callbackTriggered && !completer.isCompleted) { |
| 101 | + callbackTriggered = true; |
| 102 | + |
| 103 | + // Get the return code and handle completion |
| 104 | + final returnCode = await activeSession.getReturnCode(); |
| 105 | + |
| 106 | + if (returnCode != null && ReturnCode.isSuccess(returnCode)) { |
| 107 | + final outputFile = File(execute.outputPath); |
| 108 | + |
| 109 | + if (!outputFile.existsSync()) { |
| 110 | + _logger.warning( |
| 111 | + 'Output file does not exist at ${execute.outputPath}', |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + onCompleted(outputFile); |
| 116 | + } else { |
| 117 | + final errorCode = returnCode?.getValue() ?? -1; |
| 118 | + _logger.severe( |
| 119 | + 'FFmpeg process failed with return code $errorCode', |
| 120 | + ); |
| 121 | + |
| 122 | + final error = Exception( |
| 123 | + 'FFmpeg process exited with return code $errorCode', |
| 124 | + ); |
| 125 | + |
| 126 | + if (onError != null) { |
| 127 | + onError(error, StackTrace.current); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + completer.complete(); |
| 132 | + } |
| 133 | + } else if (state == SessionState.failed) { |
| 134 | + timer.cancel(); |
| 135 | + if (!completer.isCompleted) { |
| 136 | + final error = Exception('FFmpeg process failed'); |
| 137 | + if (onError != null) { |
| 138 | + onError(error, StackTrace.current); |
| 139 | + } |
| 140 | + completer.complete(); |
| 141 | + } |
41 | 142 | } |
42 | | - return; |
43 | 143 | } |
44 | | - }, |
45 | | - null, |
46 | | - onProgress, |
47 | | - ); |
| 144 | + }); |
| 145 | + |
| 146 | + // Wait for the session to complete |
| 147 | + await completer.future; |
| 148 | + } catch (e, stackTrace) { |
| 149 | + _logger.severe('FFmpeg execution error: $e'); |
| 150 | + if (activeSession != null) { |
| 151 | + await FFmpegKit.cancel(activeSession.getSessionId()); |
| 152 | + } |
| 153 | + if (onError != null) { |
| 154 | + onError(e, stackTrace); |
| 155 | + } else { |
| 156 | + rethrow; |
| 157 | + } |
| 158 | + } |
48 | 159 | } |
49 | 160 | } |
0 commit comments