feat: Enable compression and thumbnails for videos

This commit is contained in:
Christian Pauly 2021-12-27 13:01:51 +01:00
parent f5124aa503
commit ab9d7fcc7d
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/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/widgets/matrix.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');
final video = this.video;
if (video != null) {
await storiesRoom.sendFileEvent(
video,
await storiesRoom.sendFileEventWithThumbnail(
video.detectFileType,
extraContent: {'body': controller.text},
);
return;
}
var image = this.image;
final image = this.image;
if (image != null) {
image = await image.resizeImage();
await storiesRoom.sendFileEvent(
image,
await storiesRoom.sendFileEventWithThumbnail(
image.detectFileType,
extraContent: {'body': controller.text},
);
return;

View file

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

View file

@ -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,

View file

@ -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,
);
}
}

View file

@ -1594,6 +1594,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
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:
dependency: "direct main"
description:

View file

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