Ad

How To Upload A Video Picked From User Into Firebase?

Im trying to upload a video into firebase .But theres a problem ,the more videos I upload, the more space a video needs. Im only uploading one video the same time . The video is picked from user . And when user picked the first video and when video duration is about 2 seconds long, it gets very fast uploaded. Then on the next also 2 or 3 seconds duration it takes a bit more time then the first but still ok . And then like on the 4 video ,its need very much time. And the video ist again 2 or 3 seconds recording but storage like 13 minutes . And im not getting why. This only is resolved when im using a new android emulator. it dont care when im deleting all data from firebase and like recording the first video into it . Hope anyone can example why this error happen. Heres my code

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());
    }
  }
}


Heres how I upload it the picture is for preview picture

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

Answer

Use something like this to generate your random 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;
}

Change your upload function to look like this:

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

When you create a video, assign it a random id like this:

String randomlyGeneratedId = generateId();

Finally, to get your download URL back:

String finalVideoURL = await uploadvideotostorage(randomlyGeneratedId);
Ad
source: stackoverflow.com
Ad