0

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;
  }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Is this happening during the same session? If for example you uploaded two videos now, then closed the app, when you open it again and try to upload 12 more videos, happens fast or slow? What about terminating the app? Also on a physical device? If you haven't tried a physical device, I would suggest trying there first and compare. – Huthaifa Muayyad Apr 11 '21 at 12:51
  • ok it tried on a real device and it seems to work. But theres still the error when im deleting one video like theresVideo0, Video1, Video 2,Video3 and im deleting video 1. And then uploading a new one the last one so video 3 gets overriden, And then uploading a new Video 3 gets again overriden. You you know how tom solved it ? –  Apr 11 '21 at 13:19
  • If it's getting overriden you are probably using the same name for the old and new videos. – Huthaifa Muayyad Apr 11 '21 at 20:21
  • bro the problem when im deleting a video like the number 2 of 4 the newest gets overridden again and again and again that means getting the length of all data does not working correctly because of this im trying to using another method to setting the id no matter. which . Itjust have to every time be another id –  Apr 11 '21 at 20:44
  • So please help me im really stuck since 3 days –  Apr 11 '21 at 20:44
  • Use a randomly generated ID or something is actually unique, not the length of a list, and try – Huthaifa Muayyad Apr 11 '21 at 20:48
  • You you know how I can use the url of it ? –  Apr 11 '21 at 20:49
  • or you you have an idea what I can use thats unique ? –  Apr 11 '21 at 20:54

1 Answers1

0

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);
Huthaifa Muayyad
  • 11,321
  • 3
  • 17
  • 49
  • Yes bro thank you that solving it but how can I also enter the alphabet to make it more random? –  Apr 11 '21 at 21:42
  • You're most welcome, here's a great answer for generating [a random string](https://stackoverflow.com/a/61929967/13558035). Kindly consider marking this as an answer to your question if it helped you out. – Huthaifa Muayyad Apr 11 '21 at 21:50