executePlan static method

Future<void> executePlan(
  1. RenderPlan plan, {
  2. RenderProgressCallback? onProgress,
  3. RenderJob? job,
})

Runs plan with FFmpeg, reporting progress and honouring job.

Implementation

static Future<void> executePlan(
  RenderPlan plan, {
  RenderProgressCallback? onProgress,
  RenderJob? job,
}) async {
  if (job?.isCancelled ?? false) throw const RenderCancelledException();

  final done = Completer<void>();
  final totalMs = math.max(plan.duration.inMilliseconds, 1);
  final session = await FFmpegKit.executeWithArgumentsAsync(
    plan.arguments,
    (session) async {
      final code = await session.getReturnCode();
      if (ReturnCode.isSuccess(code)) {
        done.complete();
      } else if (ReturnCode.isCancel(code) || (job?.isCancelled ?? false)) {
        done.completeError(const RenderCancelledException());
      } else {
        final logs = await session.getLogsAsString();
        debugPrint('Render failed: $logs');
        done.completeError(RenderException(_summarize(logs), logs));
      }
    },
    null,
    (statistics) {
      final time = statistics.getTime();
      if (time > 0) {
        onProgress?.call((time / totalMs).clamp(0.0, 0.99));
      }
    },
  );
  job?._sessionId = session.getSessionId();
  if (job?.isCancelled ?? false) await job!.cancel();
  await done.future;
}