Skip to content

armgit / ente

Public Imported from GitHub
0

💚 End-to-end encrypted cloud for everything.

Replace video editor dependencies with Ente implementations

Neeraj Gupta80a1ae0f23fc
diff --git a/mobile/apps/photos/integration_test/video_editor_test.dart b/mobile/apps/photos/integration_test/video_editor_test.dart
index d370e8f554..551888109a 100644
--- a/mobile/apps/photos/integration_test/video_editor_test.dart
+++ b/mobile/apps/photos/integration_test/video_editor_test.dart
@@ -20,8 +20,8 @@ import 'package:photos/services/file_magic_service.dart';
import 'package:photos/ui/tools/editor/native_video_export_service.dart';
import 'package:photos/ui/tools/editor/video_crop_util.dart';
import 'package:photos/ui/tools/editor/video_editor/crop_value.dart';
+import 'package:photos/ui/tools/editor/video_editor/ente_video_editor_controller.dart';
import 'package:shared_preferences/shared_preferences.dart';
-import 'package:video_editor/video_editor.dart';
/// Trim configuration
class TrimConfig {
@@ -492,7 +492,7 @@ Future<IterationResult> _processVideoIteration({
required String description,
required Logger logger,
}) async {
- VideoEditorController? controller;
+ EnteVideoEditorController? controller;
File? tempOutputFile;
final failures = <ValidationFailure>[];
CropCalculation? cropCalc;
@@ -500,7 +500,7 @@ Future<IterationResult> _processVideoIteration({
try {
// Step 1: Initialize video editor controller
logger.info(' → Initializing video editor controller...');
- controller = VideoEditorController.file(
+ controller = EnteVideoEditorController.file(
sourceIoFile,
minDuration: const Duration(
milliseconds: 100,
@@ -509,7 +509,7 @@ Future<IterationResult> _processVideoIteration({
await controller.initialize();
final originalDuration = controller.videoDuration;
- final originalSize = controller.video.value.size;
+ final originalSize = controller.sourceDisplaySize;
logger.info(' → Controller initialized');
logger.info(
' - Original duration: ${originalDuration.inMilliseconds}ms',
@@ -545,11 +545,7 @@ Future<IterationResult> _processVideoIteration({
' → Applying trim from ${trimmedStart.inMilliseconds}ms to ${trimmedEnd.inMilliseconds}ms...',
);
- final minTrim =
- trimmedStart.inMilliseconds / originalDuration.inMilliseconds;
- final maxTrim =
- trimmedEnd.inMilliseconds / originalDuration.inMilliseconds;
- controller.updateTrim(minTrim, maxTrim);
+ controller.updateTrim(trimmedStart, trimmedEnd);
expectedDuration = trimmedEnd - trimmedStart;
}
@@ -586,11 +582,7 @@ Future<IterationResult> _processVideoIteration({
final aspectRatio = cropValue.getFraction()?.toDouble();
if (aspectRatio != null) {
controller.preferredCropAspectRatio = aspectRatio;
- } else {
- controller.applyCacheCrop();
}
- } else {
- controller.applyCacheCrop();
}
// Calculate expected dimensions after crop
@@ -615,20 +607,20 @@ Future<IterationResult> _processVideoIteration({
// Reset rotation to 0 first
while (controller.rotation != 0) {
- controller.rotate90Degrees(RotateDirection.left);
+ controller.rotate90Degrees(VideoRotationDirection.left);
}
// Apply the desired rotation (from video_rotate_page.dart)
switch (rotateOption) {
case 90:
- controller.rotate90Degrees(RotateDirection.right);
+ controller.rotate90Degrees(VideoRotationDirection.right);
break;
case 180:
- controller.rotate90Degrees(RotateDirection.left);
- controller.rotate90Degrees(RotateDirection.left);
+ controller.rotate90Degrees(VideoRotationDirection.left);
+ controller.rotate90Degrees(VideoRotationDirection.left);
break;
case 270:
- controller.rotate90Degrees(RotateDirection.left);
+ controller.rotate90Degrees(VideoRotationDirection.left);
break;
}
@@ -649,11 +641,9 @@ Future<IterationResult> _processVideoIteration({
'export_${DateTime.now().millisecondsSinceEpoch}.mp4',
);
- // Use original NativeVideoExportService with FFmpeg fallback disabled
final exportedFile = await NativeVideoExportService.exportVideo(
controller: controller,
outputPath: outputPath,
- allowFfmpegFallback: false, // NO FFmpeg fallback for tests
onProgress: (progress) {
// Log progress at 25% intervals
if ((progress * 100).round() % 25 == 0) {
@@ -733,14 +723,12 @@ Future<IterationResult> _processVideoIteration({
// Step 8: Validate exported video
logger.info(' → Validating exported video...');
try {
- final videoInfo = await NativeVideoEditor.getVideoInfo(
+ final videoInfo = await NativeVideoEditor.inspectVideo(
exportedFile.path,
);
- final actualDuration = Duration(
- milliseconds: (videoInfo['duration'] as num?)?.toInt() ?? 0,
- );
- final actualWidth = (videoInfo['width'] as num?)?.toInt() ?? 0;
- final actualHeight = (videoInfo['height'] as num?)?.toInt() ?? 0;
+ final actualDuration = videoInfo.duration;
+ final actualWidth = videoInfo.width;
+ final actualHeight = videoInfo.height;
logger.info(
' - Actual duration: ${actualDuration.inMilliseconds}ms',
@@ -875,7 +863,7 @@ Future<IterationResult> _processVideoIteration({
}
} finally {
// Cleanup
- await controller?.dispose();
+ controller?.dispose();
if (tempOutputFile != null && tempOutputFile.existsSync()) {
try {
tempOutputFile.deleteSync();
diff --git a/mobile/apps/photos/ios/Podfile.lock b/mobile/apps/photos/ios/Podfile.lock
index ca171a3e81..a2c833719d 100644
--- a/mobile/apps/photos/ios/Podfile.lock
+++ b/mobile/apps/photos/ios/Podfile.lock
@@ -244,9 +244,6 @@ PODS:
- video_player_avfoundation (0.0.1):
- Flutter
- FlutterMacOS
- - video_thumbnail (0.0.1):
- - Flutter
- - libwebp
- wakelock_plus (0.0.1):
- Flutter
- workmanager_apple (0.0.1):
@@ -313,7 +310,6 @@ DEPENDENCIES:
- ua_client_hints (from `.symlinks/plugins/ua_client_hints/ios`)
- url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`)
- video_player_avfoundation (from `.symlinks/plugins/video_player_avfoundation/darwin`)
- - video_thumbnail (from `.symlinks/plugins/video_thumbnail/ios`)
- wakelock_plus (from `.symlinks/plugins/wakelock_plus/ios`)
- workmanager_apple (from `.symlinks/plugins/workmanager_apple/ios`)
@@ -459,8 +455,6 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/url_launcher_ios/ios"
video_player_avfoundation:
:path: ".symlinks/plugins/video_player_avfoundation/darwin"
- video_thumbnail:
- :path: ".symlinks/plugins/video_thumbnail/ios"
wakelock_plus:
:path: ".symlinks/plugins/wakelock_plus/ios"
workmanager_apple:
@@ -544,7 +538,6 @@ SPEC CHECKSUMS:
ua_client_hints: 92fe0d139619b73ec9fcb46cc7e079a26178f586
url_launcher_ios: 7a95fa5b60cc718a708b8f2966718e93db0cef1b
video_player_avfoundation: 33f12ee023e354af9d062d2c366503424a914d10
- video_thumbnail: b637e0ad5f588ca9945f6e2c927f73a69a661140
wakelock_plus: e29112ab3ef0b318e58cfa5c32326458be66b556
workmanager_apple: 904529ae31e97fc5be632cf628507652294a0778
diff --git a/mobile/apps/photos/ios/Runner.xcodeproj/project.pbxproj b/mobile/apps/photos/ios/Runner.xcodeproj/project.pbxproj
index 9e46943cf0..706f6fe155 100644
--- a/mobile/apps/photos/ios/Runner.xcodeproj/project.pbxproj
+++ b/mobile/apps/photos/ios/Runner.xcodeproj/project.pbxproj
@@ -685,7 +685,6 @@
"${BUILT_PRODUCTS_DIR}/ua_client_hints/ua_client_hints.framework",
"${BUILT_PRODUCTS_DIR}/url_launcher_ios/url_launcher_ios.framework",
"${BUILT_PRODUCTS_DIR}/video_player_avfoundation/video_player_avfoundation.framework",
- "${BUILT_PRODUCTS_DIR}/video_thumbnail/video_thumbnail.framework",
"${BUILT_PRODUCTS_DIR}/wakelock_plus/wakelock_plus.framework",
"${BUILT_PRODUCTS_DIR}/workmanager_apple/workmanager_apple.framework",
"${PODS_XCFRAMEWORKS_BUILD_DIR}/ffmpeg_kit_custom/ffmpegkit.framework/ffmpegkit",
@@ -786,7 +785,6 @@
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/ua_client_hints.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/url_launcher_ios.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/video_player_avfoundation.framework",
- "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/video_thumbnail.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/wakelock_plus.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/workmanager_apple.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/ffmpegkit.framework",
diff --git a/mobile/apps/photos/lib/module/download/thumbnail.dart b/mobile/apps/photos/lib/module/download/thumbnail.dart
index 38c7767efd..c365ef6843 100644
--- a/mobile/apps/photos/lib/module/download/thumbnail.dart
+++ b/mobile/apps/photos/lib/module/download/thumbnail.dart
@@ -9,6 +9,7 @@ import 'package:ente_pure_utils/ente_pure_utils.dart'
show deleteFileSystemEntityIfPresent, isFileSystemPathMissing;
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:logging/logging.dart';
+import 'package:native_video_editor/native_video_editor.dart';
import 'package:path/path.dart' as p;
import 'package:photo_manager/photo_manager.dart';
import 'package:photos/core/cache/thumbnail_in_memory_cache.dart';
@@ -23,7 +24,6 @@ import "package:photos/module/download/file_url.dart";
import "package:photos/services/collections_service.dart";
import "package:photos/utils/file_key.dart";
import 'package:uuid/uuid.dart';
-import 'package:video_thumbnail/video_thumbnail.dart';
final _logger = Logger("ThumbnailUtil");
final _uploadIDToDownloadItem = <int, _ThumbnailDownload>{};
@@ -224,13 +224,8 @@ Future<Uint8List?> getThumbnailFromInAppCacheFile(EnteFile file) async {
}
if (file.fileType == FileType.video) {
try {
- return await withTemporaryVideoThumbnail<Uint8List>(
- videoPath: localFile.path,
- maxWidth: thumbnailLargeSize,
- quality: 80,
- use: (thumbnailFile) async =>
- compressThumbnailToSizeLimit(await thumbnailFile.readAsBytes()),
- );
+ final result = await getVideoThumbnailFromInAppCacheFile(file);
+ return result?.data;
} catch (e) {
_logger.warning('Failed to generate video thumbnail', e);
return null;
@@ -239,12 +234,12 @@ Future<Uint8List?> getThumbnailFromInAppCacheFile(EnteFile file) async {
return compressThumbnailToSizeLimit(await localFile.readAsBytes());
}
-Future<T?> withTemporaryVideoThumbnail<T>({
- required String videoPath,
- required int quality,
- required Future<T?> Function(File thumbnailFile) use,
- int maxWidth = 0,
-}) async {
+Future<({Uint8List data, NativeVideoInfo info})?>
+getVideoThumbnailFromInAppCacheFile(EnteFile file) async {
+ final localFile = File(getSharedMediaFilePath(file));
+ if (!localFile.existsSync() || file.fileType != FileType.video) {
+ return null;
+ }
final thumbnailFile = File(
p.join(
Configuration.instance.getTempDirectory(),
@@ -252,17 +247,22 @@ Future<T?> withTemporaryVideoThumbnail<T>({
),
);
try {
- final thumbnailFilePath = await VideoThumbnail.thumbnailFile(
- video: videoPath,
- imageFormat: ImageFormat.JPEG,
- thumbnailPath: thumbnailFile.path,
- maxWidth: maxWidth,
- quality: quality,
+ final extraction = await NativeVideoEditor.extractFrame(
+ VideoFrameRequest(
+ inputPath: localFile.path,
+ outputPath: thumbnailFile.path,
+ position: Duration.zero,
+ maxWidth: thumbnailLargeSize,
+ maxHeight: thumbnailLargeSize,
+ quality: 80,
+ ),
+ );
+ return (
+ data: await compressThumbnailToSizeLimit(
+ await thumbnailFile.readAsBytes(),
+ ),
+ info: extraction.videoInfo,
);
- if (thumbnailFilePath == null) {
- return null;
- }
- return await use(File(thumbnailFilePath));
} finally {
try {
await deleteFileSystemEntityIfPresent(thumbnailFile);
diff --git a/mobile/apps/photos/lib/module/upload/upload_data.dart b/mobile/apps/photos/lib/module/upload/upload_data.dart
index 2c8861bb5f..13cf66646a 100644
--- a/mobile/apps/photos/lib/module/upload/upload_data.dart
+++ b/mobile/apps/photos/lib/module/upload/upload_data.dart
@@ -7,6 +7,7 @@ import "package:ente_pure_utils/ente_pure_utils.dart"
show deleteFileSystemEntityIfPresent;
import "package:exif_reader/exif_reader.dart";
import 'package:logging/logging.dart';
+import 'package:native_video_editor/native_video_editor.dart';
import 'package:path/path.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:photos/core/constants.dart';
@@ -312,7 +313,8 @@ Future<MediaUploadData> _getMediaUploadDataFromAppCache(EnteFile file) async {
InvalidReason.sourceFileMissing,
);
}
- thumbnailData = await _getAppCacheThumbnailForUpload(file);
+ final thumbnailResult = await _getAppCacheThumbnailForUpload(file);
+ thumbnailData = thumbnailResult.data;
final fileHash = CryptoUtil.bin2base64(await CryptoUtil.getHash(sourceFile));
({int width, int height})? dimensions;
if (file.fileType == FileType.image) {
@@ -328,13 +330,10 @@ Future<MediaUploadData> _getMediaUploadDataFromAppCache(EnteFile file) async {
}
}
}
- } else if (thumbnailData != null) {
- // The thumbnail null check ensures that video thumbnail generation worked.
- // Use it without a max dimension to obtain the video's aspect ratio.
- dimensions = await withTemporaryVideoThumbnail<({int width, int height})>(
- videoPath: localPath,
- quality: 10,
- use: (thumbnailFile) => getImageDimensions(imagePath: thumbnailFile.path),
+ } else if (thumbnailResult.videoInfo != null) {
+ dimensions = (
+ width: thumbnailResult.videoInfo!.displayWidth,
+ height: thumbnailResult.videoInfo!.displayHeight,
);
}
@@ -359,11 +358,19 @@ Future<MediaUploadData> _getMediaUploadDataFromAppCache(EnteFile file) async {
);
}
-Future<Uint8List?> _getAppCacheThumbnailForUpload(EnteFile file) async {
+Future<({Uint8List? data, NativeVideoInfo? videoInfo})>
+_getAppCacheThumbnailForUpload(EnteFile file) async {
try {
- return await getThumbnailFromInAppCacheFile(file);
+ if (file.fileType == FileType.video) {
+ final result = await getVideoThumbnailFromInAppCacheFile(file);
+ return (data: result?.data, videoInfo: result?.info);
+ }
+ return (data: await getThumbnailFromInAppCacheFile(file), videoInfo: null);
} catch (e, s) {
_logger.warning("failed to generate thumbnail", e, s);
+ if (file.fileType == FileType.video) {
+ return (data: null, videoInfo: null);
+ }
throw InvalidFileError(
"thumbnail failed for appCache fileType: ${file.fileType.toString()}",
InvalidReason.thumbnailMissing,
diff --git a/mobile/apps/photos/lib/ui/tools/editor/export_video_service.dart b/mobile/apps/photos/lib/ui/tools/editor/export_video_service.dart
index 131ef0f6f0..662e407816 100644
--- a/mobile/apps/photos/lib/ui/tools/editor/export_video_service.dart
+++ b/mobile/apps/photos/lib/ui/tools/editor/export_video_service.dart
@@ -1,216 +1,181 @@
import 'dart:async';
import 'dart:io';
+import 'dart:ui';
import 'package:ffmpeg_kit_flutter/ffmpeg_kit.dart';
-import 'package:ffmpeg_kit_flutter/ffmpeg_session.dart';
import 'package:ffmpeg_kit_flutter/return_code.dart';
-import 'package:ffmpeg_kit_flutter/session_state.dart';
import 'package:ffmpeg_kit_flutter/statistics.dart';
import 'package:logging/logging.dart';
-import 'package:video_editor/video_editor.dart';
+import 'package:photos/ui/tools/editor/video_crop_util.dart';
+import 'package:photos/ui/tools/editor/video_editor/ente_video_editor_controller.dart';
+
+class FfmpegVideoExportPlan {
+ const FfmpegVideoExportPlan({
+ required this.arguments,
+ required this.outputPath,
+ required this.duration,
+ });
+
+ final List<String> arguments;
+ final String outputPath;
+ final Duration duration;
+}
class ExportService {
static final _logger = Logger('ExportService');
- static Future<void> dispose() async {
- final executions = await FFmpegKit.listSessions();
- if (executions.isNotEmpty) {
- await FFmpegKit.cancel();
+ static FfmpegVideoExportPlan createPlan({
+ required EnteVideoEditorController controller,
+ required String outputPath,
+ }) {
+ CropCalculation? crop;
+ if (controller.minCrop != Offset.zero ||
+ controller.maxCrop != const Offset(1, 1)) {
+ crop = VideoCropUtil.calculateFileSpaceCrop(controller: controller);
}
+ final filters = buildFfmpegVideoFilters(
+ crop: crop,
+ rotation: controller.rotation,
+ );
+
+ final start = _seconds(controller.startTrim);
+ final duration = _seconds(controller.trimmedDuration);
+ final arguments = <String>[
+ '-y',
+ '-ss',
+ start,
+ '-i',
+ controller.file.path,
+ '-t',
+ duration,
+ '-map',
+ '0:v:0',
+ '-map',
+ '0:a?',
+ if (filters.isNotEmpty) ...['-vf', filters.join(',')],
+ '-c:v',
+ 'libx264',
+ '-preset',
+ 'ultrafast',
+ '-pix_fmt',
+ 'yuv420p',
+ '-c:a',
+ 'aac',
+ '-map_metadata',
+ '0',
+ '-metadata:s:v:0',
+ 'rotate=0',
+ '-movflags',
+ '+faststart',
+ outputPath,
+ ];
+ return FfmpegVideoExportPlan(
+ arguments: arguments,
+ outputPath: outputPath,
+ duration: controller.trimmedDuration,
+ );
}
- static Future<void> runFFmpegCommand(
- FFmpegVideoEditorExecute execute, {
- required void Function(File file) onCompleted,
- void Function(Object, StackTrace)? onError,
- void Function(Statistics)? onProgress,
+ static Future<File> runFFmpegCommand(
+ FfmpegVideoExportPlan plan, {
+ void Function(double)? onProgress,
}) async {
- final completer = Completer<void>();
- FFmpegSession? activeSession;
-
+ final completer = Completer<File>();
try {
- // Run FFmpeg with async callbacks
- activeSession = await FFmpegKit.executeAsync(
- execute.command,
+ await FFmpegKit.executeWithArgumentsAsync(
+ plan.arguments,
(session) async {
- // Session complete callback
- final returnCode = await session.getReturnCode();
- final output = await session.getOutput();
-
- if (returnCode != null && ReturnCode.isSuccess(returnCode)) {
- final outputFile = File(execute.outputPath);
-
- if (!outputFile.existsSync()) {
- _logger.warning(
- 'Output file does not exist at ${execute.outputPath}',
- );
+ try {
+ final returnCode = await session.getReturnCode();
+ if (returnCode != null && ReturnCode.isSuccess(returnCode)) {
+ final file = File(plan.outputPath);
+ if (!await file.exists() || await file.length() == 0) {
+ completer.completeError(
+ StateError('FFmpeg produced no output at ${plan.outputPath}'),
+ );
+ } else {
+ completer.complete(file);
+ }
+ return;
}
-
- onCompleted(outputFile);
- } else {
- final errorCode = returnCode?.getValue() ?? -1;
- _logger.severe('FFmpeg process failed with return code $errorCode');
-
- final error = Exception(
- 'FFmpeg process exited with return code $errorCode.\n$output',
+ final output = await session.getOutput();
+ completer.completeError(
+ StateError(
+ 'FFmpeg exited with ${returnCode?.getValue() ?? -1}: $output',
+ ),
);
-
- if (onError != null) {
- onError(error, StackTrace.current);
+ } catch (error, stackTrace) {
+ if (!completer.isCompleted) {
+ completer.completeError(error, stackTrace);
}
}
-
- if (!completer.isCompleted) {
- completer.complete();
- }
},
(log) {
final message = log.getMessage();
- if (message.isEmpty) {
- return;
+ if (message.isNotEmpty) {
+ _logger.fine('FFmpeg[${log.getLevel()}] $message');
}
-
- final level = log.getLevel();
- _logger.info('FFmpeg[$level] $message');
},
(statistics) {
- // Statistics callback for progress
if (onProgress != null) {
- onProgress(statistics);
+ onProgress(_progress(statistics, plan.duration));
}
},
);
-
- // Poll session state to ensure we wait for completion
- bool callbackTriggered = false;
- Timer.periodic(const Duration(milliseconds: 500), (timer) async {
- if (activeSession != null) {
- final state = await activeSession.getState();
- final statisticsList = await activeSession.getStatistics();
-
- if (statisticsList.isNotEmpty && onProgress != null) {
- final statistics = statisticsList.last;
- onProgress(statistics);
- }
-
- if (state == SessionState.completed) {
- timer.cancel();
-
- // If callback hasn't been triggered yet, do it manually
- if (!callbackTriggered && !completer.isCompleted) {
- callbackTriggered = true;
-
- // Get the return code and handle completion
- final returnCode = await activeSession.getReturnCode();
-
- if (returnCode != null && ReturnCode.isSuccess(returnCode)) {
- final outputFile = File(execute.outputPath);
-
- if (!outputFile.existsSync()) {
- _logger.warning(
- 'Output file does not exist at ${execute.outputPath}',
- );
- }
-
- onCompleted(outputFile);
- } else {
- final errorCode = returnCode?.getValue() ?? -1;
- _logger.severe(
- 'FFmpeg process failed with return code $errorCode',
- );
-
- final error = Exception(
- 'FFmpeg process exited with return code $errorCode',
- );
-
- if (onError != null) {
- onError(error, StackTrace.current);
- }
- }
-
- completer.complete();
- }
- } else if (state == SessionState.failed) {
- timer.cancel();
- if (!completer.isCompleted) {
- final error = Exception('FFmpeg process failed');
- if (onError != null) {
- onError(error, StackTrace.current);
- }
- completer.complete();
- }
- }
- }
- });
-
- // Wait for the session to complete
- await completer.future;
- } catch (e, stackTrace) {
- _logger.severe('FFmpeg execution error: $e');
- if (activeSession != null) {
- await FFmpegKit.cancel(activeSession.getSessionId());
- }
- if (onError != null) {
- onError(e, stackTrace);
- } else {
- rethrow;
+ } catch (error, stackTrace) {
+ if (!completer.isCompleted) {
+ completer.completeError(error, stackTrace);
}
}
+ return completer.future;
}
- /// Export video using FFmpeg
static Future<File> exportVideo({
- required VideoEditorController controller,
+ required EnteVideoEditorController controller,
required String outputPath,
void Function(double)? onProgress,
void Function(Object, StackTrace)? onError,
}) async {
- final config = VideoFFmpegVideoEditorConfig(
- controller,
- format: VideoExportFormat.mp4,
- commandBuilder: (config, videoPath, outputPath) {
- final List<String> filters = config.getExportFilters();
-
- final String startTrimCmd = "-ss ${controller.startTrim}";
- final String toTrimCmd = "-t ${controller.trimmedDuration}";
-
- // Use hardware acceleration if available
- String hwAccel = "";
- if (Platform.isIOS) {
- hwAccel = "-hwaccel videotoolbox";
- } else if (Platform.isAndroid) {
- hwAccel = "-hwaccel mediacodec";
- }
-
- return '$hwAccel $startTrimCmd -i $videoPath $toTrimCmd ${config.filtersCmd(filters)} -c:v libx264 -preset ultrafast -c:a aac $outputPath';
- },
- );
+ try {
+ return await runFFmpegCommand(
+ createPlan(controller: controller, outputPath: outputPath),
+ onProgress: onProgress,
+ );
+ } catch (error, stackTrace) {
+ onError?.call(error, stackTrace);
+ rethrow;
+ }
+ }
- final completer = Completer<File>();
+ static double _progress(Statistics statistics, Duration duration) {
+ if (duration.inMilliseconds <= 0) return 0;
+ return (statistics.getTime() / duration.inMilliseconds).clamp(0.0, 1.0);
+ }
- await runFFmpegCommand(
- await config.getExecuteConfig(),
- onProgress: (Statistics stats) {
- if (onProgress != null) {
- final progress = config.getFFmpegProgress(stats.getTime().toInt());
- onProgress(progress);
- }
- },
- onError: (error, stackTrace) {
- if (onError != null) {
- onError(error, stackTrace);
- }
- if (!completer.isCompleted) {
- completer.completeError(error, stackTrace);
- }
- },
- onCompleted: (File file) {
- if (!completer.isCompleted) {
- completer.complete(file);
- }
- },
- );
+ static String _seconds(Duration duration) =>
+ (duration.inMicroseconds / Duration.microsecondsPerSecond)
+ .toStringAsFixed(6);
+}
- return completer.future;
+List<String> buildFfmpegVideoFilters({
+ required CropCalculation? crop,
+ required int rotation,
+}) {
+ final filters = <String>[];
+ if (crop != null) filters.add(crop.toFFmpegFilter());
+ switch (rotation) {
+ case 0:
+ break;
+ case 90:
+ filters.add('transpose=1');
+ case 180:
+ filters.add('hflip');
+ filters.add('vflip');
+ case 270:
+ filters.add('transpose=2');
+ default:
+ throw ArgumentError.value(rotation, 'rotation');
}
+ filters.add('scale=trunc(iw/2)*2:trunc(ih/2)*2');
+ return filters;
}
diff --git a/mobile/apps/photos/lib/ui/tools/editor/native_video_export_service.dart b/mobile/apps/photos/lib/ui/tools/editor/native_video_export_service.dart
index a0cd555fcc..4aab6efcb2 100644
--- a/mobile/apps/photos/lib/ui/tools/editor/native_video_export_service.dart
+++ b/mobile/apps/photos/lib/ui/tools/editor/native_video_export_service.dart
@@ -3,72 +3,35 @@ import 'dart:io';
import 'dart:ui';
import 'package:native_video_editor/native_video_editor.dart';
-import 'package:photos/ui/tools/editor/export_video_service.dart';
import 'package:photos/ui/tools/editor/video_crop_util.dart';
-import 'package:video_editor/video_editor.dart';
+import 'package:photos/ui/tools/editor/video_editor/ente_video_editor_controller.dart';
-/// Service that uses native video editing operations when possible
-/// Falls back to FFmpeg for operations that require re-encoding
class NativeVideoExportService {
- static const Duration _nativeFallbackThreshold = Duration(seconds: 3);
-
- /// Export video using native operations
- ///
- /// [allowFfmpegFallback] - If true (default), automatically falls back to
- /// FFmpeg if native export fails quickly. Set to false to test native-only
- /// processing (useful for integration tests).
static Future<File> exportVideo({
- required VideoEditorController controller,
+ required EnteVideoEditorController controller,
required String outputPath,
void Function(double)? onProgress,
void Function(Object, StackTrace)? onError,
- bool allowFfmpegFallback = true,
}) async {
- final startTime = DateTime.now();
try {
- final inputPath = controller.file.path;
-
- // Always use the unified native operations path
final result = await _performNativeOperations(
- inputPath: inputPath,
outputPath: outputPath,
controller: controller,
onProgress: onProgress,
);
-
return File(result.outputPath);
} catch (error, stackTrace) {
- final elapsed = DateTime.now().difference(startTime);
-
- if (onError != null) {
- onError(error, stackTrace);
- }
-
- // If native export fails quickly, attempt FFmpeg fallback automatically
- // (only if fallback is enabled)
- if (allowFfmpegFallback && elapsed <= _nativeFallbackThreshold) {
- return await ExportService.exportVideo(
- controller: controller,
- outputPath: outputPath,
- onProgress: onProgress,
- onError: onError,
- );
- }
-
+ onError?.call(error, stackTrace);
rethrow;
}
}
- /// Perform native video operations
static Future<VideoEditResult> _performNativeOperations({
- required String inputPath,
required String outputPath,
- required VideoEditorController controller,
+ required EnteVideoEditorController controller,
void Function(double)? onProgress,
}) async {
- // Use the combined native path only when needed; otherwise do a fast copy.
- // Note: despite legacy comments, Android cropping is supported via Media3
- // Transformer, so we keep native for crop/trim/rotate combinations.
+ final inputPath = controller.file.path;
final needsCrop =
controller.minCrop != Offset.zero ||
controller.maxCrop != const Offset(1.0, 1.0);
@@ -77,65 +40,24 @@ class NativeVideoExportService {
if (!(needsCrop || needsRotate || needsTrim)) {
await File(inputPath).copy(outputPath);
- return VideoEditResult(
- outputPath: outputPath,
- isReEncoded: false,
- processingTime: Duration.zero,
- );
- }
-
- Duration? trimStart;
- Duration? trimEnd;
- if (needsTrim) {
- trimStart = controller.startTrim;
- trimEnd = controller.endTrim;
+ return VideoEditResult(outputPath: outputPath, isReEncoded: false);
}
- final int? rotateDegrees = needsRotate ? controller.rotation : null;
Rect? cropRect;
if (needsCrop) {
- // Always compute a display-space crop; both iOS & Android plugins
- // take display coordinates and do the right transform internally.
- final displayCrop = VideoCropUtil.calculateDisplaySpaceCropRect(
+ cropRect = VideoCropUtil.calculateDisplaySpaceCropRect(
controller: controller,
);
- if (displayCrop.width <= 0 || displayCrop.height <= 0) {
- throw ArgumentError('Invalid crop rectangle computed: $displayCrop');
- }
- cropRect = displayCrop;
}
- final result = await NativeVideoEditor.processVideo(
+ return NativeVideoEditor.processVideo(
inputPath: inputPath,
outputPath: outputPath,
- trimStart: trimStart,
- trimEnd: trimEnd,
- rotateDegrees: rotateDegrees,
+ trimStart: needsTrim ? controller.startTrim : null,
+ trimEnd: needsTrim ? controller.endTrim : null,
+ rotateDegrees: needsRotate ? controller.rotation : null,
cropRect: cropRect,
onProgress: onProgress,
);
- return result;
}
-
- /// Get video information using native methods
- static Future<Map<String, dynamic>> getVideoInfo(String videoPath) async {
- try {
- return await NativeVideoEditor.getVideoInfo(videoPath);
- } catch (e) {
- // Fallback to MediaInfo or FFprobe if needed
- return {};
- }
- }
-
- /// Cancel any ongoing export operation
- static Future<void> cancelExport() async {
- try {
- // Cancel native operation
- await NativeVideoEditor.cancelProcessing();
- } catch (e) {
- // Silently fail
- }
- }
-
- // No extra helpers needed; crop mapping is centralized in VideoCropUtil
}
diff --git a/mobile/apps/photos/lib/ui/tools/editor/video_crop_page.dart b/mobile/apps/photos/lib/ui/tools/editor/video_crop_page.dart
index 3a1abd02c1..97a4768925 100644
--- a/mobile/apps/photos/lib/ui/tools/editor/video_crop_page.dart
+++ b/mobile/apps/photos/lib/ui/tools/editor/video_crop_page.dart
@@ -1,17 +1,14 @@
-import "package:ente_strings/ente_strings.dart";
import 'package:flutter/material.dart';
-import "package:photos/theme/ente_theme.dart";
import "package:photos/ui/tools/editor/video_editor/crop_value.dart";
-import "package:photos/ui/tools/editor/video_editor/video_editor_app_bar.dart";
+import "package:photos/ui/tools/editor/video_editor/ente_video_editor_controller.dart";
+import "package:photos/ui/tools/editor/video_editor/ente_video_editor_widgets.dart";
import "package:photos/ui/tools/editor/video_editor/video_editor_bottom_action.dart";
import "package:photos/ui/tools/editor/video_editor/video_editor_main_actions.dart";
-import "package:photos/ui/tools/editor/video_editor/video_editor_player_control.dart";
-import 'package:video_editor/video_editor.dart';
class VideoCropPage extends StatefulWidget {
const VideoCropPage({super.key, required this.controller});
- final VideoEditorController controller;
+ final EnteVideoEditorController controller;
@override
State<VideoCropPage> createState() => _VideoCropPageState();
@@ -33,10 +30,6 @@ class _VideoCropPageState extends State<VideoCropPage> {
return;
}
- // Check if we need to account for rotation
- final rotation = widget.controller.rotation;
- final isRotated = rotation % 180 != 0;
-
// Find which crop value matches the current ratio
for (final value in CropValue.values) {
if (value == CropValue.original || value == CropValue.free) continue;
@@ -44,14 +37,7 @@ class _VideoCropPageState extends State<VideoCropPage> {
final valueRatio = value.getFraction()?.toDouble();
if (valueRatio == null) continue;
- // For rotated videos, check both the normal and swapped ratios
- if (isRotated && value != CropValue.ratio_1_1) {
- final swappedRatio = 1.0 / valueRatio;
- if ((currentRatio - swappedRatio).abs() < 0.01) {
- _selectedCropValue = value;
- return;
- }
- } else if ((currentRatio - valueRatio).abs() < 0.01) {
+ if ((currentRatio - valueRatio).abs() < 0.01) {
_selectedCropValue = value;
return;
}
@@ -60,71 +46,25 @@ class _VideoCropPageState extends State<VideoCropPage> {
@override
Widget build(BuildContext context) {
- final colorScheme = getEnteColorScheme(context);
- return Scaffold(
- backgroundColor: colorScheme.backgroundColour,
- appBar: VideoEditorAppBar(
- onCancel: () => Navigator.pop(context),
- primaryActionLabel: context.strings.done,
- onPrimaryAction: () {
- widget.controller.applyCacheCrop();
- Navigator.pop(context);
- },
- ),
- body: SafeArea(
- top: false,
- bottom: true,
- child: Padding(
- padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: [
- Expanded(
- child: Stack(
- fit: StackFit.expand,
- children: [
- Positioned.fill(
- child: Hero(
- tag: "video-editor-preview",
- child: CropGridViewer.edit(
- controller: widget.controller,
- rotateCropArea: true,
- margin: const EdgeInsets.symmetric(horizontal: 12),
- ),
- ),
- ),
- Align(
- alignment: Alignment.bottomCenter,
- child: Padding(
- padding: const EdgeInsets.only(bottom: 20),
- child: VideoEditorPlayerControl(
- controller: widget.controller,
- ),
- ),
- ),
- ],
- ),
- ),
- AnimatedBuilder(
- animation: widget.controller,
- builder: (_, _) => VideoEditorMainActions(
- children: [
- _buildCropButton(context, CropValue.free),
- const SizedBox(width: 24),
- _buildCropButton(context, CropValue.ratio_1_1),
- const SizedBox(width: 24),
- _buildCropButton(context, CropValue.ratio_9_16),
- const SizedBox(width: 24),
- _buildCropButton(context, CropValue.ratio_16_9),
- const SizedBox(width: 24),
- _buildCropButton(context, CropValue.ratio_3_4),
- const SizedBox(width: 24),
- _buildCropButton(context, CropValue.ratio_4_3),
- ],
- ),
- ),
- ],
- ),
+ return EnteVideoEditorSubPage(
+ controller: widget.controller,
+ preview: EnteVideoCropEditor(controller: widget.controller),
+ actions: AnimatedBuilder(
+ animation: widget.controller,
+ builder: (_, _) => VideoEditorMainActions(
+ children: [
+ _buildCropButton(context, CropValue.free),
+ const SizedBox(width: 24),
+ _buildCropButton(context, CropValue.ratio_1_1),
+ const SizedBox(width: 24),
+ _buildCropButton(context, CropValue.ratio_9_16),
+ const SizedBox(width: 24),
+ _buildCropButton(context, CropValue.ratio_16_9),
+ const SizedBox(width: 24),
+ _buildCropButton(context, CropValue.ratio_3_4),
+ const SizedBox(width: 24),
+ _buildCropButton(context, CropValue.ratio_4_3),
+ ],
),
),
);
@@ -133,7 +73,6 @@ class _VideoCropPageState extends State<VideoCropPage> {
Widget _buildCropButton(BuildContext context, CropValue value) {
final aspectRatio = value.getFraction()?.toDouble();
- // Check if this button is selected
final isSelected = _selectedCropValue == value;
return VideoEditorBottomAction(
@@ -142,7 +81,7 @@ class _VideoCropPageState extends State<VideoCropPage> {
onPressed: () {
if (value == CropValue.original) {
widget.controller.updateCrop(Offset.zero, const Offset(1.0, 1.0));
- widget.controller.cropAspectRatio(null);
+ widget.controller.preferredCropAspectRatio = null;
_selectedCropValue = null;
setState(() {});
} else if (value == CropValue.free) {
@@ -152,14 +91,7 @@ class _VideoCropPageState extends State<VideoCropPage> {
} else if (aspectRatio != null) {
_selectedCropValue = value;
- // Store the visual aspect ratio, accounting for rotation
- final rotation = widget.controller.rotation;
- final isRotated = rotation % 180 != 0;
- final ratioToStore = (isRotated && value != CropValue.ratio_1_1)
- ? 1.0 / aspectRatio
- : aspectRatio;
-
- widget.controller.preferredCropAspectRatio = ratioToStore;
+ widget.controller.preferredCropAspectRatio = aspectRatio;
setState(() {});
}
},
diff --git a/mobile/apps/photos/lib/ui/tools/editor/video_crop_util.dart b/mobile/apps/photos/lib/ui/tools/editor/video_crop_util.dart
index f7829d69dc..5fe445c988 100644
--- a/mobile/apps/photos/lib/ui/tools/editor/video_crop_util.dart
+++ b/mobile/apps/photos/lib/ui/tools/editor/video_crop_util.dart
@@ -1,28 +1,19 @@
import 'dart:ui';
import 'package:flutter/foundation.dart' show visibleForTesting;
-import 'package:video_editor/video_editor.dart';
+import 'package:photos/ui/tools/editor/video_editor/ente_video_editor_controller.dart';
-/// Crop calculation result
class CropCalculation {
- final int x;
- final int y;
- final int width;
- final int height;
-
- CropCalculation({
+ const CropCalculation({
required this.x,
required this.y,
required this.width,
required this.height,
});
- /// Create Rect from crop calculation
- Rect toRect() => Rect.fromLTWH(
- x.toDouble(),
- y.toDouble(),
- width.toDouble(),
- height.toDouble(),
- );
+ final int x;
+ final int y;
+ final int width;
+ final int height;
/// FFmpeg crop filter string: crop=w:h:x:y
String toFFmpegFilter() => 'crop=$width:$height:$x:$y';
@@ -42,22 +33,16 @@ class VideoCropUtil {
return value.clamp(0.0, 1.0);
}
- static int _clampInt(int value, int min, int max) {
- if (value < min) return min;
- if (value > max) return max;
- return value;
- }
-
/// Calculate the crop rectangle in display-space pixels.
///
/// Returns a Rect in display-space so the native plugins can transform to file-space.
static Rect calculateDisplaySpaceCropRect({
- required VideoEditorController controller,
+ required EnteVideoEditorController controller,
}) {
return calculateDisplaySpaceCropRectFromData(
minCrop: controller.minCrop,
maxCrop: controller.maxCrop,
- videoSize: controller.video.value.size,
+ videoSize: controller.sourceDisplaySize,
);
}
@@ -69,7 +54,6 @@ class VideoCropUtil {
required Offset maxCrop,
required Size videoSize,
}) {
- // Both platforms use same path - no rotation-specific logic
double minX = _clampNormalized(minCrop.dx);
double maxX = _clampNormalized(maxCrop.dx);
double minY = _clampNormalized(minCrop.dy);
@@ -86,10 +70,6 @@ class VideoCropUtil {
maxY = temp;
}
- // Use raw video dimensions - no special handling for rotation
- final displayWidth = videoSize.width;
- final displayHeight = videoSize.height;
-
final widthNormalized = maxX - minX;
final heightNormalized = maxY - minY;
@@ -97,84 +77,43 @@ class VideoCropUtil {
throw VideoCropException('Invalid crop selection: zero or negative span');
}
- final x = minX * displayWidth;
- final y = minY * displayHeight;
- final w = widthNormalized * displayWidth;
- final h = heightNormalized * displayHeight;
+ final sourceWidth = videoSize.width.round();
+ final sourceHeight = videoSize.height.round();
+ final x = (minX * sourceWidth).round().clamp(0, sourceWidth);
+ final y = (minY * sourceHeight).round().clamp(0, sourceHeight);
+ var right = (maxX * sourceWidth).round().clamp(x, sourceWidth);
+ var bottom = (maxY * sourceHeight).round().clamp(y, sourceHeight);
+ if ((right - x).isOdd) {
+ right += right < sourceWidth ? 1 : -1;
+ }
+ if ((bottom - y).isOdd) {
+ bottom += bottom < sourceHeight ? 1 : -1;
+ }
+ final w = right - x;
+ final h = bottom - y;
if (w <= 0 || h <= 0) {
throw VideoCropException('Invalid crop rectangle after scaling');
}
- return Rect.fromLTWH(x, y, w, h);
+ return Rect.fromLTWH(
+ x.toDouble(),
+ y.toDouble(),
+ w.toDouble(),
+ h.toDouble(),
+ );
}
/// Convert the normalised crop selection into file-space coordinates.
static CropCalculation calculateFileSpaceCrop({
- required VideoEditorController controller,
+ required EnteVideoEditorController controller,
}) {
- final videoSize = controller.video.value.size;
- return calculateFileSpaceCropFromData(
- minCrop: controller.minCrop,
- maxCrop: controller.maxCrop,
- videoSize: videoSize,
+ final crop = calculateDisplaySpaceCropRect(controller: controller);
+ return CropCalculation(
+ x: crop.left.toInt(),
+ y: crop.top.toInt(),
+ width: crop.width.toInt(),
+ height: crop.height.toInt(),
);
}
-
- /// Testability helper: skips controller/platform usage to make unit tests
- /// deterministic. Not for production callers.
- @visibleForTesting
- static CropCalculation calculateFileSpaceCropFromData({
- required Offset minCrop,
- required Offset maxCrop,
- required Size videoSize,
- }) {
- final displayCrop = calculateDisplaySpaceCropRectFromData(
- minCrop: minCrop,
- maxCrop: maxCrop,
- videoSize: videoSize,
- );
-
- // Both iOS and Android produce displayCrop in raw file dimensions,
- // so we use standard file-space crop for both
- return _calculateStandardFileSpaceCrop(videoSize, displayCrop);
- }
-
- static CropCalculation _calculateStandardFileSpaceCrop(
- Size videoSize,
- Rect displayCrop,
- ) {
- final minX = _clampInt(
- displayCrop.left.round(),
- 0,
- videoSize.width.toInt(),
- );
- final minY = _clampInt(
- displayCrop.top.round(),
- 0,
- videoSize.height.toInt(),
- );
- final maxX = _clampInt(
- displayCrop.right.round(),
- 0,
- videoSize.width.toInt(),
- );
- final maxY = _clampInt(
- displayCrop.bottom.round(),
- 0,
- videoSize.height.toInt(),
- );
-
- final w = maxX - minX;
- final h = maxY - minY;
-
- // Note: we round to the nearest pixel before clamping so that symmetric
- // crops retain parity after multiple transformations. This avoids repeated
- // floor/ceil adjustments that previously caused ±1px drift.
- if (w <= 0 || h <= 0) {
- throw VideoCropException('Invalid crop dimensions after normalization');
- }
-
- return CropCalculation(x: minX, y: minY, width: w, height: h);
- }
}
diff --git a/mobile/apps/photos/lib/ui/tools/editor/video_editor/ente_video_editor_controller.dart b/mobile/apps/photos/lib/ui/tools/editor/video_editor/ente_video_editor_controller.dart
new file mode 100644
index 0000000000..2a16ade138
--- /dev/null
+++ b/mobile/apps/photos/lib/ui/tools/editor/video_editor/ente_video_editor_controller.dart
@@ -0,0 +1,256 @@
+import 'dart:async';
+import 'dart:io';
+import 'dart:ui';
+
+import 'package:flutter/foundation.dart';
+import 'package:native_video_editor/native_video_editor.dart';
+import 'package:video_player/video_player.dart';
+
+enum VideoRotationDirection { left, right }
+
+class VideoMinimumDurationError implements Exception {
+ const VideoMinimumDurationError(this.minimum, this.actual);
+
+ final Duration minimum;
+ final Duration actual;
+}
+
+class VideoEditorState {
+ const VideoEditorState({
+ required this.startTrim,
+ required this.endTrim,
+ required this.minCrop,
+ required this.maxCrop,
+ required this.rotation,
+ required this.preferredCropAspectRatio,
+ });
+
+ final Duration startTrim;
+ final Duration endTrim;
+ final Offset minCrop;
+ final Offset maxCrop;
+ final int rotation;
+ final double? preferredCropAspectRatio;
+}
+
+class EnteVideoEditorController extends ChangeNotifier {
+ EnteVideoEditorController.file(
+ this.file, {
+ this.minDuration = const Duration(seconds: 1),
+ }) : video = VideoPlayerController.file(file);
+
+ final File file;
+ final Duration minDuration;
+ final VideoPlayerController video;
+
+ NativeVideoInfo? _videoInfo;
+ bool _initialized = false;
+ bool _disposed = false;
+ bool _handlingTrimBoundary = false;
+ Duration _startTrim = Duration.zero;
+ Duration _endTrim = Duration.zero;
+ Offset _minCrop = Offset.zero;
+ Offset _maxCrop = const Offset(1, 1);
+ int _rotation = 0;
+ double? _preferredCropAspectRatio;
+
+ bool get initialized => _initialized;
+ NativeVideoInfo get videoInfo => _videoInfo!;
+ Duration get videoDuration => video.value.duration;
+ Duration get startTrim => _startTrim;
+ Duration get endTrim => _endTrim;
+ Duration get trimmedDuration => _endTrim - _startTrim;
+ Duration get videoPosition => video.value.position;
+ bool get isPlaying => video.value.isPlaying;
+ bool get isTrimmed =>
+ _startTrim != Duration.zero || _endTrim != videoDuration;
+ Offset get minCrop => _minCrop;
+ Offset get maxCrop => _maxCrop;
+ int get rotation => _rotation;
+ double? get preferredCropAspectRatio => _preferredCropAspectRatio;
+
+ set preferredCropAspectRatio(double? ratio) {
+ if (ratio != null && (!ratio.isFinite || ratio <= 0)) {
+ throw ArgumentError.value(ratio, 'ratio');
+ }
+ _preferredCropAspectRatio = ratio;
+ if (ratio != null) {
+ _fitCropToDisplayAspectRatio(ratio);
+ }
+ notifyListeners();
+ }
+
+ Size get sourceDisplaySize => Size(
+ videoInfo.displayWidth.toDouble(),
+ videoInfo.displayHeight.toDouble(),
+ );
+
+ Rect get normalizedCropRect =>
+ Rect.fromLTRB(_minCrop.dx, _minCrop.dy, _maxCrop.dx, _maxCrop.dy);
+
+ Rect get visualNormalizedCropRect =>
+ rotateNormalizedRect(normalizedCropRect, _rotation);
+
+ Future<void> initialize() async {
+ final values = await Future.wait<dynamic>([
+ video.initialize(),
+ NativeVideoEditor.inspectVideo(file.path),
+ ]);
+ if (_disposed) return;
+ _videoInfo = values[1] as NativeVideoInfo;
+
+ if (videoDuration < minDuration) {
+ throw VideoMinimumDurationError(minDuration, videoDuration);
+ }
+
+ _endTrim = videoDuration;
+ video.addListener(_onVideoChanged);
+ _initialized = true;
+ notifyListeners();
+ }
+
+ void updateTrim(Duration start, Duration end) {
+ final boundedStart = _clampDuration(start, Duration.zero, videoDuration);
+ final boundedEnd = _clampDuration(end, Duration.zero, videoDuration);
+ if (boundedEnd - boundedStart < minDuration) {
+ return;
+ }
+ _startTrim = boundedStart;
+ _endTrim = boundedEnd;
+ if (videoPosition < _startTrim || videoPosition > _endTrim) {
+ unawaited(video.seekTo(_startTrim));
+ }
+ notifyListeners();
+ }
+
+ void updateCrop(Offset minCrop, Offset maxCrop) {
+ final rect = Rect.fromLTRB(
+ minCrop.dx.clamp(0.0, 1.0),
+ minCrop.dy.clamp(0.0, 1.0),
+ maxCrop.dx.clamp(0.0, 1.0),
+ maxCrop.dy.clamp(0.0, 1.0),
+ );
+ if (rect.width <= 0 || rect.height <= 0) return;
+ _minCrop = rect.topLeft;
+ _maxCrop = rect.bottomRight;
+ notifyListeners();
+ }
+
+ void updateVisualCrop(Rect visualRect) {
+ final sourceRect = rotateNormalizedRect(visualRect, 360 - _rotation);
+ updateCrop(sourceRect.topLeft, sourceRect.bottomRight);
+ }
+
+ void rotate90Degrees(VideoRotationDirection direction) {
+ final delta = direction == VideoRotationDirection.right ? 90 : -90;
+ _rotation = (_rotation + delta) % 360;
+ if (_rotation < 0) _rotation += 360;
+ if (_preferredCropAspectRatio != null) {
+ _preferredCropAspectRatio = 1 / _preferredCropAspectRatio!;
+ }
+ notifyListeners();
+ }
+
+ VideoEditorState snapshot() => VideoEditorState(
+ startTrim: _startTrim,
+ endTrim: _endTrim,
+ minCrop: _minCrop,
+ maxCrop: _maxCrop,
+ rotation: _rotation,
+ preferredCropAspectRatio: _preferredCropAspectRatio,
+ );
+
+ void restore(VideoEditorState state) {
+ _startTrim = state.startTrim;
+ _endTrim = state.endTrim;
+ _minCrop = state.minCrop;
+ _maxCrop = state.maxCrop;
+ _rotation = state.rotation;
+ _preferredCropAspectRatio = state.preferredCropAspectRatio;
+ if (videoPosition < _startTrim || videoPosition > _endTrim) {
+ unawaited(video.seekTo(_startTrim));
+ }
+ notifyListeners();
+ }
+
+ void _fitCropToDisplayAspectRatio(double displayRatio) {
+ final sourceRatio = _rotation == 90 || _rotation == 270
+ ? 1 / displayRatio
+ : displayRatio;
+ final size = sourceDisplaySize;
+ final normalizedRatio = sourceRatio * size.height / size.width;
+ final current = normalizedCropRect;
+ var width = current.width;
+ var height = current.height;
+ if (width / height > normalizedRatio) {
+ width = height * normalizedRatio;
+ } else {
+ height = width / normalizedRatio;
+ }
+ final center = current.center;
+ final left = (center.dx - width / 2).clamp(0.0, 1.0 - width);
+ final top = (center.dy - height / 2).clamp(0.0, 1.0 - height);
+ _minCrop = Offset(left, top);
+ _maxCrop = Offset(left + width, top + height);
+ }
+
+ void _onVideoChanged() {
+ if (!_initialized) return;
+ if (!_handlingTrimBoundary &&
+ video.value.isPlaying &&
+ video.value.position >= _endTrim) {
+ _handlingTrimBoundary = true;
+ video.seekTo(_startTrim).whenComplete(() {
+ _handlingTrimBoundary = false;
+ });
+ }
+ }
+
+ @override
+ void dispose() {
+ _disposed = true;
+ video.removeListener(_onVideoChanged);
+ unawaited(video.dispose());
+ super.dispose();
+ }
+
+ static Duration _clampDuration(
+ Duration value,
+ Duration minimum,
+ Duration maximum,
+ ) {
+ if (value < minimum) return minimum;
+ if (value > maximum) return maximum;
+ return value;
+ }
+}
+
+Rect rotateNormalizedRect(Rect rect, int clockwiseDegrees) {
+ switch (clockwiseDegrees % 360) {
+ case 0:
+ return rect;
+ case 90:
+ return Rect.fromLTRB(
+ 1 - rect.bottom,
+ rect.left,
+ 1 - rect.top,
+ rect.right,
+ );
+ case 180:
+ return Rect.fromLTRB(
+ 1 - rect.right,
+ 1 - rect.bottom,
+ 1 - rect.left,
+ 1 - rect.top,
+ );
+ case 270:
+ return Rect.fromLTRB(
+ rect.top,
+ 1 - rect.right,
+ rect.bottom,
+ 1 - rect.left,
+ );
+ default:
+ throw ArgumentError.value(clockwiseDegrees, 'clockwiseDegrees');
+ }
+}
diff --git a/mobile/apps/photos/lib/ui/tools/editor/video_editor/ente_video_editor_widgets.dart b/mobile/apps/photos/lib/ui/tools/editor/video_editor/ente_video_editor_widgets.dart
new file mode 100644
index 0000000000..94a272d685
--- /dev/null
+++ b/mobile/apps/photos/lib/ui/tools/editor/video_editor/ente_video_editor_widgets.dart
@@ -0,0 +1,462 @@
+import 'dart:math' as math;
+
+import 'package:ente_strings/ente_strings.dart';
+import 'package:flutter/material.dart';
+import 'package:photos/theme/ente_theme.dart';
+import 'package:photos/ui/tools/editor/video_editor/ente_video_editor_controller.dart';
+import 'package:photos/ui/tools/editor/video_editor/video_editor_app_bar.dart';
+import 'package:photos/ui/tools/editor/video_editor/video_editor_player_control.dart';
+import 'package:video_player/video_player.dart';
+
+class EnteVideoEditorSubPage extends StatefulWidget {
+ const EnteVideoEditorSubPage({
+ super.key,
+ required this.controller,
+ required this.preview,
+ required this.actions,
+ });
+
+ final EnteVideoEditorController controller;
+ final Widget preview;
+ final Widget actions;
+
+ @override
+ State<EnteVideoEditorSubPage> createState() => _EnteVideoEditorSubPageState();
+}
+
+class _EnteVideoEditorSubPageState extends State<EnteVideoEditorSubPage> {
+ late final VideoEditorState _initialState;
+ bool _keepChanges = false;
+
+ @override
+ void initState() {
+ super.initState();
+ _initialState = widget.controller.snapshot();
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ return PopScope(
+ onPopInvokedWithResult: (didPop, _) {
+ if (didPop && !_keepChanges) {
+ widget.controller.restore(_initialState);
+ }
+ },
+ child: Scaffold(
+ backgroundColor: getEnteColorScheme(context).backgroundColour,
+ appBar: VideoEditorAppBar(
+ onCancel: () => Navigator.pop(context),
+ primaryActionLabel: context.strings.done,
+ onPrimaryAction: () {
+ _keepChanges = true;
+ Navigator.pop(context);
+ },
+ ),
+ body: SafeArea(
+ top: false,
+ bottom: true,
+ child: Padding(
+ padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.stretch,
+ children: [
+ Expanded(
+ child: Stack(
+ fit: StackFit.expand,
+ children: [
+ Positioned.fill(
+ child: Hero(
+ tag: 'video-editor-preview',
+ child: widget.preview,
+ ),
+ ),
+ Align(
+ alignment: Alignment.bottomCenter,
+ child: Padding(
+ padding: const EdgeInsets.only(bottom: 20),
+ child: VideoEditorPlayerControl(
+ controller: widget.controller,
+ ),
+ ),
+ ),
+ ],
+ ),
+ ),
+ widget.actions,
+ ],
+ ),
+ ),
+ ),
+ ),
+ );
+ }
+}
+
+class EnteVideoPreview extends StatelessWidget {
+ const EnteVideoPreview({super.key, required this.controller});
+
+ final EnteVideoEditorController controller;
+
+ @override
+ Widget build(BuildContext context) {
+ return ColoredBox(
+ color: Colors.black,
+ child: AnimatedBuilder(
+ animation: controller,
+ builder: (_, _) {
+ final sourceSize = controller.sourceDisplaySize;
+ final crop = controller.normalizedCropRect;
+ final cropWidth = sourceSize.width * crop.width;
+ final cropHeight = sourceSize.height * crop.height;
+ return Center(
+ child: FittedBox(
+ fit: BoxFit.contain,
+ child: RotatedBox(
+ quarterTurns: controller.rotation ~/ 90,
+ child: SizedBox(
+ width: cropWidth,
+ height: cropHeight,
+ child: ClipRect(
+ child: Transform.translate(
+ offset: Offset(
+ -crop.left * sourceSize.width,
+ -crop.top * sourceSize.height,
+ ),
+ child: OverflowBox(
+ alignment: Alignment.topLeft,
+ minWidth: sourceSize.width,
+ maxWidth: sourceSize.width,
+ minHeight: sourceSize.height,
+ maxHeight: sourceSize.height,
+ child: SizedBox.fromSize(
+ size: sourceSize,
+ child: VideoPlayer(controller.video),
+ ),
+ ),
+ ),
+ ),
+ ),
+ ),
+ ),
+ );
+ },
+ ),
+ );
+ }
+}
+
+class EnteVideoCropEditor extends StatefulWidget {
+ const EnteVideoCropEditor({super.key, required this.controller});
+
+ final EnteVideoEditorController controller;
+
+ @override
+ State<EnteVideoCropEditor> createState() => _EnteVideoCropEditorState();
+}
+
+enum _CropDrag {
+ move,
+ left,
+ top,
+ right,
+ bottom,
+ topLeft,
+ topRight,
+ bottomLeft,
+ bottomRight,
+}
+
+class _EnteVideoCropEditorState extends State<EnteVideoCropEditor> {
+ _CropDrag? _drag;
+ Rect? _startRect;
+ Offset? _startPosition;
+
+ @override
+ Widget build(BuildContext context) {
+ return ColoredBox(
+ color: Colors.black,
+ child: LayoutBuilder(
+ builder: (context, constraints) {
+ return AnimatedBuilder(
+ animation: widget.controller,
+ builder: (_, _) {
+ final source = widget.controller.sourceDisplaySize;
+ final rotated =
+ widget.controller.rotation == 90 ||
+ widget.controller.rotation == 270
+ ? Size(source.height, source.width)
+ : source;
+ final mediaRect = _containedRect(constraints.biggest, rotated);
+ return Stack(
+ children: [
+ Positioned.fromRect(
+ rect: mediaRect,
+ child: RotatedBox(
+ quarterTurns: widget.controller.rotation ~/ 90,
+ child: VideoPlayer(widget.controller.video),
+ ),
+ ),
+ Positioned.fromRect(
+ rect: mediaRect,
+ child: GestureDetector(
+ behavior: HitTestBehavior.opaque,
+ onPanStart: (details) =>
+ _startDrag(details.localPosition, mediaRect.size),
+ onPanUpdate: (details) =>
+ _updateDrag(details.localPosition, mediaRect.size),
+ onPanEnd: (_) => _endDrag(),
+ onPanCancel: _endDrag,
+ child: CustomPaint(
+ painter: _CropOverlayPainter(
+ widget.controller.visualNormalizedCropRect,
+ Theme.of(context).colorScheme.primary,
+ ),
+ ),
+ ),
+ ),
+ ],
+ );
+ },
+ );
+ },
+ ),
+ );
+ }
+
+ void _startDrag(Offset position, Size size) {
+ final rect = widget.controller.visualNormalizedCropRect;
+ _drag = _hitTest(position, size, rect);
+ _startRect = rect;
+ _startPosition = Offset(
+ position.dx / size.width,
+ position.dy / size.height,
+ );
+ }
+
+ void _updateDrag(Offset position, Size size) {
+ final drag = _drag;
+ final startRect = _startRect;
+ final startPosition = _startPosition;
+ if (drag == null || startRect == null || startPosition == null) return;
+ final current = Offset(position.dx / size.width, position.dy / size.height);
+ final delta = current - startPosition;
+ final minimumWidth = math.max(44 / size.width, 0.05);
+ final minimumHeight = math.max(44 / size.height, 0.05);
+ Rect next;
+
+ if (drag == _CropDrag.move) {
+ final dx = delta.dx.clamp(-startRect.left, 1 - startRect.right);
+ final dy = delta.dy.clamp(-startRect.top, 1 - startRect.bottom);
+ next = startRect.shift(Offset(dx, dy));
+ } else {
+ var left = startRect.left;
+ var top = startRect.top;
+ var right = startRect.right;
+ var bottom = startRect.bottom;
+ if (_movesLeft(drag)) {
+ left = (startRect.left + delta.dx).clamp(0.0, right - minimumWidth);
+ }
+ if (_movesRight(drag)) {
+ right = (startRect.right + delta.dx).clamp(left + minimumWidth, 1.0);
+ }
+ if (_movesTop(drag)) {
+ top = (startRect.top + delta.dy).clamp(0.0, bottom - minimumHeight);
+ }
+ if (_movesBottom(drag)) {
+ bottom = (startRect.bottom + delta.dy).clamp(top + minimumHeight, 1.0);
+ }
+ next = Rect.fromLTRB(left, top, right, bottom);
+ final ratio = widget.controller.preferredCropAspectRatio;
+ if (ratio != null) {
+ next = _constrainRatio(
+ next,
+ drag,
+ ratio,
+ size,
+ minimumWidth,
+ minimumHeight,
+ );
+ }
+ }
+ widget.controller.updateVisualCrop(next);
+ }
+
+ void _endDrag() {
+ _drag = null;
+ _startRect = null;
+ _startPosition = null;
+ }
+
+ _CropDrag? _hitTest(Offset position, Size size, Rect normalized) {
+ final rect = Rect.fromLTRB(
+ normalized.left * size.width,
+ normalized.top * size.height,
+ normalized.right * size.width,
+ normalized.bottom * size.height,
+ );
+ const threshold = 28.0;
+ final left = (position.dx - rect.left).abs() <= threshold;
+ final right = (position.dx - rect.right).abs() <= threshold;
+ final top = (position.dy - rect.top).abs() <= threshold;
+ final bottom = (position.dy - rect.bottom).abs() <= threshold;
+ if (left && top) return _CropDrag.topLeft;
+ if (right && top) return _CropDrag.topRight;
+ if (left && bottom) return _CropDrag.bottomLeft;
+ if (right && bottom) return _CropDrag.bottomRight;
+ if (widget.controller.preferredCropAspectRatio == null) {
+ if (left && position.dy >= rect.top && position.dy <= rect.bottom) {
+ return _CropDrag.left;
+ }
+ if (right && position.dy >= rect.top && position.dy <= rect.bottom) {
+ return _CropDrag.right;
+ }
+ if (top && position.dx >= rect.left && position.dx <= rect.right) {
+ return _CropDrag.top;
+ }
+ if (bottom && position.dx >= rect.left && position.dx <= rect.right) {
+ return _CropDrag.bottom;
+ }
+ }
+ return rect.contains(position) ? _CropDrag.move : null;
+ }
+
+ Rect _constrainRatio(
+ Rect rect,
+ _CropDrag drag,
+ double displayRatio,
+ Size size,
+ double minimumWidth,
+ double minimumHeight,
+ ) {
+ final normalizedRatio = displayRatio * size.height / size.width;
+ return constrainNormalizedCropRect(
+ rect: rect,
+ movesLeft: _movesLeft(drag),
+ movesTop: _movesTop(drag),
+ normalizedRatio: normalizedRatio,
+ minimumWidth: minimumWidth,
+ minimumHeight: minimumHeight,
+ );
+ }
+
+ bool _movesLeft(_CropDrag drag) => const {
+ _CropDrag.left,
+ _CropDrag.topLeft,
+ _CropDrag.bottomLeft,
+ }.contains(drag);
+
+ bool _movesRight(_CropDrag drag) => const {
+ _CropDrag.right,
+ _CropDrag.topRight,
+ _CropDrag.bottomRight,
+ }.contains(drag);
+
+ bool _movesTop(_CropDrag drag) => const {
+ _CropDrag.top,
+ _CropDrag.topLeft,
+ _CropDrag.topRight,
+ }.contains(drag);
+
+ bool _movesBottom(_CropDrag drag) => const {
+ _CropDrag.bottom,
+ _CropDrag.bottomLeft,
+ _CropDrag.bottomRight,
+ }.contains(drag);
+}
+
+@visibleForTesting
+Rect constrainNormalizedCropRect({
+ required Rect rect,
+ required bool movesLeft,
+ required bool movesTop,
+ required double normalizedRatio,
+ required double minimumWidth,
+ required double minimumHeight,
+}) {
+ if (!normalizedRatio.isFinite || normalizedRatio <= 0) {
+ throw ArgumentError.value(normalizedRatio, 'normalizedRatio');
+ }
+ final anchorX = movesLeft ? rect.right : rect.left;
+ final anchorY = movesTop ? rect.bottom : rect.top;
+ final maximumWidth = movesLeft ? anchorX : 1 - anchorX;
+ final maximumHeight = movesTop ? anchorY : 1 - anchorY;
+ final maximumRatioWidth = math.min(
+ maximumWidth,
+ maximumHeight * normalizedRatio,
+ );
+ final minimumRatioWidth = math.min(
+ maximumRatioWidth,
+ math.max(minimumWidth, minimumHeight * normalizedRatio),
+ );
+ final widthFromHeight = rect.height * normalizedRatio;
+ final preserveWidthDelta = (rect.width / normalizedRatio - rect.height).abs();
+ final preserveHeightDelta = (widthFromHeight - rect.width).abs();
+ final desiredWidth = preserveWidthDelta <= preserveHeightDelta
+ ? rect.width
+ : widthFromHeight;
+ final width = desiredWidth.clamp(minimumRatioWidth, maximumRatioWidth);
+ final height = width / normalizedRatio;
+ return Rect.fromLTWH(
+ movesLeft ? anchorX - width : anchorX,
+ movesTop ? anchorY - height : anchorY,
+ width,
+ height,
+ );
+}
+
+class _CropOverlayPainter extends CustomPainter {
+ const _CropOverlayPainter(this.normalizedRect, this.accentColor);
+
+ final Rect normalizedRect;
+ final Color accentColor;
+
+ @override
+ void paint(Canvas canvas, Size size) {
+ final crop = Rect.fromLTRB(
+ normalizedRect.left * size.width,
+ normalizedRect.top * size.height,
+ normalizedRect.right * size.width,
+ normalizedRect.bottom * size.height,
+ );
+ final shade = Path()
+ ..fillType = PathFillType.evenOdd
+ ..addRect(Offset.zero & size)
+ ..addRect(crop);
+ canvas.drawPath(shade, Paint()..color = Colors.black54);
+ canvas.drawRect(
+ crop,
+ Paint()
+ ..color = accentColor
+ ..style = PaintingStyle.stroke
+ ..strokeWidth = 2,
+ );
+ final grid = Paint()
+ ..color = Colors.white54
+ ..strokeWidth = 1;
+ for (var index = 1; index < 3; index++) {
+ final x = crop.left + crop.width * index / 3;
+ final y = crop.top + crop.height * index / 3;
+ canvas.drawLine(Offset(x, crop.top), Offset(x, crop.bottom), grid);
+ canvas.drawLine(Offset(crop.left, y), Offset(crop.right, y), grid);
+ }
+ final handle = Paint()..color = Colors.white;
+ for (final point in [
+ crop.topLeft,
+ crop.topRight,
+ crop.bottomLeft,
+ crop.bottomRight,
+ ]) {
+ canvas.drawCircle(point, 5, handle);
+ }
+ }
+
+ @override
+ bool shouldRepaint(_CropOverlayPainter oldDelegate) =>
+ oldDelegate.normalizedRect != normalizedRect ||
+ oldDelegate.accentColor != accentColor;
+}
+
+Rect _containedRect(Size available, Size content) {
+ final fitted = applyBoxFit(BoxFit.contain, content, available).destination;
+ return Alignment.center.inscribe(fitted, Offset.zero & available);
+}
diff --git a/mobile/apps/photos/lib/ui/tools/editor/video_editor/ente_video_trim_slider.dart b/mobile/apps/photos/lib/ui/tools/editor/video_editor/ente_video_trim_slider.dart
new file mode 100644
index 0000000000..f49e2927bc
--- /dev/null
+++ b/mobile/apps/photos/lib/ui/tools/editor/video_editor/ente_video_trim_slider.dart
@@ -0,0 +1,338 @@
+import 'dart:async';
+import 'dart:io';
+import 'dart:math' as math;
+
+import 'package:flutter/material.dart';
+import 'package:native_video_editor/native_video_editor.dart';
+import 'package:path/path.dart' as path;
+import 'package:photos/ui/tools/editor/video_editor/ente_video_editor_controller.dart';
+
+class EnteVideoTrimSlider extends StatefulWidget {
+ const EnteVideoTrimSlider({
+ super.key,
+ required this.controller,
+ this.height = 60,
+ this.horizontalMargin = 15,
+ });
+
+ final EnteVideoEditorController controller;
+ final double height;
+ final double horizontalMargin;
+
+ @override
+ State<EnteVideoTrimSlider> createState() => _EnteVideoTrimSliderState();
+}
+
+enum _TrimDrag { start, end, seek }
+
+class _EnteVideoTrimSliderState extends State<EnteVideoTrimSlider> {
+ Directory? _directory;
+ List<File> _frames = const [];
+ String? _requestId;
+ int _requestedFrameCount = 0;
+ _TrimDrag? _drag;
+ bool _resumeAfterDrag = false;
+
+ @override
+ Widget build(BuildContext context) {
+ return SizedBox(
+ height: widget.height,
+ child: LayoutBuilder(
+ builder: (context, constraints) {
+ final trackWidth = math.max(
+ 1.0,
+ constraints.maxWidth - widget.horizontalMargin * 2,
+ );
+ _ensureFrames(trackWidth);
+ return Padding(
+ padding: EdgeInsets.symmetric(horizontal: widget.horizontalMargin),
+ child: AnimatedBuilder(
+ animation: Listenable.merge([
+ widget.controller,
+ widget.controller.video,
+ ]),
+ builder: (_, _) => GestureDetector(
+ behavior: HitTestBehavior.opaque,
+ onTapUp: (details) =>
+ _seek(details.localPosition.dx, trackWidth),
+ onHorizontalDragStart: (details) =>
+ _startDrag(details.localPosition.dx, trackWidth),
+ onHorizontalDragUpdate: (details) =>
+ _updateDrag(details.localPosition.dx, trackWidth),
+ onHorizontalDragEnd: (_) => _finishDrag(),
+ onHorizontalDragCancel: _finishDrag,
+ child: ClipRRect(
+ borderRadius: BorderRadius.circular(4),
+ child: Stack(
+ fit: StackFit.expand,
+ children: [
+ _buildFrames(),
+ ..._buildSelection(trackWidth, context),
+ ],
+ ),
+ ),
+ ),
+ ),
+ );
+ },
+ ),
+ );
+ }
+
+ Widget _buildFrames() {
+ if (_frames.isEmpty) {
+ return const ColoredBox(color: Color(0xFF292929));
+ }
+ return Row(
+ children: [
+ for (final frame in _frames)
+ Expanded(
+ child: Image.file(
+ frame,
+ fit: BoxFit.cover,
+ height: widget.height,
+ gaplessPlayback: true,
+ ),
+ ),
+ ],
+ );
+ }
+
+ List<Widget> _buildSelection(double width, BuildContext context) {
+ final durationMs = widget.controller.videoDuration.inMilliseconds;
+ if (durationMs <= 0) return const [];
+ final start =
+ widget.controller.startTrim.inMilliseconds / durationMs * width;
+ final end = widget.controller.endTrim.inMilliseconds / durationMs * width;
+ final position =
+ widget.controller.videoPosition.inMilliseconds.clamp(0, durationMs) /
+ durationMs *
+ width;
+ final accent = Theme.of(context).colorScheme.primary;
+ return [
+ Positioned(
+ left: 0,
+ width: start,
+ top: 0,
+ bottom: 0,
+ child: const ColoredBox(color: Colors.black54),
+ ),
+ Positioned(
+ left: end,
+ right: 0,
+ top: 0,
+ bottom: 0,
+ child: const ColoredBox(color: Colors.black54),
+ ),
+ Positioned(
+ left: start,
+ width: math.max(0, end - start),
+ top: 0,
+ bottom: 0,
+ child: IgnorePointer(
+ child: DecoratedBox(
+ decoration: BoxDecoration(
+ border: Border.symmetric(
+ horizontal: BorderSide(color: accent, width: 3),
+ ),
+ ),
+ ),
+ ),
+ ),
+ _handle(start - 7, accent),
+ _handle(end - 7, accent),
+ Positioned(
+ left: (position - 1).clamp(0, width - 2),
+ width: 2,
+ top: 4,
+ bottom: 4,
+ child: const ColoredBox(color: Colors.white),
+ ),
+ ];
+ }
+
+ Widget _handle(double left, Color color) => Positioned(
+ left: left,
+ width: 14,
+ top: 0,
+ bottom: 0,
+ child: ColoredBox(
+ color: color,
+ child: const Icon(Icons.drag_handle, size: 12, color: Colors.white),
+ ),
+ );
+
+ void _startDrag(double x, double width) {
+ _resumeAfterDrag = widget.controller.isPlaying;
+ if (_resumeAfterDrag) {
+ unawaited(widget.controller.video.pause());
+ }
+ final start = _durationToPixels(widget.controller.startTrim, width);
+ final end = _durationToPixels(widget.controller.endTrim, width);
+ if ((x - start).abs() <= 28) {
+ _drag = _TrimDrag.start;
+ } else if ((x - end).abs() <= 28) {
+ _drag = _TrimDrag.end;
+ } else {
+ _drag = _TrimDrag.seek;
+ _seek(x, width);
+ }
+ }
+
+ void _finishDrag() {
+ _drag = null;
+ if (_resumeAfterDrag) {
+ _resumeAfterDrag = false;
+ unawaited(widget.controller.video.play());
+ }
+ }
+
+ void _updateDrag(double x, double width) {
+ final value = _pixelsToDuration(x, width);
+ switch (_drag) {
+ case _TrimDrag.start:
+ final latest =
+ widget.controller.endTrim - widget.controller.minDuration;
+ widget.controller.updateTrim(
+ value > latest ? latest : value,
+ widget.controller.endTrim,
+ );
+ case _TrimDrag.end:
+ final earliest =
+ widget.controller.startTrim + widget.controller.minDuration;
+ widget.controller.updateTrim(
+ widget.controller.startTrim,
+ value < earliest ? earliest : value,
+ );
+ case _TrimDrag.seek:
+ _seek(x, width);
+ case null:
+ break;
+ }
+ }
+
+ void _seek(double x, double width) {
+ var position = _pixelsToDuration(x, width);
+ if (position < widget.controller.startTrim) {
+ position = widget.controller.startTrim;
+ } else if (position > widget.controller.endTrim) {
+ position = widget.controller.endTrim;
+ }
+ unawaited(widget.controller.video.seekTo(position));
+ }
+
+ double _durationToPixels(Duration duration, double width) =>
+ duration.inMicroseconds /
+ widget.controller.videoDuration.inMicroseconds *
+ width;
+
+ Duration _pixelsToDuration(double pixels, double width) {
+ final fraction = (pixels / width).clamp(0.0, 1.0);
+ return Duration(
+ microseconds: (widget.controller.videoDuration.inMicroseconds * fraction)
+ .round(),
+ );
+ }
+
+ void _ensureFrames(double width) {
+ final count = (width / 72).ceil().clamp(4, 12);
+ if (_requestedFrameCount == count) return;
+ _requestedFrameCount = count;
+ WidgetsBinding.instance.addPostFrameCallback((_) {
+ if (mounted) unawaited(_extractFrames(count));
+ });
+ }
+
+ Future<void> _extractFrames(int count) async {
+ final requestId =
+ 'timeline-${DateTime.now().microsecondsSinceEpoch}-${identityHashCode(this)}';
+ final oldRequest = _requestId;
+ _requestId = requestId;
+ if (oldRequest != null) {
+ await NativeVideoEditor.cancelFrameExtraction(oldRequest);
+ }
+ if (!mounted || _requestId != requestId) return;
+
+ final directory = await Directory.systemTemp.createTemp(
+ 'ente-video-timeline-',
+ );
+ if (!mounted || _requestId != requestId) {
+ await directory.delete(recursive: true);
+ return;
+ }
+ final oldDirectory = _directory;
+ _directory = directory;
+ if (_frames.isNotEmpty) {
+ setState(() => _frames = const []);
+ }
+ unawaited(_deleteDirectory(oldDirectory));
+ final durationUs = widget.controller.videoDuration.inMicroseconds;
+ final positions = List<Duration>.generate(
+ count,
+ (index) =>
+ Duration(microseconds: (durationUs * (index + 0.5) / count).round()),
+ );
+ final outputPaths = List<String>.generate(
+ count,
+ (index) => path.join(directory.path, 'tile-$index.jpg'),
+ );
+ try {
+ final extraction = await NativeVideoEditor.extractTimeline(
+ VideoTimelineRequest(
+ requestId: requestId,
+ inputPath: widget.controller.file.path,
+ outputPaths: outputPaths,
+ positions: positions,
+ maxWidth: 144,
+ maxHeight: 120,
+ quality: 72,
+ policy: VideoFramePolicy.precise,
+ ),
+ );
+ if (!mounted || _requestId != requestId) return;
+ setState(() {
+ _frames = extraction.frames
+ .map((frame) => File(frame.outputPath))
+ .toList(growable: false);
+ });
+ } on NativeVideoEditorException catch (error) {
+ if (error.code != 'FRAME_CANCELLED' &&
+ mounted &&
+ _requestId == requestId) {
+ setState(() => _frames = const []);
+ }
+ }
+ }
+
+ @override
+ void dispose() {
+ final requestId = _requestId;
+ final directory = _directory;
+ _requestId = null;
+ _directory = null;
+ unawaited(_cancelAndDelete(requestId, directory));
+ super.dispose();
+ }
+
+ Future<void> _cancelAndDelete(String? requestId, Directory? directory) async {
+ try {
+ if (requestId != null) {
+ await NativeVideoEditor.cancelFrameExtraction(requestId);
+ }
+ } on NativeVideoEditorException {
+ // The widget is already disposed; only local cleanup remains actionable.
+ } finally {
+ await _deleteDirectory(directory);
+ }
+ }
+
+ Future<void> _deleteDirectory(Directory? directory) async {
+ if (directory != null && await directory.exists()) {
+ try {
+ await directory.delete(recursive: true);
+ } on FileSystemException {
+ // Temporary timeline files are best-effort cleanup.
+ }
+ }
+ }
+}
diff --git a/mobile/apps/photos/lib/ui/tools/editor/video_editor/video_editor_player_control.dart b/mobile/apps/photos/lib/ui/tools/editor/video_editor/video_editor_player_control.dart
index d6a756c433..2642b050cd 100644
--- a/mobile/apps/photos/lib/ui/tools/editor/video_editor/video_editor_player_control.dart
+++ b/mobile/apps/photos/lib/ui/tools/editor/video_editor/video_editor_player_control.dart
@@ -1,11 +1,11 @@
import "package:flutter/material.dart";
import "package:photos/ente_theme_data.dart";
-import "package:video_editor/video_editor.dart";
+import "package:photos/ui/tools/editor/video_editor/ente_video_editor_controller.dart";
class VideoEditorPlayerControl extends StatelessWidget {
const VideoEditorPlayerControl({super.key, required this.controller});
- final VideoEditorController controller;
+ final EnteVideoEditorController controller;
@override
Widget build(BuildContext context) {
diff --git a/mobile/apps/photos/lib/ui/tools/editor/video_editor_page.dart b/mobile/apps/photos/lib/ui/tools/editor/video_editor_page.dart
index 45af37a7c4..89bf81e5b4 100644
--- a/mobile/apps/photos/lib/ui/tools/editor/video_editor_page.dart
+++ b/mobile/apps/photos/lib/ui/tools/editor/video_editor_page.dart
@@ -11,7 +11,6 @@ import 'package:path/path.dart' as path;
import "package:photo_manager/photo_manager.dart";
import "package:photos/core/event_bus.dart";
import "package:photos/db/files_db.dart";
-import "package:photos/ente_theme_data.dart";
import "package:photos/events/local_photos_updated_event.dart";
import "package:photos/models/file/file.dart";
import "package:photos/models/location/location.dart";
@@ -25,6 +24,8 @@ import "package:photos/ui/tools/editor/export_video_service.dart";
import "package:photos/ui/tools/editor/native_video_export_service.dart";
import 'package:photos/ui/tools/editor/video_crop_page.dart';
import "package:photos/ui/tools/editor/video_crop_util.dart";
+import "package:photos/ui/tools/editor/video_editor/ente_video_editor_controller.dart";
+import "package:photos/ui/tools/editor/video_editor/ente_video_editor_widgets.dart";
import "package:photos/ui/tools/editor/video_editor/video_editor_app_bar.dart";
import "package:photos/ui/tools/editor/video_editor/video_editor_bottom_action.dart";
import "package:photos/ui/tools/editor/video_editor/video_editor_main_actions.dart";
@@ -33,7 +34,6 @@ import "package:photos/ui/tools/editor/video_rotate_page.dart";
import "package:photos/ui/tools/editor/video_trim_page.dart";
import "package:photos/ui/viewer/file/detail_page.dart";
import "package:photos/utils/gallery_save_title.dart";
-import "package:video_editor/video_editor.dart";
class VideoEditorPage extends StatefulWidget {
const VideoEditorPage({
@@ -55,13 +55,7 @@ class _VideoEditorPageState extends State<VideoEditorPage> {
final _isExporting = ValueNotifier<bool>(false);
final _logger = Logger("VideoEditor");
- /// Some videos have a non-zero 'rotation' property in exif which causes the
- /// video to appear rotated in the video editor preview on Android.
- /// This variable is used as a workaround to rotate the video back to its
- /// expected orientation in the viewer.
- int? _quarterTurnsForRotationCorrection;
-
- VideoEditorController? _controller;
+ EnteVideoEditorController? _controller;
/// Toggle state for internal users to switch between native and FFmpeg export
/// Initially set to the flag service value
@@ -74,57 +68,29 @@ class _VideoEditorPageState extends State<VideoEditorPage> {
// Initialize toggle with flagService value
_useNativeExport = flagService.useNativeVideoEditor;
- // First determine rotation correction for Android
- _doRotationCorrectionIfAndroid().then((_) {
- // Then initialize the controller
- if (!mounted) return;
- _controller = VideoEditorController.file(
- widget.ioFile,
- minDuration: const Duration(seconds: 1),
- cropStyle: CropGridStyle(
- background: Theme.of(context).colorScheme.surface,
- selectedBoundariesColor:
- const ColorScheme.dark().videoPlayerPrimaryColor,
- ),
- trimStyle: TrimSliderStyle(
- onTrimmedColor: const ColorScheme.dark().videoPlayerPrimaryColor,
- onTrimmingColor: const ColorScheme.dark().videoPlayerPrimaryColor,
- background: Theme.of(context).colorScheme.editorBackgroundColor,
- positionLineColor: Theme.of(
- context,
- ).colorScheme.videoPlayerBorderColor,
- lineColor: Theme.of(
- context,
- ).colorScheme.videoPlayerBorderColor.withValues(alpha: 0.6),
- ),
- );
+ _controller = EnteVideoEditorController.file(widget.ioFile);
+ unawaited(_initializeController());
+ }
- _controller!
- .initialize()
- .then((_) {
- // Apply metadata rotation to the video player
- if (_quarterTurnsForRotationCorrection != null &&
- _quarterTurnsForRotationCorrection! != 0) {
- final rotationDegrees = _quarterTurnsForRotationCorrection! * 90;
- _controller!.video.value = _controller!.video.value.copyWith(
- rotationCorrection: rotationDegrees,
- );
- }
- setState(() {});
- })
- .catchError((error) {
- // handle minimum duration bigger than video duration error
- if (!mounted) return;
- Navigator.pop(context);
- }, test: (e) => e is VideoMinDurationError);
- });
+ Future<void> _initializeController() async {
+ try {
+ await _controller!.initialize();
+ if (mounted) setState(() {});
+ } on VideoMinimumDurationError {
+ if (mounted) Navigator.pop(context);
+ } catch (error, stackTrace) {
+ _logger.severe('Failed to initialize video editor', error, stackTrace);
+ if (mounted) {
+ showShortToast(context, context.strings.somethingWentWrong);
+ Navigator.pop(context);
+ }
+ }
}
@override
- void dispose() async {
+ void dispose() {
_isExporting.dispose();
- _controller?.dispose().ignore();
- ExportService.dispose().ignore();
+ _controller?.dispose();
super.dispose();
}
@@ -143,10 +109,7 @@ class _VideoEditorPageState extends State<VideoEditorPage> {
child: ValueListenableBuilder<bool>(
valueListenable: _isExporting,
builder: (context, isExporting, _) {
- final isReady =
- _controller != null &&
- _controller!.initialized &&
- _quarterTurnsForRotationCorrection != null;
+ final isReady = _controller?.initialized ?? false;
return Scaffold(
backgroundColor: colorScheme.backgroundColour,
@@ -180,7 +143,7 @@ class _VideoEditorPageState extends State<VideoEditorPage> {
Positioned.fill(
child: Hero(
tag: "video-editor-preview",
- child: CropGridViewer.preview(
+ child: EnteVideoPreview(
controller: _controller!,
),
),
@@ -324,12 +287,8 @@ class _VideoEditorPageState extends State<VideoEditorPage> {
required GlobalKey<LinearProgressDialogState> dialogKey,
}) async {
if (shouldUseNative) {
- final tempDir = Directory.systemTemp.createTempSync('ente_video_export');
try {
- return await _runNativeExportWithRetry(
- tempDir: tempDir,
- dialogKey: dialogKey,
- );
+ return await _runNativeExportWithRetry(dialogKey: dialogKey);
} catch (nativeError, stackTrace) {
if (nativeError is NativeVideoEditorException) {
_logger.warning(
@@ -363,187 +322,90 @@ class _VideoEditorPageState extends State<VideoEditorPage> {
}
Future<File> _runNativeExportWithRetry({
- required Directory tempDir,
required GlobalKey<LinearProgressDialogState> dialogKey,
+ }) => _runExportWithRetry(
+ label: 'Native',
+ outputPrefix: 'ente-native-export',
+ dialogKey: dialogKey,
+ export: (outputPath) => NativeVideoExportService.exportVideo(
+ controller: _controller!,
+ outputPath: outputPath,
+ onProgress: (progress) => dialogKey.currentState?.setProgress(progress),
+ onError: (e, s) {
+ if (e is NativeVideoEditorException) {
+ _logger.severe(
+ "Error exporting video with native (code=${e.code}, details=${e.details})",
+ e,
+ s,
+ );
+ } else {
+ _logger.severe("Error exporting video with native", e, s);
+ }
+ },
+ ),
+ );
+
+ Future<File> _runFfmpegExportWithRetry({
+ required GlobalKey<LinearProgressDialogState> dialogKey,
+ }) => _runExportWithRetry(
+ label: 'FFmpeg',
+ outputPrefix: 'ente-ffmpeg-export',
+ dialogKey: dialogKey,
+ export: (outputPath) => ExportService.exportVideo(
+ controller: _controller!,
+ outputPath: outputPath,
+ onProgress: (progress) => dialogKey.currentState?.setProgress(progress),
+ onError: (error, stackTrace) {
+ _logger.severe("Error exporting video with FFmpeg", error, stackTrace);
+ },
+ ),
+ );
+
+ Future<File> _runExportWithRetry({
+ required String label,
+ required String outputPrefix,
+ required GlobalKey<LinearProgressDialogState> dialogKey,
+ required Future<File> Function(String outputPath) export,
}) async {
- const maxAttempts = 2;
const retryThreshold = Duration(seconds: 1);
- Object? lastError;
- StackTrace? lastStackTrace;
-
- for (var attempt = 0; attempt < maxAttempts; attempt++) {
+ for (var attempt = 0; attempt < 2; attempt++) {
+ if (attempt > 0) dialogKey.currentState?.setProgress(0);
final outputPath = path.join(
- tempDir.path,
- 'native_export_${DateTime.now().microsecondsSinceEpoch}_$attempt.mp4',
+ Directory.systemTemp.path,
+ '$outputPrefix-${DateTime.now().microsecondsSinceEpoch}-$attempt.mp4',
);
-
- if (attempt > 0 && dialogKey.currentState != null) {
- dialogKey.currentState!.setProgress(0.0);
- }
-
final startTime = DateTime.now();
try {
- return await NativeVideoExportService.exportVideo(
- controller: _controller!,
- outputPath: outputPath,
- onProgress: (progress) {
- if (dialogKey.currentState != null) {
- dialogKey.currentState!.setProgress(progress);
- }
- },
- onError: (e, s) {
- if (e is NativeVideoEditorException) {
- _logger.severe(
- "Error exporting video with native (code=${e.code}, details=${e.details})",
- e,
- s,
- );
- } else {
- _logger.severe("Error exporting video with native", e, s);
- }
- },
- allowFfmpegFallback: false,
- );
+ return await export(outputPath);
} catch (error, stackTrace) {
- lastError = error;
- lastStackTrace = stackTrace;
+ await _deleteTemporaryOutput(outputPath);
final elapsed = DateTime.now().difference(startTime);
-
- if (attempt < maxAttempts - 1) {
- if (elapsed > retryThreshold) {
- _logger.info(
- "Native export attempt ${attempt + 1} failed after ${elapsed.inMilliseconds}ms, skipping retry",
- error,
- stackTrace,
- );
- break;
- }
-
+ if (attempt == 0 && elapsed <= retryThreshold) {
_logger.info(
- "Native export attempt ${attempt + 1} failed, retrying with fresh output path",
+ "$label export failed quickly; retrying with a fresh output path",
error,
stackTrace,
);
continue;
}
-
- rethrow;
- }
- }
-
- if (lastError != null) {
- Error.throwWithStackTrace(
- lastError,
- lastStackTrace ?? StackTrace.current,
- );
- }
- throw Exception("Unknown native export failure");
- }
-
- Future<File> _runFfmpegExportWithRetry({
- required GlobalKey<LinearProgressDialogState> dialogKey,
- }) async {
- const maxAttempts = 2;
- const retryThreshold = Duration(seconds: 1);
- Object? lastError;
- StackTrace? lastStackTrace;
-
- for (var attempt = 0; attempt < maxAttempts; attempt++) {
- if (attempt > 0 && dialogKey.currentState != null) {
- dialogKey.currentState!.setProgress(0.0);
- }
-
- final config = VideoFFmpegVideoEditorConfig(
- _controller!,
- format: VideoExportFormat.mp4,
- commandBuilder: (config, videoPath, outputPath) {
- final List<String> filters = config.getExportFilters();
-
- final String startTrimCmd = "-ss ${_controller!.startTrim}";
- final String toTrimCmd = "-t ${_controller!.trimmedDuration}";
- final command =
- '$startTrimCmd -i $videoPath $toTrimCmd ${config.filtersCmd(filters)} -c:v libx264 -c:a aac $outputPath';
- return command;
- },
- );
-
- final startTime = DateTime.now();
- try {
- return await _runFfmpegExportAttempt(
- config: config,
- dialogKey: dialogKey,
- );
- } catch (error, stackTrace) {
- lastError = error;
- lastStackTrace = stackTrace;
- final elapsed = DateTime.now().difference(startTime);
-
- if (attempt < maxAttempts - 1) {
- if (elapsed > retryThreshold) {
- _logger.info(
- "FFmpeg export attempt ${attempt + 1} failed after ${elapsed.inMilliseconds}ms, skipping retry",
- error,
- stackTrace,
- );
- break;
- }
-
+ if (attempt == 0) {
_logger.info(
- "FFmpeg export attempt ${attempt + 1} failed, retrying with fresh output path",
+ "$label export failed after ${elapsed.inMilliseconds}ms; skipping retry",
error,
stackTrace,
);
- continue;
}
-
- rethrow;
+ Error.throwWithStackTrace(error, stackTrace);
}
}
-
- if (lastError != null) {
- Error.throwWithStackTrace(
- lastError,
- lastStackTrace ?? StackTrace.current,
- );
- }
- throw Exception("Unknown FFmpeg export failure");
- }
-
- Future<File> _runFfmpegExportAttempt({
- required VideoFFmpegVideoEditorConfig config,
- required GlobalKey<LinearProgressDialogState> dialogKey,
- }) async {
- final executeConfig = await config.getExecuteConfig();
- final completer = Completer<File>();
-
- await ExportService.runFFmpegCommand(
- executeConfig,
- onProgress: (stats) {
- final progress = config.getFFmpegProgress(stats.getTime().toInt());
- if (dialogKey.currentState != null) {
- dialogKey.currentState!.setProgress(progress);
- }
- },
- onError: (e, s) {
- _logger.severe("Error exporting video with FFmpeg", e, s);
- if (!completer.isCompleted) {
- completer.completeError(e, s);
- }
- },
- onCompleted: (file) {
- if (!completer.isCompleted) {
- completer.complete(file);
- }
- },
- );
-
- return completer.future;
+ throw StateError('$label export exhausted its retry attempts');
}
Future<void> _handleExportCompletion(
File result,
GlobalKey<LinearProgressDialogState> dialogKey,
) async {
+ var notificationsStopped = false;
try {
_isExporting.value = false;
if (!mounted) {
@@ -560,6 +422,7 @@ class _VideoEditorPageState extends State<VideoEditorPage> {
//Disabling notifications for assets changing to insert the file into
//files db before triggering a sync.
await PhotoManager.stopChangeNotify();
+ notificationsStopped = true;
try {
final AssetEntity newAsset = await (PhotoManager.editor.saveVideo(
@@ -567,8 +430,6 @@ class _VideoEditorPageState extends State<VideoEditorPage> {
title: galleryTitle,
));
- result.deleteSync();
-
final newFile = fileFromAsset(widget.file.deviceFolder ?? '', newAsset);
newFile.creationTime = widget.file.creationTime;
@@ -617,7 +478,7 @@ class _VideoEditorPageState extends State<VideoEditorPage> {
selectionIndex = originalIndex == -1 ? fallbackIndex : originalIndex;
files.insert(selectionIndex, newFile);
}
- Navigator.of(dialogKey.currentContext!).pop('dialog');
+ _closeProgressDialog(dialogKey);
if (!mounted) return;
replacePage(
@@ -631,10 +492,35 @@ class _VideoEditorPageState extends State<VideoEditorPage> {
);
} catch (e, s) {
_logger.severe("Error in post-processing", e, s);
- Navigator.of(dialogKey.currentContext!).pop('dialog');
+ _closeProgressDialog(dialogKey);
+ if (mounted) {
+ showShortToast(context, context.strings.somethingWentWrong);
+ }
}
} finally {
- await PhotoManager.startChangeNotify();
+ await _deleteTemporaryOutput(result.path);
+ if (notificationsStopped) {
+ await PhotoManager.startChangeNotify();
+ }
+ }
+ }
+
+ Future<void> _deleteTemporaryOutput(String outputPath) async {
+ try {
+ await deleteFileSystemEntityIfPresent(File(outputPath));
+ } on FileSystemException catch (error, stackTrace) {
+ _logger.warning(
+ 'Failed to delete temporary video output: $outputPath',
+ error,
+ stackTrace,
+ );
+ }
+ }
+
+ void _closeProgressDialog(GlobalKey<LinearProgressDialogState> dialogKey) {
+ final dialogContext = dialogKey.currentContext;
+ if (dialogContext != null) {
+ Navigator.of(dialogContext).pop('dialog');
}
}
@@ -656,9 +542,6 @@ class _VideoEditorPageState extends State<VideoEditorPage> {
final endTrimMs = controller.endTrim.inMilliseconds;
final trimmedDurationMs = controller.trimmedDuration.inMilliseconds;
final videoDurationMs = controller.videoDuration.inMilliseconds;
- final minTrim = controller.minTrim;
- final maxTrim = controller.maxTrim;
-
String fileSpaceCropSummary;
try {
final crop = VideoCropUtil.calculateFileSpaceCrop(controller: controller);
@@ -676,7 +559,7 @@ class _VideoEditorPageState extends State<VideoEditorPage> {
_logger.info(
"Export starting (native=$shouldUseNative) rotation=$rotation°, "
"trim={startMs:$startTrimMs, endMs:$endTrimMs, durationMs:$trimmedDurationMs, "
- "min:${minTrim.toStringAsFixed(3)}, max:${maxTrim.toStringAsFixed(3)}, "
+ "minMs:0, maxMs:$videoDurationMs, "
"videoDurationMs:$videoDurationMs} "
"crop={$cropInfo}",
);
@@ -684,32 +567,6 @@ class _VideoEditorPageState extends State<VideoEditorPage> {
String _formatOffset(Offset offset) =>
"(${offset.dx.toStringAsFixed(3)}, ${offset.dy.toStringAsFixed(3)})";
-
- Future<void> _doRotationCorrectionIfAndroid() async {
- if (Platform.isAndroid) {
- try {
- // Use native method to get video info more efficiently
- final videoInfo = await NativeVideoEditor.getVideoInfo(
- widget.ioFile.path,
- );
- final rotation = videoInfo['rotation'] as int? ?? 0;
-
- if (rotation != 0) {
- _quarterTurnsForRotationCorrection = (rotation / 90).round();
- } else {
- _quarterTurnsForRotationCorrection = 0;
- }
- setState(() {});
- } catch (e) {
- _logger.warning('Failed to get video info, using fallback', e);
- _quarterTurnsForRotationCorrection = 0;
- setState(() {});
- }
- } else {
- _quarterTurnsForRotationCorrection = 0;
- setState(() {});
- }
- }
}
class _VideoEditorSubPageRoute extends PageRouteBuilder<void> {
diff --git a/mobile/apps/photos/lib/ui/tools/editor/video_rotate_page.dart b/mobile/apps/photos/lib/ui/tools/editor/video_rotate_page.dart
index bd55398c13..8cec1ea756 100644
--- a/mobile/apps/photos/lib/ui/tools/editor/video_rotate_page.dart
+++ b/mobile/apps/photos/lib/ui/tools/editor/video_rotate_page.dart
@@ -1,83 +1,36 @@
import "package:ente_strings/ente_strings.dart";
import 'package:flutter/material.dart';
-import "package:photos/theme/ente_theme.dart";
-import "package:photos/ui/tools/editor/video_editor/video_editor_app_bar.dart";
+import "package:photos/ui/tools/editor/video_editor/ente_video_editor_controller.dart";
+import "package:photos/ui/tools/editor/video_editor/ente_video_editor_widgets.dart";
import "package:photos/ui/tools/editor/video_editor/video_editor_bottom_action.dart";
import "package:photos/ui/tools/editor/video_editor/video_editor_main_actions.dart";
-import "package:photos/ui/tools/editor/video_editor/video_editor_player_control.dart";
-import 'package:video_editor/video_editor.dart';
class VideoRotatePage extends StatelessWidget {
const VideoRotatePage({super.key, required this.controller});
- final VideoEditorController controller;
+ final EnteVideoEditorController controller;
@override
Widget build(BuildContext context) {
- final rotation = controller.rotation;
- final colorScheme = getEnteColorScheme(context);
- return Scaffold(
- backgroundColor: colorScheme.backgroundColour,
- appBar: VideoEditorAppBar(
- onCancel: () {
- while (controller.rotation != rotation) {
- controller.rotate90Degrees(RotateDirection.left);
- }
- Navigator.pop(context);
- },
- primaryActionLabel: context.strings.done,
- onPrimaryAction: () {
- Navigator.pop(context);
- },
- ),
- body: SafeArea(
- top: false,
- bottom: true,
- child: Padding(
- padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: [
- Expanded(
- child: Stack(
- fit: StackFit.expand,
- children: [
- Positioned.fill(
- child: Hero(
- tag: "video-editor-preview",
- child: CropGridViewer.preview(controller: controller),
- ),
- ),
- Align(
- alignment: Alignment.bottomCenter,
- child: Padding(
- padding: const EdgeInsets.only(bottom: 20),
- child: VideoEditorPlayerControl(controller: controller),
- ),
- ),
- ],
- ),
- ),
- VideoEditorMainActions(
- children: [
- VideoEditorBottomAction(
- label: context.strings.left,
- onPressed: () =>
- controller.rotate90Degrees(RotateDirection.left),
- icon: Icons.rotate_left,
- ),
- const SizedBox(width: 24),
- VideoEditorBottomAction(
- label: context.strings.right,
- onPressed: () =>
- controller.rotate90Degrees(RotateDirection.right),
- icon: Icons.rotate_right,
- ),
- ],
- ),
- ],
+ return EnteVideoEditorSubPage(
+ controller: controller,
+ preview: EnteVideoPreview(controller: controller),
+ actions: VideoEditorMainActions(
+ children: [
+ VideoEditorBottomAction(
+ label: context.strings.left,
+ onPressed: () =>
+ controller.rotate90Degrees(VideoRotationDirection.left),
+ icon: Icons.rotate_left,
+ ),
+ const SizedBox(width: 24),
+ VideoEditorBottomAction(
+ label: context.strings.right,
+ onPressed: () =>
+ controller.rotate90Degrees(VideoRotationDirection.right),
+ icon: Icons.rotate_right,
),
- ),
+ ],
),
);
}
diff --git a/mobile/apps/photos/lib/ui/tools/editor/video_trim_page.dart b/mobile/apps/photos/lib/ui/tools/editor/video_trim_page.dart
index 9ea81231df..6289dcca7d 100644
--- a/mobile/apps/photos/lib/ui/tools/editor/video_trim_page.dart
+++ b/mobile/apps/photos/lib/ui/tools/editor/video_trim_page.dart
@@ -1,93 +1,27 @@
-import "package:ente_strings/ente_strings.dart";
import 'package:flutter/material.dart';
-import "package:photos/theme/ente_theme.dart";
-import "package:photos/ui/tools/editor/video_editor/video_editor_app_bar.dart";
-import "package:photos/ui/tools/editor/video_editor/video_editor_player_control.dart";
-import 'package:video_editor/video_editor.dart';
+import "package:photos/ui/tools/editor/video_editor/ente_video_editor_controller.dart";
+import "package:photos/ui/tools/editor/video_editor/ente_video_editor_widgets.dart";
+import "package:photos/ui/tools/editor/video_editor/ente_video_trim_slider.dart";
-class VideoTrimPage extends StatefulWidget {
+class VideoTrimPage extends StatelessWidget {
const VideoTrimPage({super.key, required this.controller});
- final VideoEditorController controller;
-
- @override
- State<VideoTrimPage> createState() => _VideoTrimPageState();
-}
-
-class _VideoTrimPageState extends State<VideoTrimPage> {
- final double height = 60;
+ final EnteVideoEditorController controller;
@override
Widget build(BuildContext context) {
- final minTrim = widget.controller.minTrim;
- final maxTrim = widget.controller.maxTrim;
- final colorScheme = getEnteColorScheme(context);
-
- return Scaffold(
- backgroundColor: colorScheme.backgroundColour,
- appBar: VideoEditorAppBar(
- onCancel: () {
- widget.controller.updateTrim(minTrim, maxTrim);
- Navigator.pop(context);
- },
- primaryActionLabel: context.strings.done,
- onPrimaryAction: () {
- Navigator.pop(context);
- },
- ),
- body: SafeArea(
- top: false,
- bottom: true,
- child: Padding(
- padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: [
- Expanded(
- child: Stack(
- fit: StackFit.expand,
- children: [
- Positioned.fill(
- child: Hero(
- tag: "video-editor-preview",
- child: CropGridViewer.preview(
- controller: widget.controller,
- ),
- ),
- ),
- Align(
- alignment: Alignment.bottomCenter,
- child: Padding(
- padding: const EdgeInsets.only(bottom: 20),
- child: VideoEditorPlayerControl(
- controller: widget.controller,
- ),
- ),
- ),
- ],
- ),
- ),
- _buildTrimSlider(context),
- ],
- ),
+ const height = 60.0;
+ return EnteVideoEditorSubPage(
+ controller: controller,
+ preview: EnteVideoPreview(controller: controller),
+ actions: Padding(
+ padding: const EdgeInsets.only(top: 16),
+ child: EnteVideoTrimSlider(
+ controller: controller,
+ height: height,
+ horizontalMargin: height / 4,
),
),
);
}
-
- Widget _buildTrimSlider(BuildContext context) {
- return Padding(
- padding: const EdgeInsets.only(top: 16),
- child: TrimSlider(
- controller: widget.controller,
- height: height,
- horizontalMargin: height / 4,
- ),
- );
- }
-
- String formatter(Duration duration) => [
- duration.inMinutes.remainder(60).toString().padLeft(2, '0'),
- duration.inSeconds.remainder(60).toString().padLeft(2, '0'),
- ].join(":");
}
diff --git a/mobile/apps/photos/pubspec.yaml b/mobile/apps/photos/pubspec.yaml
index 1f9db83a03..9fdad03fac 100644
--- a/mobile/apps/photos/pubspec.yaml
+++ b/mobile/apps/photos/pubspec.yaml
@@ -225,12 +225,7 @@ dependencies:
ua_client_hints: 1.4.1
url_launcher: 6.3.2
uuid: 4.5.3
- video_editor: # edge cases handled in fork
- git:
- url: https://github.com/ente/video_editor_fork.git
- ref: 1eeb18e2b1ce36bd8ca70178c0d4b485e9982257
video_player: 2.10.0
- video_thumbnail: 0.5.6
visibility_detector: 0.4.0+2
wakelock_plus: 1.3.2
wechat_assets_picker: 9.8.0
diff --git a/mobile/apps/photos/test/ui/tools/editor/ente_video_editor_controller_test.dart b/mobile/apps/photos/test/ui/tools/editor/ente_video_editor_controller_test.dart
new file mode 100644
index 0000000000..3563e30e8c
--- /dev/null
+++ b/mobile/apps/photos/test/ui/tools/editor/ente_video_editor_controller_test.dart
@@ -0,0 +1,69 @@
+import 'dart:ui';
+
+import 'package:flutter_test/flutter_test.dart';
+import 'package:photos/ui/tools/editor/video_editor/ente_video_editor_controller.dart';
+import 'package:photos/ui/tools/editor/video_editor/ente_video_editor_widgets.dart';
+
+void main() {
+ const sourceCrop = Rect.fromLTRB(0.1, 0.2, 0.6, 0.8);
+
+ test('clockwise crop rotation preserves the selected visual region', () {
+ _expectRectClose(
+ rotateNormalizedRect(sourceCrop, 90),
+ const Rect.fromLTRB(0.2, 0.1, 0.8, 0.6),
+ );
+ _expectRectClose(
+ rotateNormalizedRect(sourceCrop, 180),
+ const Rect.fromLTRB(0.4, 0.2, 0.9, 0.8),
+ );
+ _expectRectClose(
+ rotateNormalizedRect(sourceCrop, 270),
+ const Rect.fromLTRB(0.2, 0.4, 0.8, 0.9),
+ );
+ });
+
+ test('visual crop conversion is reversible for every quarter turn', () {
+ for (final rotation in [0, 90, 180, 270]) {
+ final visual = rotateNormalizedRect(sourceCrop, rotation);
+ final restored = rotateNormalizedRect(visual, 360 - rotation);
+ _expectRectClose(restored, sourceCrop, reason: 'rotation=$rotation');
+ }
+ });
+
+ test('four clockwise turns return the original crop', () {
+ var crop = sourceCrop;
+ for (var index = 0; index < 4; index++) {
+ crop = rotateNormalizedRect(crop, 90);
+ }
+ _expectRectClose(crop, sourceCrop);
+ });
+
+ test('non-quarter-turn rotation is rejected', () {
+ expect(() => rotateNormalizedRect(sourceCrop, 45), throwsArgumentError);
+ });
+
+ test('fixed-ratio crop remains in bounds without changing ratio', () {
+ final crop = constrainNormalizedCropRect(
+ rect: const Rect.fromLTRB(-0.1, 0.1, 0.8, 0.9),
+ movesLeft: true,
+ movesTop: true,
+ normalizedRatio: 16 / 9,
+ minimumWidth: 0.05,
+ minimumHeight: 0.05,
+ );
+
+ expect(crop.left, greaterThanOrEqualTo(0));
+ expect(crop.top, greaterThanOrEqualTo(0));
+ expect(crop.right, lessThanOrEqualTo(1));
+ expect(crop.bottom, lessThanOrEqualTo(1));
+ expect(crop.width / crop.height, closeTo(16 / 9, 1e-12));
+ });
+}
+
+void _expectRectClose(Rect actual, Rect expected, {String? reason}) {
+ const tolerance = 1e-12;
+ expect(actual.left, closeTo(expected.left, tolerance), reason: reason);
+ expect(actual.top, closeTo(expected.top, tolerance), reason: reason);
+ expect(actual.right, closeTo(expected.right, tolerance), reason: reason);
+ expect(actual.bottom, closeTo(expected.bottom, tolerance), reason: reason);
+}
diff --git a/mobile/apps/photos/test/ui/tools/editor/ente_video_editor_sub_page_test.dart b/mobile/apps/photos/test/ui/tools/editor/ente_video_editor_sub_page_test.dart
new file mode 100644
index 0000000000..966b7afd1c
--- /dev/null
+++ b/mobile/apps/photos/test/ui/tools/editor/ente_video_editor_sub_page_test.dart
@@ -0,0 +1,70 @@
+import 'dart:io';
+import 'dart:ui';
+
+import 'package:ente_strings/ente_strings.dart';
+import 'package:flutter/material.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:photos/ente_theme_data.dart';
+import 'package:photos/ui/tools/editor/video_editor/ente_video_editor_controller.dart';
+import 'package:photos/ui/tools/editor/video_editor/ente_video_editor_widgets.dart';
+
+void main() {
+ testWidgets('system back restores state while Done keeps it', (tester) async {
+ final controller = EnteVideoEditorController.file(File('unused.mp4'));
+ addTearDown(controller.dispose);
+
+ await tester.pumpWidget(
+ MaterialApp(
+ theme: darkThemeData,
+ localizationsDelegates: StringsLocalizations.localizationsDelegates,
+ supportedLocales: StringsLocalizations.supportedLocales,
+ home: Builder(
+ builder: (context) => TextButton(
+ onPressed: () => Navigator.of(context).push<void>(
+ MaterialPageRoute(builder: (_) => _SubPage(controller)),
+ ),
+ child: const Text('Open'),
+ ),
+ ),
+ ),
+ );
+
+ await tester.tap(find.text('Open'));
+ await tester.pumpAndSettle();
+ await tester.tap(find.byKey(const Key('change-crop')));
+ await tester.binding.handlePopRoute();
+ await tester.pumpAndSettle();
+ expect(controller.minCrop, Offset.zero);
+ expect(controller.maxCrop, const Offset(1, 1));
+
+ await tester.tap(find.text('Open'));
+ await tester.pumpAndSettle();
+ await tester.tap(find.byKey(const Key('change-crop')));
+ await tester.tap(find.text('Done'));
+ await tester.pumpAndSettle();
+ expect(controller.minCrop, const Offset(0.1, 0.2));
+ expect(controller.maxCrop, const Offset(0.8, 0.9));
+ });
+}
+
+class _SubPage extends StatelessWidget {
+ const _SubPage(this.controller);
+
+ final EnteVideoEditorController controller;
+
+ @override
+ Widget build(BuildContext context) {
+ return EnteVideoEditorSubPage(
+ controller: controller,
+ preview: const ColoredBox(color: Colors.black),
+ actions: TextButton(
+ key: const Key('change-crop'),
+ onPressed: () => controller.updateCrop(
+ const Offset(0.1, 0.2),
+ const Offset(0.8, 0.9),
+ ),
+ child: const Text('Change crop'),
+ ),
+ );
+ }
+}
diff --git a/mobile/apps/photos/test/ui/tools/editor/video_editor_contract_test.dart b/mobile/apps/photos/test/ui/tools/editor/video_editor_contract_test.dart
new file mode 100644
index 0000000000..c3e8f7efd2
--- /dev/null
+++ b/mobile/apps/photos/test/ui/tools/editor/video_editor_contract_test.dart
@@ -0,0 +1,82 @@
+import 'dart:io';
+import 'dart:ui';
+
+import 'package:flutter_test/flutter_test.dart';
+import 'package:photos/ui/tools/editor/export_video_service.dart';
+import 'package:photos/ui/tools/editor/video_crop_util.dart';
+import 'package:photos/ui/tools/editor/video_editor/ente_video_editor_controller.dart';
+
+void main() {
+ group('calculateDisplaySpaceCropRectFromData', () {
+ test('keeps encoder dimensions even and within an odd-sized source', () {
+ final crop = VideoCropUtil.calculateDisplaySpaceCropRectFromData(
+ minCrop: const Offset(0.1, 0.1),
+ maxCrop: const Offset(0.9, 0.9),
+ videoSize: const Size(321, 241),
+ );
+
+ expect(crop.left, greaterThanOrEqualTo(0));
+ expect(crop.top, greaterThanOrEqualTo(0));
+ expect(crop.right, lessThanOrEqualTo(321));
+ expect(crop.bottom, lessThanOrEqualTo(241));
+ expect(crop.width.toInt().isEven, isTrue);
+ expect(crop.height.toInt().isEven, isTrue);
+ });
+
+ test('preserves a full even-sized source', () {
+ expect(
+ VideoCropUtil.calculateDisplaySpaceCropRectFromData(
+ minCrop: Offset.zero,
+ maxCrop: const Offset(1, 1),
+ videoSize: const Size(320, 180),
+ ),
+ const Rect.fromLTWH(0, 0, 320, 180),
+ );
+ });
+ });
+
+ group('buildFfmpegVideoFilters', () {
+ test('crops before rotating and normalizes encoder dimensions', () {
+ final filters = buildFfmpegVideoFilters(
+ crop: CropCalculation(x: 10, y: 20, width: 301, height: 199),
+ rotation: 90,
+ );
+ expect(filters, [
+ 'crop=301:199:10:20',
+ 'transpose=1',
+ 'scale=trunc(iw/2)*2:trunc(ih/2)*2',
+ ]);
+ });
+
+ test('represents a half turn without transpose ambiguity', () {
+ expect(buildFfmpegVideoFilters(crop: null, rotation: 180), [
+ 'hflip',
+ 'vflip',
+ 'scale=trunc(iw/2)*2:trunc(ih/2)*2',
+ ]);
+ });
+
+ test('rejects non-quarter-turn rotations', () {
+ expect(
+ () => buildFfmpegVideoFilters(crop: null, rotation: 45),
+ throwsArgumentError,
+ );
+ });
+ });
+
+ test('FFmpeg plan preserves file paths as structured arguments', () {
+ final controller = EnteVideoEditorController.file(
+ File("/tmp/input video's source.mp4"),
+ );
+ addTearDown(controller.dispose);
+
+ final plan = ExportService.createPlan(
+ controller: controller,
+ outputPath: '/tmp/output video.mp4',
+ );
+
+ expect(plan.arguments, contains("/tmp/input video's source.mp4"));
+ expect(plan.arguments, contains('/tmp/output video.mp4'));
+ expect(plan.arguments, containsAllInOrder(['-map', '0:a?']));
+ });
+}
diff --git a/mobile/packages/native_video_editor/lib/native_video_editor.dart b/mobile/packages/native_video_editor/lib/native_video_editor.dart
index 6150e075d3..02eeb3c34d 100644
--- a/mobile/packages/native_video_editor/lib/native_video_editor.dart
+++ b/mobile/packages/native_video_editor/lib/native_video_editor.dart
@@ -3,6 +3,9 @@ import 'dart:io';
import 'package:flutter/services.dart';
+String _normalizedPath(String path) =>
+ File(path).absolute.uri.normalizePath().toFilePath();
+
class NativeVideoEditorException implements Exception {
NativeVideoEditorException(
this.message, {
@@ -24,195 +27,237 @@ class NativeVideoEditorException implements Exception {
class VideoEditResult {
final String outputPath;
final bool isReEncoded;
- final Duration? processingTime;
- VideoEditResult({
- required this.outputPath,
- required this.isReEncoded,
- this.processingTime,
- });
+ const VideoEditResult({required this.outputPath, required this.isReEncoded});
}
-class VideoTrimParams {
- final String inputPath;
- final String outputPath;
- final Duration startTime;
- final Duration endTime;
+enum VideoFramePolicy { precise, nearestSync }
- VideoTrimParams({
- required this.inputPath,
- required this.outputPath,
- required this.startTime,
- required this.endTime,
+class NativeVideoInfo {
+ const NativeVideoInfo({
+ required this.duration,
+ required this.width,
+ required this.height,
+ required this.displayWidth,
+ required this.displayHeight,
+ required this.rotationDegrees,
+ this.bitrate,
+ this.frameRate,
});
- Map<String, dynamic> toMap() => {
- 'inputPath': inputPath,
- 'outputPath': outputPath,
- 'startTimeMs': startTime.inMilliseconds,
- 'endTimeMs': endTime.inMilliseconds,
- };
-}
+ factory NativeVideoInfo.fromMap(Map<dynamic, dynamic> map) {
+ int requiredInt(String key) {
+ final value = map[key];
+ if (value is! num) {
+ throw FormatException('Missing numeric video metadata: $key');
+ }
+ return value.toInt();
+ }
-class VideoRotateParams {
- final String inputPath;
- final String outputPath;
- final int degrees; // Must be 90, 180, or 270
+ int? positiveInt(String key) {
+ final value = map[key];
+ if (value is! num || value <= 0) return null;
+ return value.toInt();
+ }
- VideoRotateParams({
- required this.inputPath,
- required this.outputPath,
- required this.degrees,
- }) : assert(degrees == 90 || degrees == 180 || degrees == 270);
+ double? positiveDouble(String key) {
+ final value = map[key];
+ if (value is! num || value <= 0) return null;
+ return value.toDouble();
+ }
- Map<String, dynamic> toMap() => {
- 'inputPath': inputPath,
- 'outputPath': outputPath,
- 'degrees': degrees,
- };
-}
+ final rotation = requiredInt('rotation') % 360;
+ final normalizedRotation = rotation < 0 ? rotation + 360 : rotation;
+ final durationMs = requiredInt('duration');
+ final width = requiredInt('width');
+ final height = requiredInt('height');
+ final displayWidth = requiredInt('displayWidth');
+ final displayHeight = requiredInt('displayHeight');
+ if (durationMs < 0 ||
+ width <= 0 ||
+ height <= 0 ||
+ displayWidth <= 0 ||
+ displayHeight <= 0) {
+ throw const FormatException('Video metadata contains invalid dimensions');
+ }
+ if (!const {0, 90, 180, 270}.contains(normalizedRotation)) {
+ throw const FormatException('Video metadata has unsupported rotation');
+ }
+ return NativeVideoInfo(
+ duration: Duration(milliseconds: durationMs),
+ width: width,
+ height: height,
+ displayWidth: displayWidth,
+ displayHeight: displayHeight,
+ rotationDegrees: normalizedRotation,
+ bitrate: positiveInt('bitrate'),
+ frameRate: positiveDouble('frameRate'),
+ );
+ }
-class VideoCropParams {
- final String inputPath;
- final String outputPath;
- final int x;
- final int y;
+ final Duration duration;
final int width;
final int height;
- final bool forceReEncode;
+ final int displayWidth;
+ final int displayHeight;
- VideoCropParams({
+ /// Clockwise quarter-turn rotation required to display encoded pixels.
+ final int rotationDegrees;
+ final int? bitrate;
+ final double? frameRate;
+}
+
+class VideoFrameRequest {
+ const VideoFrameRequest({
required this.inputPath,
required this.outputPath,
- required this.x,
- required this.y,
- required this.width,
- required this.height,
- this.forceReEncode = false,
+ required this.position,
+ required this.maxWidth,
+ required this.maxHeight,
+ this.quality = 80,
+ this.policy = VideoFramePolicy.nearestSync,
});
+ final String inputPath;
+ final String outputPath;
+ final Duration position;
+ final int maxWidth;
+ final int maxHeight;
+ final int quality;
+ final VideoFramePolicy policy;
+
Map<String, dynamic> toMap() => {
'inputPath': inputPath,
'outputPath': outputPath,
- 'x': x,
- 'y': y,
- 'width': width,
- 'height': height,
- 'forceReEncode': forceReEncode,
+ 'positionMs': position.inMilliseconds,
+ 'maxWidth': maxWidth,
+ 'maxHeight': maxHeight,
+ 'quality': quality,
+ 'policy': policy.name,
};
}
-class NativeVideoEditor {
- static const MethodChannel _channel = MethodChannel('native_video_editor');
- static const EventChannel _progressChannel = EventChannel(
- 'native_video_editor/progress',
- );
+class VideoFrameResult {
+ const VideoFrameResult({
+ required this.outputPath,
+ required this.width,
+ required this.height,
+ });
- /// Trim video without re-encoding when possible
- /// Returns the output file path
- static Future<VideoEditResult> trimVideo(VideoTrimParams params) async {
- _ensureInputPathExists(params.inputPath);
- if (params.startTime >= params.endTime) {
- throw ArgumentError('startTime must be earlier than endTime');
+ factory VideoFrameResult.fromMap(
+ Map<dynamic, dynamic> map, {
+ required String expectedOutputPath,
+ required int maxWidth,
+ required int maxHeight,
+ }) {
+ final outputPath = map['outputPath'];
+ final width = map['width'];
+ final height = map['height'];
+ if (outputPath is! String || width is! num || height is! num) {
+ throw const FormatException('Frame result is missing required values');
}
-
- try {
- final stopwatch = Stopwatch()..start();
-
- final result = await _channel.invokeMethod<Map<dynamic, dynamic>>(
- 'trimVideo',
- params.toMap(),
- );
-
- stopwatch.stop();
-
- if (result == null) {
- throw Exception('Failed to trim video: no result');
- }
-
- return VideoEditResult(
- outputPath: result['outputPath'] as String,
- isReEncoded: result['isReEncoded'] as bool? ?? false,
- processingTime: stopwatch.elapsed,
- );
- } on PlatformException catch (e) {
- throw NativeVideoEditorException(
- 'Failed to trim video: ${e.message}',
- code: e.code,
- details: e.details,
- cause: e,
- );
+ final parsedWidth = width.toInt();
+ final parsedHeight = height.toInt();
+ if (_normalizedPath(outputPath) != _normalizedPath(expectedOutputPath)) {
+ throw const FormatException('Frame result has an unexpected output path');
+ }
+ if (parsedWidth <= 0 ||
+ parsedHeight <= 0 ||
+ parsedWidth > maxWidth ||
+ parsedHeight > maxHeight) {
+ throw const FormatException('Frame result has invalid dimensions');
}
+ return VideoFrameResult(
+ outputPath: outputPath,
+ width: parsedWidth,
+ height: parsedHeight,
+ );
}
- /// Rotate video using metadata when possible (Android) or transform (iOS)
- /// Avoids re-encoding when possible
- static Future<VideoEditResult> rotateVideo(VideoRotateParams params) async {
- _ensureInputPathExists(params.inputPath);
- _validateRotationDegrees(params.degrees);
-
- try {
- final stopwatch = Stopwatch()..start();
-
- final result = await _channel.invokeMethod<Map<dynamic, dynamic>>(
- 'rotateVideo',
- params.toMap(),
- );
-
- stopwatch.stop();
+ final String outputPath;
+ final int width;
+ final int height;
+}
- if (result == null) {
- throw Exception('Failed to rotate video: no result');
- }
+class VideoFrameExtractionResult {
+ const VideoFrameExtractionResult({
+ required this.videoInfo,
+ required this.frames,
+ });
- return VideoEditResult(
- outputPath: result['outputPath'] as String,
- isReEncoded: result['isReEncoded'] as bool? ?? false,
- processingTime: stopwatch.elapsed,
- );
- } on PlatformException catch (e) {
- throw NativeVideoEditorException(
- 'Failed to rotate video: ${e.message}',
- code: e.code,
- details: e.details,
- cause: e,
+ factory VideoFrameExtractionResult.fromMap(
+ Map<dynamic, dynamic> map, {
+ required List<String> expectedOutputPaths,
+ required int maxWidth,
+ required int maxHeight,
+ }) {
+ final frameMaps = map['frames'];
+ final videoInfo = map['videoInfo'];
+ if (frameMaps is! List<dynamic> ||
+ frameMaps.length != expectedOutputPaths.length ||
+ videoInfo is! Map<dynamic, dynamic>) {
+ throw const FormatException(
+ 'Frame extraction returned an invalid result',
);
}
+ return VideoFrameExtractionResult(
+ videoInfo: NativeVideoInfo.fromMap(videoInfo),
+ frames: [
+ for (var index = 0; index < frameMaps.length; index++)
+ VideoFrameResult.fromMap(
+ frameMaps[index] as Map<dynamic, dynamic>,
+ expectedOutputPath: expectedOutputPaths[index],
+ maxWidth: maxWidth,
+ maxHeight: maxHeight,
+ ),
+ ],
+ );
}
- /// Crop video - may require re-encoding depending on the format
- static Future<VideoEditResult> cropVideo(VideoCropParams params) async {
- _ensureInputPathExists(params.inputPath);
- _validateCropDimensions(params.width, params.height);
-
- try {
- final stopwatch = Stopwatch()..start();
+ final NativeVideoInfo videoInfo;
+ final List<VideoFrameResult> frames;
+}
- final result = await _channel.invokeMethod<Map<dynamic, dynamic>>(
- 'cropVideo',
- params.toMap(),
- );
+class VideoTimelineRequest {
+ const VideoTimelineRequest({
+ required this.requestId,
+ required this.inputPath,
+ required this.outputPaths,
+ required this.positions,
+ required this.maxWidth,
+ required this.maxHeight,
+ this.quality = 70,
+ this.policy = VideoFramePolicy.nearestSync,
+ });
- stopwatch.stop();
+ final String requestId;
+ final String inputPath;
+ final List<String> outputPaths;
+ final List<Duration> positions;
+ final int maxWidth;
+ final int maxHeight;
+ final int quality;
+ final VideoFramePolicy policy;
- if (result == null) {
- throw Exception('Failed to crop video: no result');
- }
+ Map<String, dynamic> toMap() => {
+ 'requestId': requestId,
+ 'inputPath': inputPath,
+ 'outputPaths': outputPaths,
+ 'positionsMs': positions
+ .map((position) => position.inMilliseconds)
+ .toList(),
+ 'maxWidth': maxWidth,
+ 'maxHeight': maxHeight,
+ 'quality': quality,
+ 'policy': policy.name,
+ };
+}
- return VideoEditResult(
- outputPath: result['outputPath'] as String,
- isReEncoded: result['isReEncoded'] as bool? ?? true,
- processingTime: stopwatch.elapsed,
- );
- } on PlatformException catch (e) {
- throw NativeVideoEditorException(
- 'Failed to crop video: ${e.message}',
- code: e.code,
- details: e.details,
- cause: e,
- );
- }
- }
+class NativeVideoEditor {
+ static const MethodChannel _channel = MethodChannel('native_video_editor');
+ static const EventChannel _progressChannel = EventChannel(
+ 'native_video_editor/progress',
+ );
/// Combined operation: trim, rotate, and crop in a single pass
/// More efficient when multiple operations are needed
@@ -226,136 +271,267 @@ class NativeVideoEditor {
void Function(double progress)? onProgress,
}) async {
_ensureInputPathExists(inputPath);
- if (trimStart != null && trimEnd != null && trimStart >= trimEnd) {
- throw ArgumentError('trimStart must be earlier than trimEnd');
+ _validateOutputPath(inputPath: inputPath, outputPath: outputPath);
+ _ensureOutputDirectoryExists(outputPath);
+ if ((trimStart == null) != (trimEnd == null)) {
+ throw ArgumentError('trimStart and trimEnd must be provided together');
+ }
+ if (trimStart != null && (trimStart.isNegative || trimStart >= trimEnd!)) {
+ throw ArgumentError(
+ 'trimStart must be non-negative and earlier than trimEnd',
+ );
}
if (rotateDegrees != null && rotateDegrees != 0) {
_validateRotationDegrees(rotateDegrees);
}
- if (cropRect != null && (cropRect.width <= 0 || cropRect.height <= 0)) {
- throw ArgumentError('cropRect must have positive width and height');
+ if (cropRect != null &&
+ (!cropRect.left.isFinite ||
+ !cropRect.top.isFinite ||
+ !cropRect.width.isFinite ||
+ !cropRect.height.isFinite ||
+ cropRect.left < 0 ||
+ cropRect.top < 0 ||
+ cropRect.width < 1 ||
+ cropRect.height < 1 ||
+ cropRect.left != cropRect.left.truncateToDouble() ||
+ cropRect.top != cropRect.top.truncateToDouble() ||
+ cropRect.width != cropRect.width.truncateToDouble() ||
+ cropRect.height != cropRect.height.truncateToDouble())) {
+ throw ArgumentError('cropRect must contain positive whole pixels');
}
+ final params = <String, dynamic>{
+ 'inputPath': inputPath,
+ 'outputPath': outputPath,
+ if (trimStart != null) ...{
+ 'trimStartMs': trimStart.inMilliseconds,
+ 'trimEndMs': trimEnd!.inMilliseconds,
+ },
+ 'rotateDegrees': ?rotateDegrees,
+ if (cropRect != null) ...{
+ 'cropX': cropRect.left.toInt(),
+ 'cropY': cropRect.top.toInt(),
+ 'cropWidth': cropRect.width.toInt(),
+ 'cropHeight': cropRect.height.toInt(),
+ },
+ };
+ final progressSubscription = onProgress == null
+ ? null
+ : _progressChannel.receiveBroadcastStream().listen((dynamic event) {
+ if (event is num) onProgress(event.toDouble());
+ }, onError: (_) {});
try {
- final stopwatch = Stopwatch()..start();
-
- final params = <String, dynamic>{
- 'inputPath': inputPath,
- 'outputPath': outputPath,
- };
-
- if (trimStart != null && trimEnd != null) {
- params['trimStartMs'] = trimStart.inMilliseconds;
- params['trimEndMs'] = trimEnd.inMilliseconds;
+ final result = await _invokeMap(
+ 'processVideo',
+ params,
+ operation: 'process video',
+ );
+ final resultOutputPath = result['outputPath'];
+ final isReEncoded = result['isReEncoded'];
+ if (resultOutputPath is! String ||
+ isReEncoded is! bool ||
+ _normalizedPath(resultOutputPath) != _normalizedPath(outputPath)) {
+ throw const FormatException(
+ 'Video processing returned an invalid result',
+ );
}
+ return VideoEditResult(
+ outputPath: resultOutputPath,
+ isReEncoded: isReEncoded,
+ );
+ } finally {
+ await progressSubscription?.cancel();
+ }
+ }
- if (rotateDegrees != null) {
- params['rotateDegrees'] = rotateDegrees;
- }
+ static Future<VideoFrameExtractionResult> extractFrame(
+ VideoFrameRequest request,
+ ) async {
+ _validateFrameRequest(request);
+ final result = await _invokeMap(
+ 'extractFrame',
+ request.toMap(),
+ operation: 'extract video frame',
+ );
+ return VideoFrameExtractionResult.fromMap(
+ result,
+ expectedOutputPaths: [request.outputPath],
+ maxWidth: request.maxWidth,
+ maxHeight: request.maxHeight,
+ );
+ }
- if (cropRect != null) {
- params['cropX'] = cropRect.left.toInt();
- params['cropY'] = cropRect.top.toInt();
- params['cropWidth'] = cropRect.width.toInt();
- params['cropHeight'] = cropRect.height.toInt();
+ static Future<VideoFrameExtractionResult> extractTimeline(
+ VideoTimelineRequest request,
+ ) async {
+ _ensureInputPathExists(request.inputPath);
+ if (request.requestId.isEmpty) {
+ throw ArgumentError.value(request.requestId, 'requestId', 'is empty');
+ }
+ if (request.positions.isEmpty) {
+ throw ArgumentError.value(request.positions, 'positions', 'is empty');
+ }
+ if (request.positions.length != request.outputPaths.length) {
+ throw ArgumentError('positions and outputPaths must have equal lengths');
+ }
+ _validateFrameOptions(
+ maxWidth: request.maxWidth,
+ maxHeight: request.maxHeight,
+ quality: request.quality,
+ );
+ final outputPaths = <String>{};
+ for (var index = 0; index < request.positions.length; index++) {
+ if (request.positions[index].isNegative) {
+ throw ArgumentError.value(
+ request.positions[index],
+ 'positions[$index]',
+ 'is negative',
+ );
}
-
- StreamSubscription? progressSubscription;
- if (onProgress != null) {
- progressSubscription = _progressChannel.receiveBroadcastStream().listen(
- (dynamic event) {
- if (event is double) {
- onProgress(event);
- } else if (event is int) {
- onProgress(event.toDouble());
- }
- },
- onError: (error) {},
+ _validateOutputPath(
+ inputPath: request.inputPath,
+ outputPath: request.outputPaths[index],
+ );
+ final absoluteOutputPath = _normalizedPath(request.outputPaths[index]);
+ if (!outputPaths.add(absoluteOutputPath)) {
+ throw ArgumentError.value(
+ request.outputPaths[index],
+ 'outputPaths[$index]',
+ 'is duplicated',
);
}
+ _ensureOutputDirectoryExists(request.outputPaths[index]);
+ }
- try {
- final result = await _channel.invokeMethod<Map<dynamic, dynamic>>(
- 'processVideo',
- params,
- );
+ final result = await _invokeMap(
+ 'extractTimeline',
+ request.toMap(),
+ operation: 'extract video timeline',
+ );
+ return VideoFrameExtractionResult.fromMap(
+ result,
+ expectedOutputPaths: request.outputPaths,
+ maxWidth: request.maxWidth,
+ maxHeight: request.maxHeight,
+ );
+ }
- stopwatch.stop();
+ static Future<void> cancelFrameExtraction(String requestId) async {
+ if (requestId.isEmpty) return;
+ await _invokeVoid('cancelFrameExtraction', {
+ 'requestId': requestId,
+ }, operation: 'cancel frame extraction');
+ }
- if (result == null) {
- throw Exception('Failed to process video: no result');
- }
+ static Future<NativeVideoInfo> inspectVideo(String videoPath) async {
+ _ensureInputPathExists(videoPath);
+ final result = await _invokeMap('getVideoInfo', {
+ 'videoPath': videoPath,
+ }, operation: 'inspect video');
+ return NativeVideoInfo.fromMap(result);
+ }
- return VideoEditResult(
- outputPath: result['outputPath'] as String,
- isReEncoded: result['isReEncoded'] as bool? ?? false,
- processingTime: stopwatch.elapsed,
- );
- } finally {
- await progressSubscription?.cancel();
- }
- } on PlatformException catch (e) {
- throw NativeVideoEditorException(
- 'Failed to process video: ${e.message}',
- code: e.code,
- details: e.details,
- cause: e,
- );
- }
+ /// Cancel any ongoing video processing
+ static Future<void> cancelProcessing() async {
+ await _invokeVoid('cancelProcessing', null, operation: 'cancel processing');
}
- /// Get video information without processing
- static Future<Map<String, dynamic>> getVideoInfo(String videoPath) async {
+ static Future<Map<dynamic, dynamic>> _invokeMap(
+ String method,
+ Map<String, dynamic> arguments, {
+ required String operation,
+ }) async {
try {
final result = await _channel.invokeMethod<Map<dynamic, dynamic>>(
- 'getVideoInfo',
- {'videoPath': videoPath},
+ method,
+ arguments,
);
-
if (result == null) {
- throw Exception('Failed to get video info: no result');
+ throw NativeVideoEditorException('Failed to $operation: no result');
}
-
- return Map<String, dynamic>.from(result);
- } on PlatformException catch (e) {
- throw NativeVideoEditorException(
- 'Failed to get video info: ${e.message}',
- code: e.code,
- details: e.details,
- cause: e,
- );
+ return result;
+ } on PlatformException catch (error) {
+ throw _platformException(operation, error);
}
}
- /// Cancel any ongoing video processing
- static Future<void> cancelProcessing() async {
+ static Future<void> _invokeVoid(
+ String method,
+ Map<String, dynamic>? arguments, {
+ required String operation,
+ }) async {
try {
- await _channel.invokeMethod('cancelProcessing');
- } on PlatformException catch (e) {
- throw NativeVideoEditorException(
- 'Failed to cancel processing: ${e.message}',
- code: e.code,
- details: e.details,
- cause: e,
- );
+ await _channel.invokeMethod<void>(method, arguments);
+ } on PlatformException catch (error) {
+ throw _platformException(operation, error);
}
}
+ static NativeVideoEditorException _platformException(
+ String operation,
+ PlatformException error,
+ ) => NativeVideoEditorException(
+ 'Failed to $operation: ${error.message}',
+ code: error.code,
+ details: error.details,
+ cause: error,
+ );
+
static void _ensureInputPathExists(String inputPath) {
if (!File(inputPath).existsSync()) {
throw ArgumentError('Input file does not exist: $inputPath');
}
}
- static void _validateRotationDegrees(int degrees) {
- if (degrees != 90 && degrees != 180 && degrees != 270) {
- throw ArgumentError('Rotation degrees must be 90, 180, or 270');
+ static void _validateFrameRequest(VideoFrameRequest request) {
+ _ensureInputPathExists(request.inputPath);
+ if (request.position.isNegative) {
+ throw ArgumentError.value(request.position, 'position', 'is negative');
+ }
+ _validateOutputPath(
+ inputPath: request.inputPath,
+ outputPath: request.outputPath,
+ );
+ _ensureOutputDirectoryExists(request.outputPath);
+ _validateFrameOptions(
+ maxWidth: request.maxWidth,
+ maxHeight: request.maxHeight,
+ quality: request.quality,
+ );
+ }
+
+ static void _validateFrameOptions({
+ required int maxWidth,
+ required int maxHeight,
+ required int quality,
+ }) {
+ if (maxWidth <= 0 || maxHeight <= 0) {
+ throw ArgumentError('maxWidth and maxHeight must both be positive');
+ }
+ if (quality < 1 || quality > 100) {
+ throw RangeError.range(quality, 1, 100, 'quality');
}
}
- static void _validateCropDimensions(int width, int height) {
- if (width <= 0 || height <= 0) {
- throw ArgumentError('Crop dimensions must be greater than zero');
+ static void _ensureOutputDirectoryExists(String outputPath) {
+ final parent = File(outputPath).parent;
+ if (!parent.existsSync()) {
+ throw ArgumentError('Output directory does not exist: ${parent.path}');
+ }
+ }
+
+ static void _validateOutputPath({
+ required String inputPath,
+ required String outputPath,
+ }) {
+ if (_normalizedPath(inputPath) == _normalizedPath(outputPath)) {
+ throw ArgumentError('Output path must differ from the input path');
+ }
+ }
+
+ static void _validateRotationDegrees(int degrees) {
+ if (degrees != 90 && degrees != 180 && degrees != 270) {
+ throw ArgumentError('Rotation degrees must be 90, 180, or 270');
}
}
}
diff --git a/mobile/packages/native_video_editor/test/native_video_editor_test.dart b/mobile/packages/native_video_editor/test/native_video_editor_test.dart
new file mode 100644
index 0000000000..8a0f3ef981
--- /dev/null
+++ b/mobile/packages/native_video_editor/test/native_video_editor_test.dart
@@ -0,0 +1,314 @@
+import 'dart:io';
+
+import 'package:flutter/services.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:native_video_editor/native_video_editor.dart';
+
+void main() {
+ TestWidgetsFlutterBinding.ensureInitialized();
+ const channel = MethodChannel('native_video_editor');
+ late Directory directory;
+ late File input;
+ MethodCall? lastCall;
+ Map<dynamic, dynamic>? extractionResultOverride;
+ Map<dynamic, dynamic>? processResultOverride;
+
+ setUp(() async {
+ directory = await Directory.systemTemp.createTemp('native-video-test-');
+ input = await File('${directory.path}/input.mp4').writeAsBytes([0]);
+ extractionResultOverride = null;
+ processResultOverride = null;
+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
+ .setMockMethodCallHandler(channel, (call) async {
+ lastCall = call;
+ switch (call.method) {
+ case 'processVideo':
+ final arguments = call.arguments as Map<dynamic, dynamic>;
+ return processResultOverride ??
+ {'outputPath': arguments['outputPath'], 'isReEncoded': true};
+ case 'getVideoInfo':
+ return _videoInfoMap;
+ case 'extractFrame':
+ case 'extractTimeline':
+ if (extractionResultOverride != null) {
+ return extractionResultOverride;
+ }
+ final arguments = call.arguments as Map<dynamic, dynamic>;
+ final outputPaths = call.method == 'extractFrame'
+ ? [arguments['outputPath'] as String]
+ : (arguments['outputPaths'] as List<dynamic>).cast<String>();
+ return {
+ 'videoInfo': _videoInfoMap,
+ 'frames': [
+ for (var index = 0; index < outputPaths.length; index++)
+ {
+ 'outputPath': outputPaths[index],
+ 'width': 120,
+ 'height': 90,
+ },
+ ],
+ };
+ case 'cancelFrameExtraction':
+ return null;
+ }
+ throw PlatformException(code: 'UNEXPECTED_METHOD');
+ });
+ });
+
+ tearDown(() async {
+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
+ .setMockMethodCallHandler(channel, null);
+ await directory.delete(recursive: true);
+ });
+
+ test('inspectVideo returns normalized display metadata', () async {
+ final info = await NativeVideoEditor.inspectVideo(input.path);
+
+ expect(info.duration, const Duration(milliseconds: 4200));
+ expect(info.width, 1920);
+ expect(info.height, 1080);
+ expect(info.displayWidth, 1080);
+ expect(info.displayHeight, 1920);
+ expect(info.rotationDegrees, 270);
+ expect(info.bitrate, 8000000);
+ expect(info.frameRate, 29.97);
+ });
+
+ test('unavailable optional metadata is represented as null', () {
+ final info = NativeVideoInfo.fromMap({
+ ..._videoInfoMap,
+ 'bitrate': 0,
+ 'frameRate': -1,
+ });
+
+ expect(info.bitrate, isNull);
+ expect(info.frameRate, isNull);
+ });
+
+ test('non-quarter-turn metadata rotation is rejected', () {
+ expect(
+ () => NativeVideoInfo.fromMap({..._videoInfoMap, 'rotation': 45}),
+ throwsFormatException,
+ );
+ });
+
+ test('extractFrame sends bounded JPEG policy and parses result', () async {
+ final output = '${directory.path}/frame.jpg';
+ final extraction = await NativeVideoEditor.extractFrame(
+ VideoFrameRequest(
+ inputPath: input.path,
+ outputPath: output,
+ position: const Duration(milliseconds: 900),
+ maxWidth: 320,
+ maxHeight: 180,
+ quality: 76,
+ policy: VideoFramePolicy.precise,
+ ),
+ );
+
+ expect(lastCall?.method, 'extractFrame');
+ expect(lastCall?.arguments, containsPair('maxWidth', 320));
+ expect(lastCall?.arguments, containsPair('maxHeight', 180));
+ expect(lastCall?.arguments, containsPair('quality', 76));
+ expect(lastCall?.arguments, containsPair('policy', 'precise'));
+ expect(extraction.frames.single.outputPath, output);
+ expect(extraction.frames.single.width, 120);
+ expect(extraction.videoInfo.displayHeight, 1920);
+ });
+
+ test('extractTimeline preserves request and timestamp ordering', () async {
+ final outputs = [
+ '${directory.path}/0.jpg',
+ '${directory.path}/1.jpg',
+ '${directory.path}/2.jpg',
+ ];
+ final positions = [
+ Duration.zero,
+ const Duration(milliseconds: 2100),
+ const Duration(milliseconds: 4199),
+ ];
+
+ final extraction = await NativeVideoEditor.extractTimeline(
+ VideoTimelineRequest(
+ requestId: 'trim-1',
+ inputPath: input.path,
+ outputPaths: outputs,
+ positions: positions,
+ maxWidth: 144,
+ maxHeight: 120,
+ ),
+ );
+
+ expect(lastCall?.arguments, containsPair('requestId', 'trim-1'));
+ expect(
+ extraction.frames.map((frame) => frame.outputPath),
+ orderedEquals(outputs),
+ );
+ });
+
+ test('invalid pixel bounds fail before invoking native code', () async {
+ lastCall = null;
+
+ await expectLater(
+ NativeVideoEditor.extractFrame(
+ VideoFrameRequest(
+ inputPath: input.path,
+ outputPath: '${directory.path}/invalid.jpg',
+ position: Duration.zero,
+ maxWidth: 0,
+ maxHeight: 120,
+ ),
+ ),
+ throwsArgumentError,
+ );
+ expect(lastCall, isNull);
+ });
+
+ test('timeline requires one destination for every timestamp', () async {
+ await expectLater(
+ NativeVideoEditor.extractTimeline(
+ VideoTimelineRequest(
+ requestId: 'trim-2',
+ inputPath: input.path,
+ outputPaths: ['${directory.path}/only-one.jpg'],
+ positions: [Duration.zero, const Duration(seconds: 1)],
+ maxWidth: 144,
+ maxHeight: 120,
+ ),
+ ),
+ throwsArgumentError,
+ );
+ });
+
+ test('frame extraction cannot overwrite its input', () async {
+ await expectLater(
+ NativeVideoEditor.extractFrame(
+ VideoFrameRequest(
+ inputPath: input.path,
+ outputPath: input.path,
+ position: Duration.zero,
+ maxWidth: 144,
+ maxHeight: 120,
+ ),
+ ),
+ throwsArgumentError,
+ );
+ });
+
+ test('timeline destinations must be unique', () async {
+ final output = '${directory.path}/same.jpg';
+ await expectLater(
+ NativeVideoEditor.extractTimeline(
+ VideoTimelineRequest(
+ requestId: 'trim-duplicate',
+ inputPath: input.path,
+ outputPaths: [output, output],
+ positions: [Duration.zero, const Duration(seconds: 1)],
+ maxWidth: 144,
+ maxHeight: 120,
+ ),
+ ),
+ throwsArgumentError,
+ );
+ });
+
+ test('frame result must match the requested output path', () async {
+ extractionResultOverride = {
+ 'videoInfo': _videoInfoMap,
+ 'frames': [
+ {
+ 'outputPath': '${directory.path}/wrong.jpg',
+ 'width': 120,
+ 'height': 90,
+ },
+ ],
+ };
+
+ await expectLater(
+ NativeVideoEditor.extractFrame(
+ VideoFrameRequest(
+ inputPath: input.path,
+ outputPath: '${directory.path}/expected.jpg',
+ position: Duration.zero,
+ maxWidth: 144,
+ maxHeight: 120,
+ ),
+ ),
+ throwsFormatException,
+ );
+ });
+
+ test('timeline result must contain every requested frame', () async {
+ extractionResultOverride = {
+ 'videoInfo': _videoInfoMap,
+ 'frames': const <Map<String, dynamic>>[],
+ };
+
+ await expectLater(
+ NativeVideoEditor.extractTimeline(
+ VideoTimelineRequest(
+ requestId: 'trim-incomplete',
+ inputPath: input.path,
+ outputPaths: ['${directory.path}/expected.jpg'],
+ positions: [Duration.zero],
+ maxWidth: 144,
+ maxHeight: 120,
+ ),
+ ),
+ throwsFormatException,
+ );
+ });
+
+ test('processing cannot overwrite its input', () async {
+ lastCall = null;
+
+ await expectLater(
+ NativeVideoEditor.processVideo(
+ inputPath: input.path,
+ outputPath: input.path,
+ ),
+ throwsArgumentError,
+ );
+ expect(lastCall, isNull);
+ });
+
+ test('processing requires both trim boundaries', () async {
+ lastCall = null;
+
+ await expectLater(
+ NativeVideoEditor.processVideo(
+ inputPath: input.path,
+ outputPath: '${directory.path}/output.mp4',
+ trimStart: Duration.zero,
+ ),
+ throwsArgumentError,
+ );
+ expect(lastCall, isNull);
+ });
+
+ test('processing result must match the requested output path', () async {
+ processResultOverride = {
+ 'outputPath': '${directory.path}/wrong.mp4',
+ 'isReEncoded': true,
+ };
+
+ await expectLater(
+ NativeVideoEditor.processVideo(
+ inputPath: input.path,
+ outputPath: '${directory.path}/expected.mp4',
+ ),
+ throwsFormatException,
+ );
+ });
+}
+
+const _videoInfoMap = <String, dynamic>{
+ 'duration': 4200,
+ 'width': 1920,
+ 'height': 1080,
+ 'displayWidth': 1080,
+ 'displayHeight': 1920,
+ 'rotation': -90,
+ 'bitrate': 8000000,
+ 'frameRate': 29.97,
+};
diff --git a/mobile/pubspec.lock b/mobile/pubspec.lock
index 0d39ce3cab..61f94c7616 100644
--- a/mobile/pubspec.lock
+++ b/mobile/pubspec.lock
@@ -2878,14 +2878,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.10.1"
- transparent_image:
- dependency: transitive
- description:
- name: transparent_image
- sha256: e8991d955a2094e197ca24c645efec2faf4285772a4746126ca12875e54ca02f
- url: "https://pub.dev"
- source: hosted
- version: "2.0.1"
tray_manager:
dependency: transitive
description:
@@ -3054,15 +3046,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.2.0"
- video_editor:
- dependency: transitive
- description:
- path: "."
- ref: "1eeb18e2b1ce36bd8ca70178c0d4b485e9982257"
- resolved-ref: "1eeb18e2b1ce36bd8ca70178c0d4b485e9982257"
- url: "https://github.com/ente/video_editor_fork.git"
- source: git
- version: "3.0.0"
video_player:
dependency: transitive
description:
@@ -3104,14 +3087,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.4.0"
- video_thumbnail:
- dependency: transitive
- description:
- name: video_thumbnail
- sha256: "181a0c205b353918954a881f53a3441476b9e301641688a581e0c13f00dc588b"
- url: "https://pub.dev"
- source: hosted
- version: "0.5.6"
visibility_detector:
dependency: transitive
description: