Class: RubySpriter::VideoProcessor
- Inherits:
-
Object
- Object
- RubySpriter::VideoProcessor
- 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
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#create_spritesheet(video_file, output_file) ⇒ Hash
Create spritesheet from video file.
-
#get_duration(video_file) ⇒ Float
Get video duration in seconds.
-
#initialize(options = {}) ⇒ VideoProcessor
constructor
A new instance of VideoProcessor.
-
#process_with_background_removal(video_path, output_path, options) ⇒ Hash
Process video frames with background removal.
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 = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/ruby_spriter/video_processor.rb', line 8 def @options end |
Instance Method Details
#create_spritesheet(video_file, output_file) ⇒ Hash
Create spritesheet from video file
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 = [:columns] || 4 frame_count = [: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.( temp_file, output_file, columns: columns, rows: rows, frames: frame_count, debug: [: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
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
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, ) temp_dir = Dir.mktmpdir('ruby_spriter_') begin # Extract frames from video frame_files = extract_frames(video_path, temp_dir, ) if [:by_frame] # Frame-by-frame processing process_frames_individually(frame_files, temp_dir, ) # Assemble spritesheet from processed frames processed_frames = frame_files.map { |f| no_background_filename(f) } assemble_spritesheet_from_frames(processed_frames, output_path, .merge(temp_dir: temp_dir)) else # Standard processing: assemble first, then process spritesheet assemble_spritesheet_from_frames(frame_files, output_path, .merge(temp_dir: temp_dir)) # Apply background removal to entire spritesheet process_image_with_gimp(output_path, output_path, ) end # Cell-based cleanup (if --cleanup-cells flag present) if [:cleanup_cells] require_relative 'cell_cleanup_processor' cell_processor = CellCleanupProcessor.new() stats = cell_processor.cleanup_cells(output_path, ) end # Calculate rows for metadata columns = [:columns] || 4 frames = [: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.( temp_file, output_path, columns: columns, rows: rows, frames: frames, debug: [:debug] ) # Clean up temp file File.delete(temp_file) if File.exist?(temp_file) # Return processing results { output_file: output_path, columns: [:columns], frames: [:frames], processing_mode: [:by_frame] ? 'by-frame' : 'standard' } ensure # Cleanup temp directory unless --keep-temp or --debug FileUtils.rm_rf(temp_dir) unless [:keep_temp] || [:debug] end end |