从带有抖动的视频创建缩略图时图像无效

Invalid image on Creating thumbnails from video with flutter

尝试从视频生成缩略图图像,文件已创建,但加载时出现图像无效错误。使用此包 video_thumbnail

正在创建缩略图,

Future<File> genThumbnail(url) async {
    //WidgetsFlutterBinding.ensureInitialized();
    Uint8List bytes;
    final Completer<ThumbnailResult> completer = Completer();
      bytes = await VideoThumbnail.thumbnailData(
          video: url,
          imageFormat: ImageFormat.JPEG,
          maxHeight: 250,
          maxWidth: 300,
          timeMs: 0,
          quality: 0);

    int _imageDataSize = bytes.length;
    print("image size: $_imageDataSize");

    //final _image = Image.memory(bytes);
    //var _file =File.fromRawPath(bytes);

    Directory tempDir = await getTemporaryDirectory();
    var uint8list = bytes;
    var buffer = uint8list.buffer;
    ByteData byteData = ByteData.view(buffer);
    File file = await File('${tempDir.path}/img/THUMBNAIL${DateTime.now().toIso8601String()}.JPEG').writeAsBytes(
        buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));

    return file;
  }

保存到 Firestore

await genThumbnail(fileurl).then((_thumbFIle) async{
          String Thumbfileurl = await uploadFile(_thumbFIle, 'thumbnailOf${filenamewithoutExtension}.JPEG', 'videothumbnail');
          await sendFileToFirestoreChat(fileType, fileurl, filenamewithoutExtension,Thumbfileurl);
          return fileurl;
        });

保存的图像, https://firebasestorage.googleapis.com/v0/b/proj-inhouse.appspot.com/o/videos%2Fvideothumbnails%2FthumbnailOfVID-20210301-WA0006.JPEG?alt=media&token=fa4f23c1-601f-486b-97d1-c63e221166af

将此作为社区 Wiki 发布,因为它基于@pskink 评论。

要解决,请添加 writeAsBytes(bytes) 而不是 writeAsBytes(buffer.asUint8List())。不需要任何 buffer.