Class ScreenRecorder

java.lang.Object
uno.anahata.asi.yam.tools.screenrecording.ScreenRecorder

public class ScreenRecorder extends Object
Universal cross-platform screen recording engine utilizing native FFmpeg process execution.

Supports automated video capture and instantaneous thumbnail frame snapshots across Linux (X11), macOS (AVFoundation), and Windows (gdigrab).

Supports customizable recording directories, explicit target file paths, and multi-monitor resolution routing.

Author:
anahata
  • Field Details

    • ffmpegProcess

      private Process ffmpegProcess
      The active FFmpeg recording operating system process, or null if idle.
    • currentVideoPath

      private Path currentVideoPath
      The absolute path to the .mp4 file currently being recorded.
    • startEpochMillis

      private long startEpochMillis
      Start time in epoch milliseconds.
    • selectedDeviceIndex

      private int selectedDeviceIndex
      Selected screen graphics device index.
    • customRecordingsDirectory

      private Path customRecordingsDirectory
      Customizable output directory for recorded videos.
  • Constructor Details

    • ScreenRecorder

      public ScreenRecorder()
  • Method Details

    • getEffectiveRecordingsDirectory

      public Path getEffectiveRecordingsDirectory()
      Resolves the directory where screen recordings are stored.
      Returns:
      The path to the recordings directory.
    • isRecording

      public boolean isRecording()
      Checks if a screen recording process is currently active.
      Returns:
      true if FFmpeg is actively recording.
    • startRecording

      public Path startRecording(Path targetFilePath, int deviceIndex) throws Exception
      Initiates a new screen recording session to an explicit target file path on the specified screen device.
      Parameters:
      targetFilePath - The exact destination .mp4 file path.
      deviceIndex - The 0-based screen graphics device index to record.
      Returns:
      The path to the destination .mp4 file.
      Throws:
      Exception - If launching the FFmpeg process fails.
    • startRecording

      public Path startRecording(String prefix, String identifier, int deviceIndex) throws Exception
      Initiates a new screen recording session using a generated filename with prefix and identifier.
      Parameters:
      prefix - The filename prefix (e.g., "desktop", "java-jna-1").
      identifier - The descriptor identifier (e.g., "gemini-3.6-flash").
      deviceIndex - The 0-based screen graphics device index.
      Returns:
      The path to the destination .mp4 file.
      Throws:
      Exception - If launching FFmpeg fails.
    • stopRecording

      public RecordedSession stopRecording(boolean captureThumbnail, Path thumbnailDestination) throws Exception
      Stops the active recording, captures an instantaneous screen frame thumbnail, and finalizes the MP4 file by cleanly sending the 'q' quit signal to FFmpeg.
      Parameters:
      captureThumbnail - Whether to capture a PNG screenshot at the stop moment.
      thumbnailDestination - The optional destination path for the captured thumbnail PNG.
      Returns:
      A RecordedSession containing paths and recording duration.
      Throws:
      Exception - If process termination or thumbnail capture fails.
    • cancelRecording

      public void cancelRecording()
      Cancels the active recording session, terminates FFmpeg, and deletes partial video files.
    • captureScreenFrame

      public Path captureScreenFrame(Path customDest, int deviceIndex)
      Captures a high-resolution snapshot of the recorded screen at the current instant.
      Parameters:
      customDest - The optional custom destination file path.
      deviceIndex - The screen device index.
      Returns:
      The path to the saved PNG thumbnail file.
    • buildFfmpegCommand

      private List<String> buildFfmpegCommand(String outputPath, int deviceIndex)
      Builds the OS-specific FFmpeg CLI command for the given screen device.
      Parameters:
      outputPath - The target output .mp4 file path.
      deviceIndex - The target screen graphics device index.
      Returns:
      List of command arguments.