feat: Enable compression and thumbnails for videos
This commit is contained in:
parent
f5124aa503
commit
ab9d7fcc7d
6 changed files with 72 additions and 7 deletions
|
|
@ -1,5 +1,6 @@
|
|||
//@dart=2.12
|
||||
|
||||
import 'dart:io';
|
||||
import 'dart:math' as math;
|
||||
import 'dart:typed_data';
|
||||
|
||||
|
|
@ -8,11 +9,60 @@ import 'package:flutter/foundation.dart';
|
|||
import 'package:blurhash_dart/blurhash_dart.dart';
|
||||
import 'package:image/image.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:video_compress/video_compress.dart';
|
||||
|
||||
import 'package:fluffychat/utils/platform_infos.dart';
|
||||
import 'package:fluffychat/utils/sentry_controller.dart';
|
||||
|
||||
extension ResizeImage on MatrixFile {
|
||||
static const int max = 1200;
|
||||
static const int quality = 40;
|
||||
|
||||
Future<MatrixFile> resizeVideo() async {
|
||||
if (!PlatformInfos.isMobile) return this;
|
||||
final tmpDir = await getTemporaryDirectory();
|
||||
final tmpFile = File(tmpDir.path + name);
|
||||
if (await tmpFile.exists() == false) {
|
||||
await tmpFile.writeAsBytes(bytes);
|
||||
try {
|
||||
final mediaInfo = await VideoCompress.compressVideo(tmpFile.path);
|
||||
if (mediaInfo == null) return this;
|
||||
return MatrixVideoFile(
|
||||
bytes: await tmpFile.readAsBytes(),
|
||||
name: name,
|
||||
mimeType: mimeType,
|
||||
width: mediaInfo.width,
|
||||
height: mediaInfo.height,
|
||||
duration: mediaInfo.duration?.round(),
|
||||
);
|
||||
} catch (e, s) {
|
||||
SentryController.captureException(e, s);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
Future<MatrixImageFile?> getVideoThumbnail() async {
|
||||
if (!PlatformInfos.isMobile) return null;
|
||||
final tmpDir = await getTemporaryDirectory();
|
||||
final tmpFile = File(tmpDir.path + name);
|
||||
if (await tmpFile.exists() == false) {
|
||||
await tmpFile.writeAsBytes(bytes);
|
||||
}
|
||||
try {
|
||||
final bytes = await VideoCompress.getByteThumbnail(tmpFile.path);
|
||||
if (bytes == null) return null;
|
||||
return MatrixImageFile(
|
||||
bytes: bytes,
|
||||
name: name,
|
||||
).resizeImage();
|
||||
} catch (e, s) {
|
||||
SentryController.captureException(e, s);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<MatrixImageFile> resizeImage({
|
||||
bool calcBlurhash = true,
|
||||
int max = ResizeImage.max,
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ extension RoomSendFileExtension on Room {
|
|||
Event? inReplyTo,
|
||||
String? editEventId,
|
||||
bool? waitUntilSent,
|
||||
Map<String, dynamic>? extraContent,
|
||||
}) async {
|
||||
MatrixImageFile? thumbnail;
|
||||
if (file is MatrixImageFile) {
|
||||
|
|
@ -19,6 +20,8 @@ extension RoomSendFileExtension on Room {
|
|||
if (thumbnail.size > file.size ~/ 2) {
|
||||
thumbnail = null;
|
||||
}
|
||||
} else if (file is MatrixVideoFile) {
|
||||
thumbnail = await file.getVideoThumbnail();
|
||||
}
|
||||
|
||||
return sendFileEvent(
|
||||
|
|
@ -28,6 +31,7 @@ extension RoomSendFileExtension on Room {
|
|||
editEventId: editEventId,
|
||||
waitUntilSent: waitUntilSent ?? false,
|
||||
thumbnail: thumbnail,
|
||||
extraContent: extraContent,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue