compressVideoHelper function

Future<File?> compressVideoHelper(
  1. File file, {
  2. bool compressVideo = false,
  3. int maxSize = 10 * 1024 * 1024,
})

Implementation

Future<File?> compressVideoHelper(File file, {bool compressVideo = false, int maxSize = 10 * 1024 * 1024}) async {
  try {
    if (!compressVideo) {
      return file;
    }
    debugPrint("⚡ Compressing video...");
    final MediaInfo? compressedVideo = await VideoCompress.compressVideo(
      file.path,
      quality: VideoQuality.MediumQuality, // Options: Low, Medium, High
      deleteOrigin: false, // Keep original video
    );

    final int size = compressedVideo?.filesize ?? 0;

    debugPrint("📏 File Size: ${compressedVideo?.filesize} bytes < $maxSize");
    if (compressedVideo != null && size < maxSize) {
      debugPrint("✅ Compression completed: ${compressedVideo.path}");
      return File(compressedVideo.path!);
    } else {
      debugPrint("❌ Compression failed");
      return null;
    }
  } catch (e) {
    debugPrint("❌ Compression failed: $e");
    return null;
  }
}