screenRecordToFile method

Future<int> screenRecordToFile({
  1. required String outputFile,
  2. ScreenRecordOptions recordingOptions = const ScreenRecordOptions(),
  3. Duration? duration,
  4. bool debug = false,
})

Records the device screen to a file on the host, without any GUI.

This pipes the raw H264 stream produced by screenrecord ... - directly to outputFile on the host. Recording stops after ScreenRecordOptions.timelimit (or the device default), or when duration elapses (whichever comes first).

Returns the exit code of the adb process.

Implementation

Future<int> screenRecordToFile({
  required String outputFile,
  ScreenRecordOptions recordingOptions = const ScreenRecordOptions(),
  Duration? duration,
  bool debug = false,
}) async {
  final arguments = [
    ..._connection.arguments,
    'shell',
    'screenrecord',
    ...recordingOptions.toArgs(),
    '-',
  ];
  final process = await _bridge.executor.start(arguments);

  final sink = io.File(outputFile).openWrite();
  final stdoutDone = process.stdout.pipe(sink);

  if (duration != null) {
    Future.delayed(duration, () => process.kill());
  }

  final exitCode = await process.exitCode;
  await stdoutDone;

  if (debug) {
    final length = await io.File(outputFile).length();
    debugPrint('screenRecordToFile: exitCode=$exitCode, bytes=$length');
  }

  return exitCode;
}