'Compress & Upload large videos to Google cloud storage using Flutter/Dart

There are a couple of notable packages on pub.dev that offer video compression. I've tried them, and other sketchy packages, and none work well once a video gets around 300MB. They crash or have other issues on various platforms and hardware. Namely, video compress and light compressor. The GH commits and support are concerning as well on the packages I've seen for video compression in pub.dev. PR's not being pulled in and issues not being resolved in a timely manner and some quite serious for recent android APK updates. So not something I want in my dependency stack.

I am uploading to Google Cloud Storage using FlutterFire. While my code does upload using FireBaseStorage upload task it does not have any ability to compress on the client side or handle background uploading when the app is closed.

So, currently on the server side, I have a GCF that triggers on file uploaded. Then I use nodejs ffmpeg, which is baked into GCF's to compress server side and convert to H264. And finally delete the original large upload video and save the compressed video to storage.

This solution works, but depending on a user's connection and whether they are on wifi, can take an awful long time and when it fails or the user closes the app, my current solution is useless.

I wish there was a solid native library on Android and iOS, that I could tap into, to confidently perform compression and conversion from any format to H264 and also allow uploading, whether my app is closed or in the background, to GC storage. Any thoughts? I wish this was standard in FlutterFire's cloud storage handling!

I have yet to test flutter_ffmpeg, but only because some have said it runs so slowly on client. So again, Flutter/Dart can access natively written code, but I don't know where to start on Android/iOS to do this the right way. And I understand this is what some of the packages are doing, but they do not work with large videos, so I'm hoping someone can point me in the right direction on Android and iOS.

My code for handling upload tasks to GC storage.

 
 ​  ​Future<​void​>​ ​uploadTask​({ 
 ​    ​required​ ​WidgetRef​ ref, 
 ​    ​required​ ​File​ file, 
 ​    ​required​ ​String​ objectPath, 
 ​    ​SettableMetadata​?​ metaData, 
 ​    ​bool​ deleteAfterUpload ​=​ ​false​, 
 ​    ​bool​ displayProgressNotification ​=​ ​false​, 
 ​  }) ​async​ { 
 ​    ​String​ filePath ​=​ file.path; 
 ​    filename ​=​ ​basename​(file.path); 
  
 ​    ​/// Remove any instances of '//' from the path. 
 ​    ​final​ ​String​ path ​=​ ​'$​storagePath​/$​objectPath​'​.​replaceAll​(​'//'​, ​'/'​); 
 ​    ​UploadTask​ task ​=​ storage.​ref​(path).​putFile​( 
 ​          ​File​(filePath), 
 ​          metaData, 
 ​        ); 
  
 ​    ​/// Store UploadTask in StateNotifierProvider to monitor progress. 
 ​    ref.​read​(uploadingStateProvider.notifier).​myUploadTask​(task); 
 ​  }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source