Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I check the file size of a video in flutter before uploading

I want to upload a video file to my cloud storage but I want to check the size of the video file before I upload. how to achieve this

like image 549
Norbert Avatar asked Oct 28 '18 21:10

Norbert


People also ask

How do I find the size of a video file?

The Formula For Calculating Video File Sizes First, divide bitrate by 8 to get the byte rate. Then, multiply the byte rate with the duration of the video in seconds, and you get the file size in megabytes (MB).


2 Answers

If you have the path of the file, you can use dart:io

var file = File('the_path_to_the_video.mp4'); 

You an either use:

print(file.lengthSync());  

or

print (await file.length()); 

Note: The size returned is in bytes.

like image 138
Günter Zöchbauer Avatar answered Sep 23 '22 04:09

Günter Zöchbauer


// defined the function

getFileSize(String filepath, int decimals) async {     var file = File(filepath);     int bytes = await file.length();     if (bytes <= 0) return "0 B";     const suffixes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];     var i = (log(bytes) / log(1024)).floor();     return ((bytes / pow(1024, i)).toStringAsFixed(decimals)) + ' ' + suffixes[i];   } 

// how to call the function

print(getFileSize('file-path-will-be-here', 1)); 

// output will be as string like:

97.5 KB 

This worked for me. I hope, this will also help you. Thanks a lot for asking question.

like image 44
Kamlesh Avatar answered Sep 26 '22 04:09

Kamlesh