Class: FFMPEG::KeyframeExtractor
- Inherits:
-
Object
- Object
- FFMPEG::KeyframeExtractor
- Defined in:
- lib/ffmpeg/keyframe_extractor.rb
Overview
Extracts keyframes/thumbnails from video files
Constant Summary collapse
- FORMATS =
Supported output formats
%w[jpg jpeg png bmp webp].freeze
Instance Attribute Summary collapse
-
#media ⇒ Media
readonly
Source media.
Instance Method Summary collapse
-
#create_sprite(columns: 10, rows: 10, width: 160, output_path:) ⇒ Hash
Create a thumbnail sprite sheet (useful for video scrubbing).
-
#extract_at_intervals(interval: 5.0, output_dir:, format: "jpg", quality: 2, resolution: nil) ⇒ Array<String>
Extract frames at regular intervals.
-
#extract_at_timestamps(timestamps, output_dir:, format: "jpg", quality: 2, resolution: nil) ⇒ Array<String>
Extract frames at specific timestamps.
-
#extract_count(count, output_dir:, format: "jpg", quality: 2, resolution: nil) ⇒ Array<String>
Extract a specific number of frames evenly distributed.
-
#extract_iframes(output_dir:, format: "jpg", quality: 2, max_frames: nil) ⇒ Array<String>
Extract actual I-frames (keyframes) from the video.
-
#initialize(media) ⇒ KeyframeExtractor
constructor
Create a new KeyframeExtractor.
Constructor Details
#initialize(media) ⇒ KeyframeExtractor
Create a new KeyframeExtractor
29 30 31 32 |
# File 'lib/ffmpeg/keyframe_extractor.rb', line 29 def initialize(media) @media = media validate_media! end |
Instance Attribute Details
#media ⇒ Media (readonly)
Returns source media.
25 26 27 |
# File 'lib/ffmpeg/keyframe_extractor.rb', line 25 def media @media end |
Instance Method Details
#create_sprite(columns: 10, rows: 10, width: 160, output_path:) ⇒ Hash
Create a thumbnail sprite sheet (useful for video scrubbing)
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 160 |
# File 'lib/ffmpeg/keyframe_extractor.rb', line 130 def create_sprite(columns: 10, rows: 10, width: 160, output_path:) total_frames = columns * rows interval = (media.duration || 0) / total_frames # Calculate thumbnail height maintaining aspect ratio aspect_ratio = (media.width.to_f / media.height) rescue 16.0/9 height = (width / aspect_ratio).round tile = "#{columns}x#{rows}" scale = "scale=#{width}:#{height}" cmd = [ FFMPEG.ffmpeg_binary, "-i", media.path, "-vf", "fps=1/#{interval},#{scale},tile=#{tile}", "-frames:v", "1", "-y", output_path ] Command.run!(*cmd) { path: output_path, columns: columns, rows: rows, thumbnail_width: width, thumbnail_height: height, interval: interval } end |
#extract_at_intervals(interval: 5.0, output_dir:, format: "jpg", quality: 2, resolution: nil) ⇒ Array<String>
Extract frames at regular intervals
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ffmpeg/keyframe_extractor.rb', line 41 def extract_at_intervals(interval: 5.0, output_dir:, format: "jpg", quality: 2, resolution: nil) ensure_output_dir!(output_dir) validate_format!(format) # Calculate timestamps = [] current = 0.0 while current < media.duration << current current += interval end extract_frames(, output_dir, format, quality, resolution) end |
#extract_at_timestamps(timestamps, output_dir:, format: "jpg", quality: 2, resolution: nil) ⇒ Array<String>
Extract frames at specific timestamps
88 89 90 91 92 93 |
# File 'lib/ffmpeg/keyframe_extractor.rb', line 88 def (, output_dir:, format: "jpg", quality: 2, resolution: nil) ensure_output_dir!(output_dir) validate_format!(format) extract_frames(.sort, output_dir, format, quality, resolution) end |
#extract_count(count, output_dir:, format: "jpg", quality: 2, resolution: nil) ⇒ Array<String>
Extract a specific number of frames evenly distributed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/ffmpeg/keyframe_extractor.rb', line 63 def extract_count(count, output_dir:, format: "jpg", quality: 2, resolution: nil) ensure_output_dir!(output_dir) validate_format!(format) return [] if count <= 0 # Calculate evenly distributed timestamps duration = media.duration || 0 = if count == 1 [0.0] else interval = duration / (count - 1) (0...count).map { |i| i * interval } end extract_frames(, output_dir, format, quality, resolution) end |
#extract_iframes(output_dir:, format: "jpg", quality: 2, max_frames: nil) ⇒ Array<String>
Extract actual I-frames (keyframes) from the video
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/ffmpeg/keyframe_extractor.rb', line 101 def extract_iframes(output_dir:, format: "jpg", quality: 2, max_frames: nil) ensure_output_dir!(output_dir) validate_format!(format) output_pattern = File.join(output_dir, "iframe_%04d.#{format}") cmd = [ FFMPEG.ffmpeg_binary, "-i", media.path, "-vf", "select='eq(pict_type,I)'", "-vsync", "vfr", "-q:v", quality.to_s ] cmd += ["-frames:v", max_frames.to_s] if max_frames cmd += ["-y", output_pattern] result = Command.run!(*cmd) # Return list of created files Dir.glob(File.join(output_dir, "iframe_*.#{format}")).sort end |