2021-12-27 13:01:51 +01:00
|
|
|
import 'dart:io';
|
2021-11-20 16:54:18 +01:00
|
|
|
import 'dart:typed_data';
|
2020-10-06 18:22:50 +02:00
|
|
|
|
2021-11-20 16:54:18 +01:00
|
|
|
import 'package:blurhash_dart/blurhash_dart.dart';
|
|
|
|
import 'package:image/image.dart';
|
|
|
|
import 'package:matrix/matrix.dart';
|
2021-12-27 13:01:51 +01:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
import 'package:video_compress/video_compress.dart';
|
|
|
|
|
|
|
|
import 'package:fluffychat/utils/platform_infos.dart';
|
2020-10-06 18:22:50 +02:00
|
|
|
|
2021-11-20 16:54:18 +01:00
|
|
|
extension ResizeImage on MatrixFile {
|
2021-12-12 12:03:58 +01:00
|
|
|
static const int max = 1200;
|
|
|
|
static const int quality = 40;
|
2021-11-20 16:54:18 +01:00
|
|
|
|
2021-12-27 14:42:06 +01:00
|
|
|
Future<MatrixVideoFile> resizeVideo() async {
|
2021-12-27 13:01:51 +01:00
|
|
|
final tmpDir = await getTemporaryDirectory();
|
2022-08-14 16:59:21 +02:00
|
|
|
final tmpFile = File('${tmpDir.path}/$name');
|
2021-12-27 14:42:06 +01:00
|
|
|
MediaInfo? mediaInfo;
|
|
|
|
await tmpFile.writeAsBytes(bytes);
|
|
|
|
try {
|
2022-02-15 09:25:13 +01:00
|
|
|
// will throw an error e.g. on Android SDK < 18
|
2021-12-27 18:33:29 +01:00
|
|
|
mediaInfo = await VideoCompress.compressVideo(tmpFile.path);
|
2021-12-27 14:42:06 +01:00
|
|
|
} catch (e, s) {
|
2022-08-14 16:18:18 +02:00
|
|
|
Logs().w('Error while compressing video', e, s);
|
2021-12-27 13:01:51 +01:00
|
|
|
}
|
2021-12-27 14:42:06 +01:00
|
|
|
return MatrixVideoFile(
|
2021-12-27 18:33:29 +01:00
|
|
|
bytes: (await mediaInfo?.file?.readAsBytes()) ?? bytes,
|
2021-12-27 14:42:06 +01:00
|
|
|
name: name,
|
|
|
|
mimeType: mimeType,
|
|
|
|
width: mediaInfo?.width,
|
|
|
|
height: mediaInfo?.height,
|
|
|
|
duration: mediaInfo?.duration?.round(),
|
|
|
|
);
|
2021-12-27 13:01:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<MatrixImageFile?> getVideoThumbnail() async {
|
|
|
|
if (!PlatformInfos.isMobile) return null;
|
|
|
|
final tmpDir = await getTemporaryDirectory();
|
2022-08-14 16:59:21 +02:00
|
|
|
final tmpFile = File('${tmpDir.path}/$name');
|
2021-12-27 13:01:51 +01:00
|
|
|
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,
|
2022-02-03 07:35:44 +01:00
|
|
|
);
|
2021-12-27 13:01:51 +01:00
|
|
|
} catch (e, s) {
|
2022-08-14 16:18:18 +02:00
|
|
|
Logs().w('Error while compressing video', e, s);
|
2021-12-27 13:01:51 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2020-10-06 18:22:50 +02:00
|
|
|
}
|
|
|
|
|
2021-11-20 16:54:18 +01:00
|
|
|
Future<BlurHash> createBlurHash(Uint8List file) async {
|
|
|
|
final image = decodeImage(file)!;
|
|
|
|
return BlurHash.encode(image, numCompX: 4, numCompY: 3);
|
2020-10-06 18:22:50 +02:00
|
|
|
}
|