如何将从用户那里挑选的视频上传到 firebase?

How to upload a video picked from user into firebase?

我正在尝试将视频上传到 firebase。但是有一个问题,我上传的视频越多,视频需要的 space 就越多。我一次只上传一个视频。该视频是从用户那里挑选的。当用户选择第一个视频并且视频持续时间约为 2 秒时,上传速度非常快。然后在接下来的 2 或 3 秒持续时间内,它比第一个需要更多时间但仍然可以。然后就像第 4 个视频一样,它需要很多时间。视频再次录制 2 或 3 秒,但存储时间为 13 分钟。我不明白为什么。这只有在我使用新的 android 模拟器时才能解决。当我从 firebase 中删除所有数据并喜欢将第一个视频录制到其中时,它并不关心。 希望任何人都可以举例说明为什么会发生此错误。 这是我的代码

final allvideos = FirebaseStorage.instance.ref().child('allvideos');

  final allimages = FirebaseStorage.instance.ref().child('allimages');
uploadVideo() async {
    setState(() {
      isuploading = true;
    });
    try {
      var firebaseuseruid = FirebaseAuth.instance.currentUser.uid;
      DocumentSnapshot userdoc = await FirebaseFirestore.instance
          .collection('meinprofilsettings')
          .doc(firebaseuseruid)
          .get();
      var alldocs = await FirebaseFirestore.instance.collection('videos').get();
      int length = alldocs.docs.length;
      String videourl = await uploadvideotostorage("Video $length");
      String previewimage = await uploadimagetostorage("Video $length");
      FirebaseFirestore.instance.collection('videos').doc("Video $length").set({
        'username': userdoc.data()['username'],
        'uid': firebaseuseruid,
        'profilepic': userdoc.data()['url'],
        'id':"Video $length",
        'likes': [],
        'commentcount': 0,
        'sharecount': 0,
        'hashtag1': hashtagcontroller.text,
        'hashtag2': hashtagcontroller2.text,
        'hashtag3': hashtagcontroller3.text,
        'videourl': videourl,
        'previewimage': previewimage,
        'ratings': [],

      });
      Navigator.pop(context);
    } catch (e) {
      print(e.toString());
    }
  }
}


这是我上传的方式 图片为预览图

getpreviewimage() async {
    final previewimage = await flutterVideoCompress.getThumbnailWithFile(
      widget.videopath_asstring,
    );
    return previewimage;
  }

  compressvideo() async {
    if (widget.imageSource == ImageSource.gallery) {
      return widget.videofile;
    } else {
      final compressvideo = await flutterVideoCompress.compressVideo(
          widget.videopath_asstring,
          quality: VideoQuality.MediumQuality);
      return File(compressvideo.path);
    }
  }

  uploadvideotostorage(String id) async {
    final video = await allvideos.child(id).putFile(await compressvideo());
    String url = await video.ref.getDownloadURL();
    return url;
  }

  uploadimagetostorage(String id) async {
    final video = await allimages.child(id).putFile(await getpreviewimage());
    String url = await video.ref.getDownloadURL();
    id=url;
    return url;
  }

使用类似这样的方式生成您的随机 ID:

import 'dart:math';
import 'package:intl/intl.dart';

String generateId() {
  int randomNumber = Random().nextInt(9999999);
  String now = DateTime.now().millisecondsSinceEpoch.toString().substring(7);
  String formatted = DateFormat('MMdh').format(DateTime.now());
  return randomNumber.toString() + formatted + now;
}

将您的上传功能更改为如下所示:

  Future<String> uploadvideotostorage(String id) async {
    final video = await allvideos.child(id).putFile(await compressvideo());
    String url = await video.ref.getDownloadURL();
    return url;
  }

创建视频时,为其分配一个随机 ID,如下所示:

String randomlyGeneratedId = generateId();

最后,要恢复您的下载 URL:

String finalVideoURL = await uploadvideotostorage(randomlyGeneratedId);