首页 > 解决方案 > 如何将用户挑选的视频上传到firebase?

问题描述

我正在尝试将视频上传到 Firebase。但是有一个问题,我上传的视频越多,视频需要的空间就越多。我只同时上传一个视频。视频是从用户中挑选出来的。当用户选择第一个视频并且视频持续时间约为 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;
  }

标签: fluttergoogle-cloud-firestorefile-uploadfirebase-storage

解决方案


使用这样的东西来生成你的随机 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);

推荐阅读