feat: Enable compression and thumbnails for videos

This commit is contained in:
Christian Pauly 2021-12-27 13:01:51 +01:00
parent f79adea8da
commit d49515011d
6 changed files with 72 additions and 7 deletions

View File

@ -15,7 +15,8 @@ import 'package:vrouter/vrouter.dart';
import 'package:fluffychat/pages/add_story/add_story_view.dart'; import 'package:fluffychat/pages/add_story/add_story_view.dart';
import 'package:fluffychat/pages/add_story/invite_story_page.dart'; import 'package:fluffychat/pages/add_story/invite_story_page.dart';
import 'package:fluffychat/utils/resize_image.dart'; import 'package:fluffychat/utils/matrix_sdk_extensions.dart/matrix_file_extension.dart';
import 'package:fluffychat/utils/room_send_file_extension.dart';
import 'package:fluffychat/utils/string_color.dart'; import 'package:fluffychat/utils/string_color.dart';
import 'package:fluffychat/widgets/matrix.dart'; import 'package:fluffychat/widgets/matrix.dart';
import '../../utils/matrix_sdk_extensions.dart/client_stories_extension.dart'; import '../../utils/matrix_sdk_extensions.dart/client_stories_extension.dart';
@ -123,17 +124,16 @@ class AddStoryController extends State<AddStoryPage> {
if (storiesRoom == null) throw ('Stories room is null'); if (storiesRoom == null) throw ('Stories room is null');
final video = this.video; final video = this.video;
if (video != null) { if (video != null) {
await storiesRoom.sendFileEvent( await storiesRoom.sendFileEventWithThumbnail(
video, video.detectFileType,
extraContent: {'body': controller.text}, extraContent: {'body': controller.text},
); );
return; return;
} }
var image = this.image; final image = this.image;
if (image != null) { if (image != null) {
image = await image.resizeImage(); await storiesRoom.sendFileEventWithThumbnail(
await storiesRoom.sendFileEvent( image.detectFileType,
image,
extraContent: {'body': controller.text}, extraContent: {'body': controller.text},
); );
return; return;

View File

@ -38,6 +38,9 @@ class _SendFileDialogState extends State<SendFileDialog> {
file.bytes.length > minSizeToCompress) { file.bytes.length > minSizeToCompress) {
file = await file.resizeImage(); file = await file.resizeImage();
} }
if (file is MatrixVideoFile && file.bytes.length > minSizeToCompress) {
file = await file.resizeVideo();
}
await widget.room.sendFileEventWithThumbnail(file); await widget.room.sendFileEventWithThumbnail(file);
} }

View File

@ -1,5 +1,6 @@
//@dart=2.12 //@dart=2.12
import 'dart:io';
import 'dart:math' as math; import 'dart:math' as math;
import 'dart:typed_data'; import 'dart:typed_data';
@ -8,11 +9,60 @@ import 'package:flutter/foundation.dart';
import 'package:blurhash_dart/blurhash_dart.dart'; import 'package:blurhash_dart/blurhash_dart.dart';
import 'package:image/image.dart'; import 'package:image/image.dart';
import 'package:matrix/matrix.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 { extension ResizeImage on MatrixFile {
static const int max = 1200; static const int max = 1200;
static const int quality = 40; 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({ Future<MatrixImageFile> resizeImage({
bool calcBlurhash = true, bool calcBlurhash = true,
int max = ResizeImage.max, int max = ResizeImage.max,

View File

@ -11,6 +11,7 @@ extension RoomSendFileExtension on Room {
Event? inReplyTo, Event? inReplyTo,
String? editEventId, String? editEventId,
bool? waitUntilSent, bool? waitUntilSent,
Map<String, dynamic>? extraContent,
}) async { }) async {
MatrixImageFile? thumbnail; MatrixImageFile? thumbnail;
if (file is MatrixImageFile) { if (file is MatrixImageFile) {
@ -19,6 +20,8 @@ extension RoomSendFileExtension on Room {
if (thumbnail.size > file.size ~/ 2) { if (thumbnail.size > file.size ~/ 2) {
thumbnail = null; thumbnail = null;
} }
} else if (file is MatrixVideoFile) {
thumbnail = await file.getVideoThumbnail();
} }
return sendFileEvent( return sendFileEvent(
@ -28,6 +31,7 @@ extension RoomSendFileExtension on Room {
editEventId: editEventId, editEventId: editEventId,
waitUntilSent: waitUntilSent ?? false, waitUntilSent: waitUntilSent ?? false,
thumbnail: thumbnail, thumbnail: thumbnail,
extraContent: extraContent,
); );
} }
} }

View File

@ -1594,6 +1594,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.1" version: "2.1.1"
video_compress:
dependency: "direct main"
description:
name: video_compress
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.0"
video_player: video_player:
dependency: "direct main" dependency: "direct main"
description: description:

View File

@ -72,6 +72,7 @@ dependencies:
unifiedpush: ^3.0.1 unifiedpush: ^3.0.1
universal_html: ^2.0.8 universal_html: ^2.0.8
url_launcher: ^6.0.12 url_launcher: ^6.0.12
video_compress: ^3.1.0
video_player: ^2.2.10 video_player: ^2.2.10
vrouter: ^1.2.0+15 vrouter: ^1.2.0+15
wakelock: ^0.5.6 wakelock: ^0.5.6