mirror of
https://gitlab.com/famedly/fluffychat.git
synced 2024-11-09 05:29:30 +01:00
62 lines
1.8 KiB
Dart
62 lines
1.8 KiB
Dart
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
|
|
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';
|
|
|
|
extension ResizeImage on MatrixFile {
|
|
static const int max = 1200;
|
|
static const int quality = 40;
|
|
|
|
Future<MatrixVideoFile> resizeVideo() async {
|
|
final tmpDir = await getTemporaryDirectory();
|
|
final tmpFile = File('${tmpDir.path}/$name');
|
|
MediaInfo? mediaInfo;
|
|
await tmpFile.writeAsBytes(bytes);
|
|
try {
|
|
// will throw an error e.g. on Android SDK < 18
|
|
mediaInfo = await VideoCompress.compressVideo(tmpFile.path);
|
|
} catch (e, s) {
|
|
Logs().w('Error while compressing video', e, s);
|
|
}
|
|
return MatrixVideoFile(
|
|
bytes: (await mediaInfo?.file?.readAsBytes()) ?? bytes,
|
|
name: name,
|
|
mimeType: mimeType,
|
|
width: mediaInfo?.width,
|
|
height: mediaInfo?.height,
|
|
duration: mediaInfo?.duration?.round(),
|
|
);
|
|
}
|
|
|
|
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,
|
|
);
|
|
} catch (e, s) {
|
|
Logs().w('Error while compressing video', e, s);
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<BlurHash> createBlurHash(Uint8List file) async {
|
|
final image = decodeImage(file)!;
|
|
return BlurHash.encode(image, numCompX: 4, numCompY: 3);
|
|
}
|