Sha256: 83cf1ec0086cf82b3ff20f2fe6568587f9e83356af507b0ca0cd62b692283826

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

module ScreenRecorder
  # @since 1.0.0-beta4
  module Titles
    # Regex to filter out "Window Title: N/A" from Chrome extensions and "Window Title: ".
    # This is done to remove unusable titles and to match the Ffmpeg expected input format
    # for capturing specific windows.
    # For example, "Window Title: Google - Mozilla Firefox" becomes "Google - Mozilla Firefox".
    FILTERED_TITLES = %r{^Window Title:( N/A|\s+)?}.freeze

    #
    # Returns a list of available window titles for the given application (process) name.
    #
    def self.fetch(application)
      ScreenRecorder.logger.debug "Retrieving available windows for: #{application}"
      WindowGrabber.new.available_windows_for application
    end

    # @since 1.0.0-beta4
    class WindowGrabber
      #
      # Returns a list of available window titles for the given application (process) name.
      #
      def available_windows_for(application)
        raise NotImplementedError, 'Only Microsoft Windows (gdigrab) supports window capture.' unless OS.windows?

        titles = `tasklist /v /fi "imagename eq #{application}.exe" /fo list | findstr  Window`
                   .split("\n")
                   .map { |i| i.gsub(FILTERED_TITLES, '') }
                   .reject(&:empty?)
        raise Errors::ApplicationNotFound, "No open windows found for: #{application}.exe" if titles.empty?

        warn_on_mismatch(titles, application)
        titles
      end

      private

      #
      # Prints a warning if the retrieved list of window titles does no include
      # the given application process name, which applications commonly do.
      #
      def warn_on_mismatch(titles, application)
        return if titles.map(&:downcase).join(',').include? application.to_s

        ScreenRecorder.logger.warn "Process name and window title(s) do not match: #{titles}"
        ScreenRecorder.logger.warn 'Please manually provide the displayed window title.'
      end
    end # class WindowGrabber
  end # module Windows
end # module FFMPEG

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
screen-recorder-1.1.0 lib/screen-recorder/titles.rb