How to get the first frame of the local video file in the Flutter project? Here is my code:
ImagePicker.pickVideo(source: ImageSource.camera).then((File file) {
if (file != null && mounted) {
//I got the video file here, but I want to get the first frame of the video.
}
});
},
First, get the file path from File Object and pass it to the below method which uses video_thumbnail lib for generating thumbnail
Future<File> genThumbnailFile(String path) async {
final fileName = await VideoThumbnail.thumbnailFile(
video: path,
thumbnailPath: (await getTemporaryDirectory()).path,
imageFormat: ImageFormat.PNG,
maxHeight: 100, // specify the height of the thumbnail, let the width auto-scaled to keep the source aspect ratio
quality: 75,
);
File file = File(fileName);
return file;
}
Second, to use it you need to receive the File object in another method using await
call, then do setState()
to update the file.
File file = await genThumbnailFile(filePath);
setState(() {});
Finally, Use files like
Image.file(file, fit: BoxFit.cover,)
Use export_video_frame
, it works for IOS.
var duration = Duration(seconds: 1);
var image = await ExportVideoFrame.exportImageBySeconds(widget.file, duration, 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With