Class: RubySpriter::VideoProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_spriter/video_processor.rb

Overview

Processes video files with FFmpeg

Constant Summary collapse

NO_BACKGROUND_SUFFIX =

Filename suffix for background-removed frames

'_nobg'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ VideoProcessor

Returns a new instance of VideoProcessor.



13
14
15
# File 'lib/ruby_spriter/video_processor.rb', line 13

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/ruby_spriter/video_processor.rb', line 8

def options
  @options
end

Instance Method Details

#create_spritesheet(video_file, output_file) ⇒ Hash

Create spritesheet from video file

Parameters:

  • video_file (String)

    Path to video file

  • output_file (String)

    Path to output spritesheet

Returns:

  • (Hash)

    Processing results



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ruby_spriter/video_processor.rb', line 21

def create_spritesheet(video_file, output_file)
  Utils::FileHelper.validate_readable!(video_file)

  Utils::OutputFormatter.header("Video Analysis")
  duration = get_duration(video_file)
  Utils::OutputFormatter.indent("Duration: #{duration.round(2)} seconds\n")

  columns = options[:columns] || 4
  frame_count = options[:frame_count] || 16
  rows = (frame_count.to_f / columns).ceil

  Utils::OutputFormatter.header("Creating Spritesheet")

  temp_file = output_file.sub('.png', '_temp.png')

  create_with_ffmpeg(video_file, temp_file, duration, columns, rows, frame_count)

  # Embed metadata
  MetadataManager.embed(
    temp_file,
    output_file,
    columns: columns,
    rows: rows,
    frames: frame_count,
    debug: options[:debug]
  )

  # Clean up temp file
  File.delete(temp_file) if File.exist?(temp_file)

  file_size = File.size(output_file)

  # Display results with Godot instructions
  display_spritesheet_results(output_file, file_size, columns, rows, frame_count)

  {
    output_file: output_file,
    columns: columns,
    rows: rows,
    frames: frame_count,
    size: file_size
  }
end

#get_duration(video_file) ⇒ Float

Get video duration in seconds

Parameters:

  • video_file (String)

    Path to video file

Returns:

  • (Float)

    Duration in seconds



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ruby_spriter/video_processor.rb', line 68

def get_duration(video_file)
  cmd = [
    'ffprobe',
    '-v', 'error',
    '-show_entries', 'format=duration',
    '-of', 'default=noprint_wrappers=1:nokey=1',
    Utils::PathHelper.quote_path(video_file)
  ].join(' ')

  stdout, stderr, status = Open3.capture3(cmd)

  unless status.success?
    raise ProcessingError, "Could not determine video duration: #{stderr}"
  end

  stdout.strip.to_f
end

#process_with_background_removal(video_path, output_path, options) ⇒ Hash

Process video frames with background removal

Parameters:

  • video_path (String)

    Path to input video file

  • output_path (String)

    Path to output spritesheet

  • options (Hash)

    Processing options

Options Hash (options):

  • :by_frame (Boolean)

    Process each frame individually

  • :gimp_path (String)

    Path to GIMP executable

  • :columns (Integer)

    Number of columns in spritesheet

  • :frames (Integer)

    Number of frames to extract

  • :keep_temp (Boolean)

    Keep temporary files

  • :debug (Boolean)

    Enable debug output

Returns:

  • (Hash)

    Processing results with :output_file, :columns, :frames, :processing_mode



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/ruby_spriter/video_processor.rb', line 97

def process_with_background_removal(video_path, output_path, options)
  temp_dir = Dir.mktmpdir('ruby_spriter_')

  begin
    # Extract frames from video
    frame_files = extract_frames(video_path, temp_dir, options)

    if options[:by_frame]
      # Frame-by-frame processing
      process_frames_individually(frame_files, temp_dir, options)

      # Assemble spritesheet from processed frames
      processed_frames = frame_files.map { |f| no_background_filename(f) }
      assemble_spritesheet_from_frames(processed_frames, output_path, options.merge(temp_dir: temp_dir))
    else
      # Standard processing: assemble first, then process spritesheet
      assemble_spritesheet_from_frames(frame_files, output_path, options.merge(temp_dir: temp_dir))

      # Apply background removal to entire spritesheet
      process_image_with_gimp(output_path, output_path, options)
    end

    # Cell-based cleanup (if --cleanup-cells flag present)
    if options[:cleanup_cells]
      require_relative 'cell_cleanup_processor'
      cell_processor = CellCleanupProcessor.new(options)
      stats = cell_processor.cleanup_cells(output_path, options)
    end

    # Calculate rows for metadata
    columns = options[:columns] || 4
    frames = options[:frames] || 16
    rows = (frames.to_f / columns).ceil

    # Embed metadata using temp file pattern (like create_spritesheet does)
    temp_file = output_path.sub('.png', '_temp.png')
    FileUtils.mv(output_path, temp_file)

    RubySpriter::MetadataManager.embed(
      temp_file,
      output_path,
      columns: columns,
      rows: rows,
      frames: frames,
      debug: options[:debug]
    )

    # Clean up temp file
    File.delete(temp_file) if File.exist?(temp_file)

    # Return processing results
    {
      output_file: output_path,
      columns: options[:columns],
      frames: options[:frames],
      processing_mode: options[:by_frame] ? 'by-frame' : 'standard'
    }

  ensure
    # Cleanup temp directory unless --keep-temp or --debug
    FileUtils.rm_rf(temp_dir) unless options[:keep_temp] || options[:debug]
  end
end