fix: Workaround for reversed width and height of compressed videos sent from Android
This commit is contained in:
parent
b8d64a113e
commit
3d0a3ee226
3 changed files with 31 additions and 15 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
import 'package:flutter_linkify/flutter_linkify.dart';
|
import 'package:flutter_linkify/flutter_linkify.dart';
|
||||||
|
|
@ -36,11 +38,14 @@ class EventVideoPlayer extends StatelessWidget {
|
||||||
.tryGet<String>('xyz.amorgan.blurhash') ??
|
.tryGet<String>('xyz.amorgan.blurhash') ??
|
||||||
fallbackBlurHash;
|
fallbackBlurHash;
|
||||||
final fileDescription = event.fileDescription;
|
final fileDescription = event.fileDescription;
|
||||||
|
const maxDimension = 300.0;
|
||||||
final infoMap = event.content.tryGetMap<String, Object?>('info');
|
final infoMap = event.content.tryGetMap<String, Object?>('info');
|
||||||
final videoWidth = infoMap?.tryGet<int>('w') ?? 400;
|
final videoWidth = infoMap?.tryGet<int>('w') ?? maxDimension;
|
||||||
final videoHeight = infoMap?.tryGet<int>('h') ?? 300;
|
final videoHeight = infoMap?.tryGet<int>('h') ?? maxDimension;
|
||||||
const height = 300.0;
|
|
||||||
final width = videoWidth * (height / videoHeight);
|
final modifier = max(videoWidth, videoHeight) / maxDimension;
|
||||||
|
final width = videoWidth / modifier;
|
||||||
|
final height = videoHeight / modifier;
|
||||||
|
|
||||||
final durationInt = infoMap?.tryGet<int>('duration');
|
final durationInt = infoMap?.tryGet<int>('duration');
|
||||||
final duration =
|
final duration =
|
||||||
|
|
|
||||||
|
|
@ -59,16 +59,22 @@ class SendFileDialogState extends State<SendFileDialog> {
|
||||||
final length = await xfile.length();
|
final length = await xfile.length();
|
||||||
final mimeType = xfile.mimeType ?? lookupMimeType(xfile.path);
|
final mimeType = xfile.mimeType ?? lookupMimeType(xfile.path);
|
||||||
|
|
||||||
|
// Generate video thumbnail
|
||||||
|
if (PlatformInfos.isMobile &&
|
||||||
|
mimeType != null &&
|
||||||
|
mimeType.startsWith('video')) {
|
||||||
|
scaffoldMessenger.showLoadingSnackBar(l10n.generatingVideoThumbnail);
|
||||||
|
thumbnail = await xfile.getVideoThumbnail();
|
||||||
|
}
|
||||||
|
|
||||||
// If file is a video, shrink it!
|
// If file is a video, shrink it!
|
||||||
if (PlatformInfos.isMobile &&
|
if (PlatformInfos.isMobile &&
|
||||||
mimeType != null &&
|
mimeType != null &&
|
||||||
mimeType.startsWith('video') &&
|
mimeType.startsWith('video')) {
|
||||||
length > minSizeToCompress &&
|
|
||||||
compress) {
|
|
||||||
scaffoldMessenger.showLoadingSnackBar(l10n.compressVideo);
|
scaffoldMessenger.showLoadingSnackBar(l10n.compressVideo);
|
||||||
file = await xfile.resizeVideo();
|
file = await xfile.getVideoInfo(
|
||||||
scaffoldMessenger.showLoadingSnackBar(l10n.generatingVideoThumbnail);
|
compress: length > minSizeToCompress && compress,
|
||||||
thumbnail = await xfile.getVideoThumbnail();
|
);
|
||||||
} else {
|
} else {
|
||||||
if (length > maxUploadSize) {
|
if (length > maxUploadSize) {
|
||||||
throw FileTooBigMatrixException(length, maxUploadSize);
|
throw FileTooBigMatrixException(length, maxUploadSize);
|
||||||
|
|
|
||||||
|
|
@ -8,22 +8,27 @@ extension ResizeImage on XFile {
|
||||||
static const int max = 1200;
|
static const int max = 1200;
|
||||||
static const int quality = 40;
|
static const int quality = 40;
|
||||||
|
|
||||||
Future<MatrixVideoFile> resizeVideo() async {
|
Future<MatrixVideoFile> getVideoInfo({bool compress = true}) async {
|
||||||
MediaInfo? mediaInfo;
|
MediaInfo? mediaInfo;
|
||||||
try {
|
try {
|
||||||
if (PlatformInfos.isMobile) {
|
if (PlatformInfos.isMobile) {
|
||||||
// will throw an error e.g. on Android SDK < 18
|
// will throw an error e.g. on Android SDK < 18
|
||||||
mediaInfo = await VideoCompress.compressVideo(path);
|
mediaInfo = compress
|
||||||
|
? await VideoCompress.compressVideo(path, deleteOrigin: true)
|
||||||
|
: await VideoCompress.getMediaInfo(path);
|
||||||
}
|
}
|
||||||
} catch (e, s) {
|
} catch (e, s) {
|
||||||
Logs().w('Error while compressing video', e, s);
|
Logs().w('Error while fetching video media info', e, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
return MatrixVideoFile(
|
return MatrixVideoFile(
|
||||||
bytes: (await mediaInfo?.file?.readAsBytes()) ?? await readAsBytes(),
|
bytes: (await mediaInfo?.file?.readAsBytes()) ?? await readAsBytes(),
|
||||||
name: name,
|
name: name,
|
||||||
mimeType: mimeType,
|
mimeType: mimeType,
|
||||||
width: mediaInfo?.width,
|
// on Android width and height is reversed:
|
||||||
height: mediaInfo?.height,
|
// https://github.com/jonataslaw/VideoCompress/issues/172
|
||||||
|
width: PlatformInfos.isAndroid ? mediaInfo?.height : mediaInfo?.width,
|
||||||
|
height: PlatformInfos.isAndroid ? mediaInfo?.width : mediaInfo?.height,
|
||||||
duration: mediaInfo?.duration?.round(),
|
duration: mediaInfo?.duration?.round(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue