flutterdartgoogle-cloud-firestorevideo-thumbnails

Invalid image on Creating thumbnails from video with flutter


Trying to generate an Thumbnail image from video , the file is created but , errors as Invalid image on load .Using this package video_thumbnail

Creating 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;
  }

Saving to firestore

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

The Saved Image , 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


Solution

  • Posting this as a Community Wiki as it's based on @pskink comments.

    To resolve, add the writeAsBytes(bytes) instead of writeAsBytes(buffer.asUint8List()). There is no need for any buffer.