Files
tatuchat/lib/utils/resize_video.dart
Welton Moura 0037b0b9cf
Some checks failed
Main Deploy Workflow / deploy_web (push) Has been cancelled
Main Deploy Workflow / deploy_playstore_internal (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
Initial commit
2026-09-11 15:59:56 -03:00

66 lines
2.1 KiB
Dart

// SPDX-FileCopyrightText: 2019-Present Christian Kußowski
// SPDX-FileCopyrightText: 2019-Present Contributors to FluffyChat
//
// SPDX-License-Identifier: AGPL-3.0-or-later
import 'package:cross_file/cross_file.dart';
import 'package:tatchat/utils/platform_infos.dart';
import 'package:light_compressor_v2/light_compressor_v2.dart';
import 'package:matrix/matrix.dart';
import 'package:mime/mime.dart';
extension ResizeImage on XFile {
Future<MatrixVideoFile> getVideoInfo({bool compress = true}) async {
MediaInfo? mediaInfo;
String? destinationPath;
try {
if (PlatformInfos.isMobile || PlatformInfos.isMacOS) {
final result = await LightCompressor().compressVideo(
path: path,
videoQuality: VideoQuality.medium,
android: AndroidConfig(isSharedStorage: false),
ios: IOSConfig(saveInGallery: false),
video: Video(videoName: name, targetSizeMb: 5),
);
if (result is OnFailure) {
throw result.message;
}
if (result is OnSuccess) {
destinationPath = result.destinationPath;
}
if (destinationPath != null) {
mediaInfo = await LightCompressor().getMediaInfo(destinationPath);
}
}
} catch (e, s) {
Logs().w('Error while fetching video media info', e, s);
}
return MatrixVideoFile(
bytes: destinationPath != null
? await XFile(destinationPath).readAsBytes()
: await readAsBytes(),
name: name,
mimeType: destinationPath != null
? lookupMimeType(destinationPath)
: mimeType,
width: mediaInfo?.width,
height: mediaInfo?.height,
duration: mediaInfo?.duration?.inMilliseconds,
);
}
Future<MatrixImageFile?> getVideoThumbnail() async {
if (!PlatformInfos.isMobile) return null;
try {
final thumbnailPath = await LightCompressor().getVideoThumbnail(path);
final bytes = await XFile(thumbnailPath).readAsBytes();
return MatrixImageFile(bytes: bytes, name: name);
} catch (e, s) {
Logs().w('Error while compressing video', e, s);
}
return null;
}
}