compressVideo static method

Future<File> compressVideo(
  1. File file
)

Compresses a video using the native platform implementation via MethodChannel.

Implementation

static Future<File> compressVideo(File file) async {
  try {
    final originalSize = await file.length();

    // SKIP COMPRESSION IF ALREADY UNDER 100MB
    if (originalSize < 100 * 1024 * 1024) {
      return file;
    }

    final String? compressedPath = await _hwVideoCompress.compressVideo(
      file.path,
    );

    if (compressedPath != null) {
      final compressedFile = File(compressedPath);
      if (await compressedFile.exists()) {
        final compressedSize = await compressedFile.length();
        if (compressedSize >= originalSize) {
          try {
            await compressedFile.delete();
          } catch (_) {}
          return file;
        }
        return compressedFile;
      }
    }
  } catch (e) {
    log('Error compressing video: $e');
  }
  return file;
}