Flutter?获取视频第一帧

Cod*_*24h 10 dart flutter flutter-layout

Flutter项目中如何获取本地视频文件的第一帧?这是我的代码:

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.
  }
 });
},
Run Code Online (Sandbox Code Playgroud)

jit*_*555 5

首先,从文件对象获取文件路径并将其传递给下面的方法,该方法使用video_thumbnail lib 生成缩略图

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;
  }
Run Code Online (Sandbox Code Playgroud)

其次,要使用它,您需要使用调用在另一个方法中接收 File 对象await,然后setState()更新文件。

 File file = await genThumbnailFile(filePath);
 setState(() {});
Run Code Online (Sandbox Code Playgroud)

最后,使用类似的文件

Image.file(file, fit: BoxFit.cover,)
Run Code Online (Sandbox Code Playgroud)


小智 -7

String thumb = await Thumbnails.getThumbnail(
    thumbnailFolder:'[FOLDER PATH TO STORE THUMBNAILS]', // creates the specified path if it doesnt exist
    videoFile: '[VIDEO PATH HERE]',
    imageType: ThumbFormat.PNG,
    quality: 30);

/*
* thumbnailFolder property can be omitted if you dont wish to keep the generated thumbails past each usage
*/
Run Code Online (Sandbox Code Playgroud)

  • 此答案仅适用于 Android。Flutter 的主要卖点是跨平台,如果您发布解决方案,请明确说明它只能工作一半。 (3认同)