Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter:Get the first frame of the video

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.
  }
 });
},
like image 435
Coding24h Avatar asked Nov 26 '22 20:11

Coding24h


2 Answers

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,)
like image 91
Jitesh Mohite Avatar answered Dec 05 '22 18:12

Jitesh Mohite


Use export_video_frame, it works for IOS.

var duration = Duration(seconds: 1);
var image = await ExportVideoFrame.exportImageBySeconds(widget.file, duration, 0);
like image 40
user3710385 Avatar answered Dec 05 '22 18:12

user3710385