feat(desktop): run FFmpeg and ffprobe through bundled executables

Route every FFmpegKit and FFprobeKit use through FFmpegExecution. On
desktop it starts the ffmpeg and ffprobe executables with argument lists,
collects their logs, cancels sessions by killing the process, and parses
ffprobe JSON into the FFprobeKit MediaInformation model. Tools resolve
from MORNYE_FFMPEG_DIR, the app bundle, then PATH, and the Rust desktop
library receives the same paths.
This commit is contained in:
zarzet committed 2026-10-20 14:36:18 +07:00
1 parent 067b1b69c8
commit fb4f457430
10 files changed
+805 -82

No files matched your search

+17
View File
@@ -114,6 +114,23 @@ class FFmpegResult {
});
}
/// Outcome of an FFmpeg session started with `FFmpegExecution.start`. Logs
/// are read on demand because most callers only need the return code.
class FFmpegSessionResult {
/// Null when the session ended without a return code.
final int? returnCode;
final Future<String> Function() _logs;
FFmpegSessionResult({
required this.returnCode,
required Future<String> Function() logs,
}) : _logs = logs;
bool get success => returnCode == 0;
Future<String> logs() => _logs();
}
/// Result of an EBU R128 loudness scan, used to compute ReplayGain tags.
class ReplayGainResult {
/// Track gain in dB, e.g. "-6.50 dB".
+10 -30
View File
@@ -2,16 +2,14 @@ import 'dart:async';
import 'dart:io';
import 'dart:ui' as ui;
import 'package:ffmpeg_kit_flutter_new_audio/ffmpeg_kit.dart';
import 'package:ffmpeg_kit_flutter_new_audio/ffmpeg_session.dart';
import 'package:ffmpeg_kit_flutter_new_audio/ffprobe_kit.dart';
import 'package:ffmpeg_kit_flutter_new_audio/return_code.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:mornye/services/audio_analysis_ffmpeg.dart';
import 'package:mornye/services/audio_analysis_jobs.dart';
import 'package:mornye/services/audio_analysis_models.dart';
import 'package:mornye/services/ffmpeg_models.dart';
import 'package:mornye/services/ffmpeg_support.dart';
import 'package:mornye/services/network_audio_analysis_input.dart';
import 'package:mornye/services/platform_bridge.dart';
@@ -120,22 +118,9 @@ class _LevelMetrics {
/// and answers by throwing [AudioAnalysisCancelled].
class AudioAnalysisRunner {
final networkInput = NetworkAudioAnalysisInput();
final _analysisJobs = AudioAnalysisJobs<FFmpegSession>(
start: (arguments) async {
final completed = Completer<FFmpegSession>();
final session = await FFmpegKit.executeWithArgumentsAsync(
arguments,
completed.complete,
);
final id = session.getSessionId();
if (id == null) {
// Never pass null to cancel: FFmpegKit interprets it as cancel-all.
await completed.future;
throw StateError('Analysis session has no ID');
}
return (id: id, completed: completed.future);
},
cancel: (id) => FFmpegKit.cancel(id),
final _analysisJobs = AudioAnalysisJobs<FFmpegSessionResult>(
start: FFmpegExecution.start,
cancel: FFmpegExecution.cancel,
);
void _checkCancelled(bool Function() isCancelled) {
@@ -351,9 +336,8 @@ class AudioAnalysisRunner {
),
);
final returnCode = await session.getReturnCode();
if (!ReturnCode.isSuccess(returnCode)) {
final logs = await session.getLogsAsString();
if (!session.success) {
final logs = await session.logs();
throw Exception('FFmpeg spectrogram failed: $logs');
}
@@ -434,8 +418,7 @@ class AudioAnalysisRunner {
}
Future<_MediaInfo> _getMediaInfo(String filePath) async {
final session = await FFprobeKit.getMediaInformation(filePath);
final info = session.getMediaInformation();
final info = await FFmpegExecution.mediaInformation(filePath);
if (info == null) {
throw Exception('Failed to get media information');
@@ -615,8 +598,7 @@ class AudioAnalysisRunner {
),
);
final returnCode = await session.getReturnCode();
if (!ReturnCode.isSuccess(returnCode)) {
if (!session.success) {
return null;
}
@@ -624,9 +606,7 @@ class AudioAnalysisRunner {
? await metadataFile.readAsString()
: '';
final metadataSummary = parseAudioAnalysisMetadata(metadata);
final logs = metadataSummary == null
? await session.getAllLogsAsString() ?? ''
: '';
final logs = metadataSummary == null ? await session.logs() : '';
final summary = metadataSummary ?? parseAudioAstatsSummary(logs);
if (summary == null) return null;
final channelStats = metadataSummary?.channelStats.isNotEmpty == true
+11 -14
View File
@@ -1,13 +1,12 @@
import 'dart:async';
import 'dart:io';
import 'package:ffmpeg_kit_flutter_new_audio/ffmpeg_kit.dart';
import 'package:ffmpeg_kit_flutter_new_audio/return_code.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:mornye/services/audio_analysis_jobs.dart';
import 'package:mornye/services/automix_analysis.dart';
import 'package:mornye/services/ffmpeg_support.dart';
import 'package:mornye/services/platform_bridge.dart';
import 'package:mornye/utils/logger.dart';
@@ -19,21 +18,19 @@ class AutoMixAnalyzer {
final _cache = <String, AutoMixBeatGrid>{};
final _jobs = AudioAnalysisJobs<bool>(
start: (arguments) async {
final done = Completer<bool>();
final session = await FFmpegKit.executeWithArgumentsAsync(arguments, (
session,
) async {
final success = ReturnCode.isSuccess(await session.getReturnCode());
if (!done.isCompleted) done.complete(success);
});
final id = session.getSessionId();
if (id == null) throw StateError('AutoMix analysis session has no ID');
final session = await FFmpegExecution.start(arguments);
final id = session.id;
final timeout = Timer(const Duration(seconds: 20), () {
unawaited(FFmpegKit.cancel(id));
unawaited(FFmpegExecution.cancel(id));
});
return (id: id, completed: done.future.whenComplete(timeout.cancel));
return (
id: id,
completed: session.completed
.then((result) => result.success)
.whenComplete(timeout.cancel),
);
},
cancel: FFmpegKit.cancel,
cancel: FFmpegExecution.cancel,
);
int _generation = 0;
final _nativeRequests = <String>{};
+14 -15
View File
@@ -2,11 +2,10 @@ import 'dart:async';
import 'dart:io';
import 'dart:math';
import 'package:ffmpeg_kit_flutter_new_audio/ffmpeg_kit.dart';
import 'package:ffmpeg_kit_flutter_new_audio/return_code.dart';
import 'package:path_provider/path_provider.dart';
import 'package:mornye/models/automix_options.dart';
import 'package:mornye/services/audio_analysis_jobs.dart';
import 'package:mornye/services/ffmpeg_support.dart';
/// Keep the beginning dry for the live deck handover, then ease in the FX.
String autoMixEffectFilters(AutoMixOptions options, Duration duration) {
@@ -57,7 +56,10 @@ class AutoMixEffectRenderer {
Future<Directory> Function()? temporaryDirectory,
}) : _jobs =
jobs ??
AudioAnalysisJobs<bool>(start: _start, cancel: FFmpegKit.cancel),
AudioAnalysisJobs<bool>(
start: _start,
cancel: FFmpegExecution.cancel,
),
_temporaryDirectory = temporaryDirectory ?? getTemporaryDirectory;
final AudioAnalysisJobs<bool> _jobs;
@@ -67,21 +69,18 @@ class AutoMixEffectRenderer {
static Future<({int id, Future<bool> completed})> _start(
List<String> arguments,
) async {
final done = Completer<bool>();
final session = await FFmpegKit.executeWithArgumentsAsync(arguments, (
session,
) async {
if (!done.isCompleted) {
done.complete(ReturnCode.isSuccess(await session.getReturnCode()));
}
});
final id = session.getSessionId();
if (id == null) throw StateError('Mix effect session has no ID');
final session = await FFmpegExecution.start(arguments);
final id = session.id;
final timeout = Timer(
const Duration(seconds: 30),
() => unawaited(FFmpegKit.cancel(id)),
() => unawaited(FFmpegExecution.cancel(id)),
);
return (
id: id,
completed: session.completed
.then((result) => result.success)
.whenComplete(timeout.cancel),
);
return (id: id, completed: done.future.whenComplete(timeout.cancel));
}
void cancel() {
+9 -4
View File
@@ -6,6 +6,7 @@ import 'package:flutter/services.dart';
import 'package:mornye/services/backend/backend_channel.dart';
import 'package:mornye/services/backend/ffi_backend_transport.dart';
import 'package:mornye/services/backend/native_backend_transport.dart';
import 'package:mornye/services/ffmpeg_desktop.dart';
import 'package:path_provider/path_provider.dart';
/// Error code of calls made while the desktop library cannot be loaded.
@@ -43,23 +44,27 @@ class DesktopBackendConfig {
final String? ffmpegPath;
final String? ffprobePath;
/// Directories from path_provider. Desktop packaging passes the bundled
/// FFmpeg tool locations through [ffmpegPath] and [ffprobePath].
/// Directories from path_provider. The FFmpeg tool locations default to
/// those [DesktopFFmpegTools] resolves, so Rust and Dart run one binary.
static Future<DesktopBackendConfig> resolve({
String? ffmpegPath,
String? ffprobePath,
Future<DesktopFFmpegTools> Function()? locateTools,
}) async {
final directories = await Future.wait([
getApplicationSupportDirectory(),
getApplicationDocumentsDirectory(),
getTemporaryDirectory(),
]);
final tools = ffmpegPath != null && ffprobePath != null
? null
: await (locateTools ?? () => DesktopFFmpegTools.shared)();
return DesktopBackendConfig(
appSupportDir: directories[0].path,
documentsDir: directories[1].path,
tempDir: directories[2].path,
ffmpegPath: ffmpegPath,
ffprobePath: ffprobePath,
ffmpegPath: ffmpegPath ?? tools?.ffmpegPath,
ffprobePath: ffprobePath ?? tools?.ffprobePath,
);
}
+272
View File
@@ -0,0 +1,272 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:ffmpeg_kit_flutter_new_audio/media_information.dart';
import 'package:flutter/foundation.dart';
import 'package:mornye/models/ffmpeg_models.dart';
import 'package:mornye/utils/logger.dart';
import 'package:path/path.dart' as p;
final _log = AppLogger('FFmpeg');
/// Raised when a desktop FFmpeg tool is missing; its message is shown to
/// users as the reason a feature failed.
class FFmpegUnavailableException implements Exception {
const FFmpegUnavailableException(this.tool);
/// `ffmpeg` or `ffprobe`.
final String tool;
String get message =>
'$tool is not available. Reinstall Mornye, or set '
'${DesktopFFmpegTools.directoryVariable} to a folder that contains '
'ffmpeg and ffprobe.';
@override
String toString() => message;
}
/// Locations of the standalone FFmpeg executables shipped with desktop builds.
///
/// Lookup order for each tool:
/// 1. the folder named by `MORNYE_FFMPEG_DIR`;
/// 2. the app bundle: `Contents/Resources/ffmpeg/` then `Contents/MacOS/` on
/// macOS, `ffmpeg\` next to the executable on Windows, and `lib/ffmpeg/`
/// next to the executable on Linux (see scripts/fetch_desktop_ffmpeg.sh);
/// 3. `PATH`, a developer fallback.
@immutable
class DesktopFFmpegTools {
const DesktopFFmpegTools({this.ffmpegPath, this.ffprobePath});
static const directoryVariable = 'MORNYE_FFMPEG_DIR';
final String? ffmpegPath;
final String? ffprobePath;
static Future<DesktopFFmpegTools>? _shared;
/// The tools of this process, located once.
static Future<DesktopFFmpegTools> get shared => _shared ??= locate();
@visibleForTesting
static set debugShared(Future<DesktopFFmpegTools>? tools) => _shared = tools;
/// Folders searched for the tools, in order.
static List<String> searchDirectories({
required String operatingSystem,
required String executablePath,
required Map<String, String> environment,
}) {
final windows = operatingSystem == 'windows';
final path = windows ? p.windows : p.posix;
final executableDir = path.dirname(executablePath);
final override = environment[directoryVariable]?.trim() ?? '';
final searchPath =
environment[windows ? 'Path' : 'PATH'] ?? environment['PATH'] ?? '';
return [
if (override.isNotEmpty) override,
...switch (operatingSystem) {
'macos' => [
path.normalize(path.join(executableDir, '..', 'Resources', 'ffmpeg')),
executableDir,
],
'windows' => [path.join(executableDir, 'ffmpeg')],
_ => [path.join(executableDir, 'lib', 'ffmpeg')],
},
...searchPath
.split(windows ? ';' : ':')
.map((entry) => entry.trim())
.where((entry) => entry.isNotEmpty),
];
}
/// Resolves both tools; a missing tool stays null.
static Future<DesktopFFmpegTools> locate({
String? operatingSystem,
String? executablePath,
Map<String, String>? environment,
Future<bool> Function(String path)? isFile,
}) async {
final os = operatingSystem ?? Platform.operatingSystem;
final exists = isFile ?? FileSystemEntity.isFile;
final directories = searchDirectories(
operatingSystem: os,
executablePath: executablePath ?? Platform.resolvedExecutable,
environment: environment ?? Platform.environment,
);
final path = os == 'windows' ? p.windows : p.posix;
final suffix = os == 'windows' ? '.exe' : '';
Future<String?> find(String tool) async {
for (final directory in directories) {
final candidate = path.join(directory, '$tool$suffix');
try {
if (await exists(candidate)) return candidate;
} catch (_) {
// Unreadable PATH entries are skipped.
}
}
return null;
}
final tools = DesktopFFmpegTools(
ffmpegPath: await find('ffmpeg'),
ffprobePath: await find('ffprobe'),
);
if (tools.ffmpegPath == null || tools.ffprobePath == null) {
_log.w(
'Desktop FFmpeg tools missing (ffmpeg: ${tools.ffmpegPath}, '
'ffprobe: ${tools.ffprobePath})',
);
}
return tools;
}
}
/// Starts a tool process; injected by tests.
typedef FFmpegProcessStarter =
Future<Process> Function(String executable, List<String> arguments);
/// Builds the probe model FFprobeKit returns from `ffprobe -print_format
/// json` output. Returns null when ffprobe recognized nothing.
MediaInformation? parseFFprobeJson(String json) {
final Object? decoded;
try {
decoded = jsonDecode(json);
} on FormatException {
return null;
}
if (decoded is! Map || decoded.isEmpty) return null;
return MediaInformation(decoded);
}
/// Runs the standalone `ffmpeg`/`ffprobe` executables, mirroring the FFmpegKit
/// session semantics the app relies on: an id per session, its return code
/// and log output on completion, and cancellation by id.
class DesktopFFmpegRunner {
DesktopFFmpegRunner({
Future<DesktopFFmpegTools> Function()? tools,
FFmpegProcessStarter? startProcess,
this.killGrace = const Duration(seconds: 2),
}) : _tools = tools ?? (() => DesktopFFmpegTools.shared),
_startProcess = startProcess ?? _defaultStart;
static DesktopFFmpegRunner? _shared;
static DesktopFFmpegRunner get shared => _shared ??= DesktopFFmpegRunner();
@visibleForTesting
static set debugShared(DesktopFFmpegRunner? runner) => _shared = runner;
/// How long a cancelled process may run after SIGTERM before it is killed.
final Duration killGrace;
final Future<DesktopFFmpegTools> Function() _tools;
final FFmpegProcessStarter _startProcess;
final _processes = <int, Process>{};
int _nextId = 1;
static Future<Process> _defaultStart(
String executable,
List<String> arguments,
) => Process.start(executable, arguments);
static const _decoder = Utf8Decoder(allowMalformed: true);
Future<String> _tool(String name) async {
final tools = await _tools();
final path = name == 'ffprobe' ? tools.ffprobePath : tools.ffmpegPath;
if (path == null) throw FFmpegUnavailableException(name);
return path;
}
/// Starts `ffmpeg` with [arguments]. Throws [FFmpegUnavailableException]
/// when the executable is missing.
Future<({int id, Future<FFmpegSessionResult> completed})> start(
List<String> arguments,
) async {
final executable = await _tool('ffmpeg');
// The CLI polls stdin for interactive keys; FFmpegKit never reads it.
final process = await _startProcess(executable, [
if (!arguments.contains('-nostdin')) '-nostdin',
...arguments,
]);
final id = _nextId++;
_processes[id] = process;
unawaited(process.stdin.close().catchError((Object _) {}));
// FFmpegKit reports FFmpeg's log, which the CLI writes to stderr.
final logs = process.stderr.transform(_decoder).join();
final stdout = process.stdout.drain<void>();
final completed = () async {
try {
final code = await process.exitCode;
final output = await logs;
await stdout;
return FFmpegSessionResult(returnCode: code, logs: () async => output);
} finally {
_processes.remove(id);
}
}();
return (id: id, completed: completed);
}
/// Runs `ffmpeg` to completion; failures, including a missing executable,
/// are reported in the result.
Future<FFmpegResult> run(List<String> arguments) async {
try {
final session = await start(arguments);
final result = await session.completed;
return FFmpegResult(
success: result.success,
returnCode: result.returnCode ?? -1,
output: await result.logs(),
);
} catch (e) {
_log.e('FFmpeg process error: $e');
return FFmpegResult(success: false, returnCode: -1, output: '$e');
}
}
/// Stops the session with [id]. FFmpeg finalizes on SIGTERM; a process that
/// does not exit within [killGrace] is killed. Unknown ids are ignored.
Future<void> cancel(int id) async {
final process = _processes[id];
if (process == null) return;
process.kill();
if (Platform.isWindows) return;
unawaited(
process.exitCode.timeout(
killGrace,
onTimeout: () {
process.kill(ProcessSignal.sigkill);
return -1;
},
),
);
}
/// Probes [path] with `ffprobe`. Throws [FFmpegUnavailableException] when
/// the executable is missing; returns null when the probe fails.
Future<MediaInformation?> probe(String path) async {
final executable = await _tool('ffprobe');
final process = await _startProcess(executable, [
'-v',
'quiet',
'-print_format',
'json',
'-show_format',
'-show_streams',
'-show_chapters',
'-i',
path,
]);
unawaited(process.stdin.close().catchError((Object _) {}));
final stdout = process.stdout.transform(_decoder).join();
final stderr = process.stderr.drain<void>();
final code = await process.exitCode;
final json = await stdout;
await stderr;
if (code != 0) return null;
return parseFFprobeJson(json);
}
}
+3 -7
View File
@@ -1,6 +1,6 @@
import 'dart:io';
import 'package:ffmpeg_kit_flutter_new_audio/ffprobe_kit.dart';
import 'package:mornye/services/ffmpeg_support.dart';
import 'package:mornye/utils/logger.dart';
import 'package:mornye/utils/audio_format_utils.dart' as audio;
@@ -40,9 +40,7 @@ abstract final class FFmpegProbe {
static Future<({int width, int height})?> imageDimensions(String path) async {
try {
final info = (await FFprobeKit.getMediaInformation(
path,
)).getMediaInformation();
final info = await FFmpegExecution.mediaInformation(path);
if (info == null) return null;
for (final stream in info.getStreams()) {
final dimensions = imageDimensionsFromProperties(
@@ -89,9 +87,7 @@ abstract final class FFmpegProbe {
String path,
) async {
try {
final info = (await FFprobeKit.getMediaInformation(
path,
)).getMediaInformation();
final info = await FFmpegExecution.mediaInformation(path);
if (info == null) return const PrimaryAudioProperties();
for (final stream in info.getStreams()) {
final props = stream.getAllProperties() ?? const <String, dynamic>{};
+62
View File
@@ -1,17 +1,34 @@
import 'dart:async';
import 'dart:io';
import 'package:ffmpeg_kit_flutter_new_audio/ffmpeg_kit.dart';
import 'package:ffmpeg_kit_flutter_new_audio/ffmpeg_kit_config.dart';
import 'package:ffmpeg_kit_flutter_new_audio/ffprobe_kit.dart';
import 'package:ffmpeg_kit_flutter_new_audio/media_information.dart';
import 'package:ffmpeg_kit_flutter_new_audio/return_code.dart';
import 'package:path_provider/path_provider.dart';
import 'package:mornye/services/ffmpeg_desktop.dart';
import 'package:mornye/services/ffmpeg_models.dart';
import 'package:mornye/services/ffmpeg_probe.dart';
import 'package:mornye/utils/desktop_platform.dart';
import 'package:mornye/utils/logger.dart';
export 'package:mornye/services/ffmpeg_desktop.dart'
show FFmpegUnavailableException;
final _log = AppLogger('FFmpeg');
/// Shared native execution boundary; feature modules supply their own commands.
/// Mobile runs FFmpegKit in process; desktop runs the bundled `ffmpeg` and
/// `ffprobe` executables through [DesktopFFmpegRunner].
abstract final class FFmpegExecution {
static Future<FFmpegResult> command(String command) async {
if (isDesktopPlatform) {
// FFmpegKit's own quoting rules, so commands behave as on mobile.
return DesktopFFmpegRunner.shared.run(
FFmpegKitConfig.parseArguments(command),
);
}
try {
final session = await FFmpegKit.execute(command);
final code = await session.getReturnCode();
@@ -27,6 +44,7 @@ abstract final class FFmpegExecution {
}
static Future<FFmpegResult> arguments(List<String> arguments) async {
if (isDesktopPlatform) return DesktopFFmpegRunner.shared.run(arguments);
try {
final session = await FFmpegKit.executeWithArguments(arguments);
final code = await session.getReturnCode();
@@ -41,6 +59,50 @@ abstract final class FFmpegExecution {
}
}
/// Starts a cancellable session; [cancel] takes its id. Throws when the
/// session cannot start, including [FFmpegUnavailableException] on desktop.
static Future<({int id, Future<FFmpegSessionResult> completed})> start(
List<String> arguments,
) async {
if (isDesktopPlatform) return DesktopFFmpegRunner.shared.start(arguments);
final completed = Completer<FFmpegSessionResult>();
final session = await FFmpegKit.executeWithArgumentsAsync(arguments, (
session,
) async {
int? code;
try {
code = (await session.getReturnCode())?.getValue();
} catch (e) {
_log.w('FFmpeg return code unavailable: $e');
}
if (completed.isCompleted) return;
completed.complete(
FFmpegSessionResult(
returnCode: code,
logs: () async => await session.getAllLogsAsString() ?? '',
),
);
});
final id = session.getSessionId();
if (id == null) {
// Never pass null to cancel: FFmpegKit interprets it as cancel-all.
await completed.future;
throw StateError('FFmpeg session has no ID');
}
return (id: id, completed: completed.future);
}
static Future<void> cancel(int id) => isDesktopPlatform
? DesktopFFmpegRunner.shared.cancel(id)
: FFmpegKit.cancel(id);
/// FFprobe media information, or null when the file cannot be probed.
/// Throws [FFmpegUnavailableException] when desktop ffprobe is missing.
static Future<MediaInformation?> mediaInformation(String path) async {
if (isDesktopPlatform) return DesktopFFmpegRunner.shared.probe(path);
return (await FFprobeKit.getMediaInformation(path)).getMediaInformation();
}
static String preview(String command) {
final redacted = command
.replaceAll(
+6 -12
View File
@@ -3,10 +3,9 @@ import 'dart:convert';
import 'dart:io';
import 'package:crypto/crypto.dart';
import 'package:ffmpeg_kit_flutter_new_audio/ffmpeg_kit.dart';
import 'package:ffmpeg_kit_flutter_new_audio/return_code.dart';
import 'package:path_provider/path_provider.dart';
import 'package:mornye/services/ffmpeg_service.dart';
import 'package:mornye/services/ffmpeg_support.dart';
import 'package:mornye/services/motion_artwork_download.dart';
import 'package:mornye/services/motion_artwork_proxy.dart';
import 'package:mornye/utils/file_access.dart';
@@ -280,19 +279,14 @@ class MotionArtworkStore {
List<String> arguments, {
required Duration timeout,
}) async {
final completed = Completer<bool>();
final session = await FFmpegKit.executeWithArgumentsAsync(arguments, (
session,
) async {
final success = ReturnCode.isSuccess(await session.getReturnCode());
if (!completed.isCompleted) completed.complete(success);
});
return completed.future.timeout(
final session = await FFmpegExecution.start(arguments);
final completed = session.completed.then((result) => result.success);
return completed.timeout(
timeout,
onTimeout: () async {
await FFmpegKit.cancel(session.getSessionId());
await FFmpegExecution.cancel(session.id);
// Wait for this session to release its file before removing partials.
await completed.future.timeout(
await completed.timeout(
const Duration(seconds: 5),
onTimeout: () => false,
);
+401
View File
@@ -0,0 +1,401 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:mornye/services/ffmpeg_desktop.dart';
import 'package:mornye/services/ffmpeg_probe.dart';
import 'package:mornye/services/ffmpeg_support.dart';
import 'package:mornye/utils/desktop_platform.dart';
class _DiscardingConsumer implements StreamConsumer<List<int>> {
@override
Future<void> addStream(Stream<List<int>> stream) => stream.drain<void>();
@override
Future<void> close() async {}
}
class _FakeProcess implements Process {
_FakeProcess({String stdoutText = '', String stderrText = ''}) {
_stdout
..add(utf8.encode(stdoutText))
..close();
_stderr
..add(utf8.encode(stderrText))
..close();
}
final _stdout = StreamController<List<int>>();
final _stderr = StreamController<List<int>>();
final _exit = Completer<int>();
final kills = <ProcessSignal>[];
void finish(int code) {
if (!_exit.isCompleted) _exit.complete(code);
}
@override
Stream<List<int>> get stdout => _stdout.stream;
@override
Stream<List<int>> get stderr => _stderr.stream;
@override
IOSink get stdin => IOSink(_DiscardingConsumer());
@override
Future<int> get exitCode => _exit.future;
@override
int get pid => 42;
@override
bool kill([ProcessSignal signal = ProcessSignal.sigterm]) {
kills.add(signal);
finish(255);
return true;
}
}
class _Starts {
final calls = <(String, List<String>)>[];
final processes = <_FakeProcess>[];
_FakeProcess Function() next = _FakeProcess.new;
Future<Process> call(String executable, List<String> arguments) async {
calls.add((executable, arguments));
final process = next();
processes.add(process);
return process;
}
}
const _tools = DesktopFFmpegTools(
ffmpegPath: '/bundle/ffmpeg',
ffprobePath: '/bundle/ffprobe',
);
const _probeJson = '''
{
"streams": [
{"index": 0, "codec_name": "mjpeg", "codec_type": "video",
"width": 600, "height": 500},
{"index": 1, "codec_name": "flac", "codec_type": "audio",
"sample_fmt": "s32", "sample_rate": "96000", "channels": 2,
"bits_per_raw_sample": "24", "duration": "12.500000"}
],
"chapters": [],
"format": {"filename": "a.flac", "format_name": "flac",
"duration": "12.500000", "bit_rate": "2400000"}
}
''';
Future<void> _writeWav(File file) async {
const sampleRate = 8000;
const samples = sampleRate ~/ 4;
final data = ByteData(44 + samples * 2);
void ascii(int offset, String text) {
for (var i = 0; i < text.length; i++) {
data.setUint8(offset + i, text.codeUnitAt(i));
}
}
ascii(0, 'RIFF');
data.setUint32(4, 36 + samples * 2, Endian.little);
ascii(8, 'WAVE');
ascii(12, 'fmt ');
data.setUint32(16, 16, Endian.little);
data.setUint16(20, 1, Endian.little);
data.setUint16(22, 1, Endian.little);
data.setUint32(24, sampleRate, Endian.little);
data.setUint32(28, sampleRate * 2, Endian.little);
data.setUint16(32, 2, Endian.little);
data.setUint16(34, 16, Endian.little);
ascii(36, 'data');
data.setUint32(40, samples * 2, Endian.little);
for (var i = 0; i < samples; i++) {
data.setInt16(44 + i * 2, (i % 40 - 20) * 800, Endian.little);
}
await file.writeAsBytes(data.buffer.asUint8List());
}
void main() {
group('DesktopFFmpegTools', () {
test('searches the override, the bundle, then PATH', () {
expect(
DesktopFFmpegTools.searchDirectories(
operatingSystem: 'macos',
executablePath: '/Applications/Mornye.app/Contents/MacOS/Mornye',
environment: {'MORNYE_FFMPEG_DIR': '/opt/ff', 'PATH': '/a:/b'},
),
[
'/opt/ff',
'/Applications/Mornye.app/Contents/Resources/ffmpeg',
'/Applications/Mornye.app/Contents/MacOS',
'/a',
'/b',
],
);
expect(
DesktopFFmpegTools.searchDirectories(
operatingSystem: 'linux',
executablePath: '/opt/mornye/mornye',
environment: {'PATH': '/usr/bin'},
),
['/opt/mornye/lib/ffmpeg', '/usr/bin'],
);
expect(
DesktopFFmpegTools.searchDirectories(
operatingSystem: 'windows',
executablePath: r'C:\Program Files\Mornye\mornye.exe',
environment: {'PATH': r'C:\tools;;C:\bin'},
),
[r'C:\Program Files\Mornye\ffmpeg', r'C:\tools', r'C:\bin'],
);
});
test('takes each tool from the first folder that has it', () async {
final existing = {
'/usr/local/bin/ffmpeg',
'/opt/mornye/lib/ffmpeg/ffmpeg',
'/opt/mornye/lib/ffmpeg/ffprobe',
};
final tools = await DesktopFFmpegTools.locate(
operatingSystem: 'linux',
executablePath: '/opt/mornye/mornye',
environment: {
'MORNYE_FFMPEG_DIR': '/usr/local/bin',
'PATH': '/usr/bin',
},
isFile: (path) async => existing.contains(path),
);
expect(tools.ffmpegPath, '/usr/local/bin/ffmpeg');
expect(tools.ffprobePath, '/opt/mornye/lib/ffmpeg/ffprobe');
});
test('appends .exe on Windows and leaves missing tools null', () async {
final tools = await DesktopFFmpegTools.locate(
operatingSystem: 'windows',
executablePath: r'C:\Mornye\mornye.exe',
environment: const {},
isFile: (path) async => path == r'C:\Mornye\ffmpeg\ffmpeg.exe',
);
expect(tools.ffmpegPath, r'C:\Mornye\ffmpeg\ffmpeg.exe');
expect(tools.ffprobePath, isNull);
});
});
group('DesktopFFmpegRunner', () {
test('runs ffmpeg with an argument list and returns its log', () async {
final starts = _Starts();
final runner = DesktopFFmpegRunner(
tools: () async => _tools,
startProcess: starts.call,
);
final pending = runner.run(['-i', 'in put.flac', 'out.mp3']);
await pumpEventQueue();
starts.processes.single.finish(0);
starts.next = () =>
_FakeProcess(stderrText: 'Conversion failed!')..finish(1);
final ok = await pending;
expect(starts.calls.single.$1, '/bundle/ffmpeg');
expect(starts.calls.single.$2, [
'-nostdin',
'-i',
'in put.flac',
'out.mp3',
]);
expect(ok.success, isTrue);
expect(ok.returnCode, 0);
final failed = await runner.run(['-nostdin', '-i', 'x']);
expect(starts.calls.last.$2, ['-nostdin', '-i', 'x']);
expect(failed.success, isFalse);
expect(failed.returnCode, 1);
expect(failed.output, 'Conversion failed!');
});
test('cancels only the owned session', () async {
final starts = _Starts();
final runner = DesktopFFmpegRunner(
tools: () async => _tools,
startProcess: starts.call,
);
final first = await runner.start(['-i', 'a']);
final second = await runner.start(['-i', 'b']);
expect(first.id, isNot(second.id));
await runner.cancel(first.id);
final result = await first.completed;
expect(result.success, isFalse);
expect(starts.processes.first.kills, [ProcessSignal.sigterm]);
expect(starts.processes.last.kills, isEmpty);
await runner.cancel(first.id);
await runner.cancel(999);
expect(starts.processes.first.kills, hasLength(1));
starts.processes.last.finish(0);
expect((await second.completed).success, isTrue);
});
test('reports a missing executable clearly', () async {
final runner = DesktopFFmpegRunner(
tools: () async => const DesktopFFmpegTools(),
startProcess: (_, _) => throw StateError('must not start'),
);
await expectLater(
runner.start(['-i', 'a']),
throwsA(
isA<FFmpegUnavailableException>().having(
(e) => e.message,
'message',
contains('MORNYE_FFMPEG_DIR'),
),
),
);
await expectLater(
runner.probe('a.flac'),
throwsA(isA<FFmpegUnavailableException>()),
);
final result = await runner.run(['-i', 'a']);
expect(result.success, isFalse);
expect(result.output, contains('ffmpeg is not available'));
});
test('probes with ffprobe JSON into MediaInformation', () async {
final starts = _Starts()
..next = () => _FakeProcess(stdoutText: _probeJson)..finish(0);
final runner = DesktopFFmpegRunner(
tools: () async => _tools,
startProcess: starts.call,
);
final info = await runner.probe('/music/a.flac');
expect(starts.calls.single.$1, '/bundle/ffprobe');
expect(starts.calls.single.$2, [
'-v',
'quiet',
'-print_format',
'json',
'-show_format',
'-show_streams',
'-show_chapters',
'-i',
'/music/a.flac',
]);
expect(info!.getDuration(), '12.500000');
expect(info.getBitrate(), '2400000');
expect(info.getFormat(), 'flac');
final streams = info.getStreams();
expect(streams.map((s) => s.getType()), ['video', 'audio']);
expect(
FFmpegProbe.imageDimensionsFromProperties(
streams.first.getAllProperties()!,
),
(width: 600, height: 500),
);
expect(streams.last.getAllProperties()!['bits_per_raw_sample'], '24');
starts.next = () => _FakeProcess(stdoutText: '{}')..finish(1);
expect(await runner.probe('/music/missing.flac'), isNull);
});
test('parseFFprobeJson rejects empty or invalid output', () {
expect(parseFFprobeJson(''), isNull);
expect(parseFFprobeJson('{}'), isNull);
expect(parseFFprobeJson('[1]'), isNull);
expect(parseFFprobeJson(_probeJson)!.getStreams(), hasLength(2));
});
});
group('FFmpegExecution on desktop', () {
late _Starts starts;
setUp(() {
debugDesktopPlatformOverride = true;
starts = _Starts()..next = () => _FakeProcess()..finish(0);
DesktopFFmpegRunner.debugShared = DesktopFFmpegRunner(
tools: () async => _tools,
startProcess: starts.call,
);
});
tearDown(() {
debugDesktopPlatformOverride = null;
DesktopFFmpegRunner.debugShared = null;
});
test('splits command strings with FFmpegKit quoting', () async {
final result = await FFmpegExecution.command(
'-v error -i "/music/My Song.flac" -metadata title=\'A B\' out.mp3',
);
expect(result.success, isTrue);
expect(starts.calls.single.$2, [
'-nostdin',
'-v',
'error',
'-i',
'/music/My Song.flac',
'-metadata',
'title=A B',
'out.mp3',
]);
});
test('routes sessions and cancellation to the process runner', () async {
starts.next = _FakeProcess.new;
final session = await FFmpegExecution.start(['-i', 'a']);
await FFmpegExecution.cancel(session.id);
expect((await session.completed).success, isFalse);
expect(starts.processes.single.kills, isNotEmpty);
});
});
final hostTools = DesktopFFmpegTools.locate(
environment: {'PATH': Platform.environment['PATH'] ?? ''},
);
test(
'converts a generated WAV to FLAC and probes it with host binaries',
() async {
final tools = await hostTools;
if (tools.ffmpegPath == null || tools.ffprobePath == null) {
markTestSkipped('ffmpeg and ffprobe are not on PATH');
return;
}
debugDesktopPlatformOverride = true;
DesktopFFmpegRunner.debugShared = DesktopFFmpegRunner(
tools: () async => tools,
);
final directory = await Directory.systemTemp.createTemp('mornye-ff-');
addTearDown(() async {
debugDesktopPlatformOverride = null;
DesktopFFmpegRunner.debugShared = null;
await directory.delete(recursive: true);
});
final wav = File('${directory.path}/tone in.wav');
final flac = '${directory.path}/tone out.flac';
await _writeWav(wav);
final result = await FFmpegExecution.arguments([
'-v',
'error',
'-i',
wav.path,
'-c:a',
'flac',
flac,
]);
expect(result.success, isTrue, reason: result.output);
final properties = await FFmpegProbe.audioProperties(flac);
expect(properties.codec, 'flac');
expect(properties.sampleRate, 8000);
expect(properties.bitDepth, 16);
expect(await FFmpegProbe.isNativeFlacFile(flac), isTrue);
},
timeout: const Timeout(Duration(seconds: 60)),
);
}