2021-03-12 18:36:42 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:comunic/models/api_request.dart';
|
|
|
|
import 'package:comunic/utils/log_utils.dart';
|
|
|
|
import 'package:video_thumbnail/video_thumbnail.dart';
|
|
|
|
|
2021-03-13 16:50:59 +00:00
|
|
|
import 'files_utils.dart';
|
|
|
|
|
2021-03-12 18:36:42 +00:00
|
|
|
/// Video utilities
|
|
|
|
///
|
|
|
|
/// @author Pierre Hubert
|
|
|
|
|
|
|
|
/// Generate a thumbnail for a video. In case of failure, return null
|
2022-03-10 18:39:57 +00:00
|
|
|
Future<BytesFile?> generateVideoThumbnail({
|
|
|
|
required BytesFile videoFile,
|
|
|
|
required int maxWidth,
|
2021-03-12 18:36:42 +00:00
|
|
|
}) async {
|
2022-03-10 18:39:57 +00:00
|
|
|
File? file;
|
2021-03-12 18:36:42 +00:00
|
|
|
|
|
|
|
try {
|
2021-03-13 16:50:59 +00:00
|
|
|
file = await generateTemporaryFile();
|
2021-03-12 18:36:42 +00:00
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
await file.writeAsBytes(videoFile.bytes!);
|
2021-03-12 18:36:42 +00:00
|
|
|
|
|
|
|
return BytesFile(
|
|
|
|
"thumb.png",
|
|
|
|
await VideoThumbnail.thumbnailData(
|
|
|
|
video: file.absolute.path, maxWidth: maxWidth),
|
|
|
|
);
|
|
|
|
} catch (e, s) {
|
|
|
|
logError(e, s);
|
|
|
|
return null;
|
|
|
|
} finally {
|
|
|
|
if (file != null && await file.exists()) await file.delete();
|
|
|
|
}
|
|
|
|
}
|