Class: FFMPEG::SceneDetector
- Inherits:
-
Object
- Object
- FFMPEG::SceneDetector
- Defined in:
- lib/ffmpeg/scene_detector.rb
Overview
Detects scene changes in video using FFmpeg’s scene detection filter
Instance Attribute Summary collapse
-
#media ⇒ Media
readonly
Source media.
Instance Method Summary collapse
-
#detect(threshold: 0.3, min_scene_length: nil, max_scenes: nil) ⇒ Array<Hash>
Detect scene changes.
-
#detect_with_keyframes(threshold: 0.3, output_dir:, format: "jpg") ⇒ Array<Hash>
Detect scenes and extract a keyframe for each.
-
#initialize(media) ⇒ SceneDetector
constructor
Create a new SceneDetector.
Constructor Details
#initialize(media) ⇒ SceneDetector
Create a new SceneDetector
28 29 30 31 |
# File 'lib/ffmpeg/scene_detector.rb', line 28 def initialize(media) @media = media validate_media! end |
Instance Attribute Details
#media ⇒ Media (readonly)
Returns source media.
24 25 26 |
# File 'lib/ffmpeg/scene_detector.rb', line 24 def media @media end |
Instance Method Details
#detect(threshold: 0.3, min_scene_length: nil, max_scenes: nil) ⇒ Array<Hash>
Detect scene changes
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/ffmpeg/scene_detector.rb', line 40 def detect(threshold: 0.3, min_scene_length: nil, max_scenes: nil) scenes = run_detection(threshold) # Always include first frame scenes.unshift({ timestamp: 0.0, score: 1.0 }) unless scenes.any? { |s| s[:timestamp].zero? } # Filter by minimum scene length if min_scene_length scenes = filter_by_min_length(scenes, min_scene_length) end # Limit number of scenes if max_scenes && scenes.length > max_scenes scenes = select_best_scenes(scenes, max_scenes) end scenes.sort_by { |s| s[:timestamp] } end |
#detect_with_keyframes(threshold: 0.3, output_dir:, format: "jpg") ⇒ Array<Hash>
Detect scenes and extract a keyframe for each
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/ffmpeg/scene_detector.rb', line 64 def detect_with_keyframes(threshold: 0.3, output_dir:, format: "jpg") scenes = detect(threshold: threshold) FileUtils.mkdir_p(output_dir) scenes.each_with_index do |scene, index| output_path = File.join(output_dir, "scene_%04d.#{format}" % index) extract_frame(scene[:timestamp], output_path) scene[:keyframe_path] = output_path end scenes end |