1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-26 06:49:22 +00:00
comunicmobile/lib/utils/video_utils.dart

41 lines
1.1 KiB
Dart

import 'dart:io';
import 'package:comunic/models/api_request.dart';
import 'package:comunic/utils/log_utils.dart';
import 'package:flutter/material.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import 'package:random_string/random_string.dart';
import 'package:video_thumbnail/video_thumbnail.dart';
/// Video utilities
///
/// @author Pierre Hubert
/// Generate a thumbnail for a video. In case of failure, return null
Future<BytesFile> generateVideoThumbnail({
@required BytesFile videoFile,
int maxWidth,
}) async {
File file;
try {
final tempDir = await getTemporaryDirectory();
if (tempDir == null) return null;
file = File(path.join(tempDir.path, randomString(15, from: 65, to: 90)));
await file.writeAsBytes(videoFile.bytes);
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();
}
}