compressVideo static method

Future<String?> compressVideo({
  1. required String inputPath,
  2. int bitrate = 2000000,
  3. int? width,
  4. int? height,
  5. bool preserveResolution = true,
  6. bool avoidLargerOutput = true,
  7. VideoSetting videoSetting = VideoSetting.h264,
  8. AudioSetting audioSetting = AudioSetting.aac,
  9. int audioBitrate = 128000,
  10. int audioSampleRate = 44100,
  11. int audioChannels = 2,
  12. bool printingInfo = false,
  13. dynamic onProgress(
    1. double progress
    )?,
})

Compress video

inputPath : Input video path outputPath : Output video path bitrate : Video bitrate (e.g. 2000000 = 2Mbps) width : Output video width height : Output video height videoCodec : Video codec ("h264" or "h265"/"hevc"), default: "h264" audioCodec : Audio codec ("aac", "alac", "mp3"), default: "aac" audioBitrate : Audio bitrate (e.g. 128000 = 128kbps), default: 128000 audioSampleRate : Audio sample rate (e.g. 44100), default: 44100 audioChannels : Audio channels (1=mono, 2=stereo), default: 2 printingInfo : Print video info, default: false onProgress : Progress callback (0.0 - 1.0), NEW parameter

Implementation

static Future<String?> compressVideo({
  required String inputPath,
  int bitrate = 2000000,
  int? width,
  int? height,
  bool preserveResolution = true,
  bool avoidLargerOutput = true,
  VideoSetting videoSetting = VideoSetting.h264,
  AudioSetting audioSetting = AudioSetting.aac,
  int audioBitrate = 128000,
  int audioSampleRate = 44100,
  int audioChannels = 2,
  bool printingInfo = false,
  Function(double progress)? onProgress,
}) async {
  try {
    // Setup progress handler
    _setupProgressHandler();
    _currentProgressCallback = onProgress;

    if (printingInfo) {
      debugPrint(
        '-------------------------------- Before compress video info --------------------------------',
      );
      await printVideoInfo(inputPath);
    }

    /// Output path creation
    final dir = await getTemporaryDirectory();
    final outputPath = '${dir.path}/$outputFileName';
    await _channel.invokeMethod('compressVideo', {
      'input': inputPath,
      'output': outputPath,
      'bitrate': bitrate,
      'width': width,
      'height': height,
      'preserveResolution': preserveResolution,
      'avoidLargerOutput': avoidLargerOutput,
      'videoCodec': videoSetting.value,
      'audioCodec': audioSetting.value,
      'audioBitrate': audioBitrate,
      'audioSampleRate': audioSampleRate,
      'audioChannels': audioChannels,
    });
    if (printingInfo) {
      debugPrint(
        '-------------------------------- After compress video info --------------------------------',
      );
      await printVideoInfo(outputPath);
    }
    return outputPath;
  } on PlatformException catch (e) {
    debugPrint('Failed to compress video: $e');
    return null;
  } finally {
    // Clean up callback reference to prevent memory leaks
    _currentProgressCallback = null;
  }
}