getVideoDuration method

Future<int?> getVideoDuration(
  1. String videoPath
)

Get video duration in seconds

Implementation

Future<int?> getVideoDuration(String videoPath) async {
  try {
    ProcessResult result = await Process.run('ffmpeg', [
      '-i',
      videoPath,
      '-hide_banner',
      '-print_format',
      'json',
      '-show_entries',
      'format=duration',
      '-v',
      'quiet',
    ]);

    if (result.exitCode == 0) {
      RegExp regex = RegExp(r'"duration"\s*:\s*"(\d+\.\d+)"');
      Match? match = regex.firstMatch(result.stdout);
      if (match != null) {
        return double.parse(match.group(1)!).toInt();
      }
    }
  } catch (e) {
    debugPrint("Error getting duration: $e");
  }
  return null;
}