Module: VideoTranscoding::Crop
Instance Method Summary collapse
- #constrain(raw_crop, width, height) ⇒ Object
- #detect(path, duration, width, height) ⇒ Object
- #drawbox_string(crop, width, height) ⇒ Object
- #handbrake_string(crop) ⇒ Object
- #player_string(crop, width, height) ⇒ Object
Instance Method Details
#constrain(raw_crop, width, height) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/video_transcoding/crop.rb', line 81 def constrain(raw_crop, width, height) crop = raw_crop.dup delta_x = crop[:left] + crop[:right] delta_y = crop[:top] + crop[:bottom] min_delta = (width / 64) min_delta += 1 if min_delta.odd? if delta_x > delta_y crop[:top], crop[:bottom] = 0, 0 if delta_x < min_delta or delta_x >= width crop[:left], crop[:right] = 0, 0 end else crop[:left], crop[:right] = 0, 0 if delta_y < min_delta or delta_y >= height crop[:top], crop[:bottom] = 0, 0 end end crop end |
#detect(path, duration, width, height) ⇒ Object
11 12 13 14 15 16 17 18 19 20 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/video_transcoding/crop.rb', line 11 def detect(path, duration, width, height) Console.info "Detecting crop with ffmpeg..." fail "media duration too short: #{duration} second(s)" if duration < 2 steps = 10 interval = duration / (steps + 1) target_interval = 5 * 60 if interval == 0 steps = 1 interval = 1 elsif interval > target_interval steps = (duration / target_interval) - 1 interval = duration / (steps + 1) end crop_width, crop_height, crop_x, crop_y = 0, 0, width, height last_seconds = Time.now.tv_sec (1..steps).each do |step| begin IO.popen([ FFmpeg.command_name, '-hide_banner', '-nostdin', '-noaccurate_seek', '-ss', (interval * step).to_s, '-i', path, '-frames:v', '10', '-filter:v', 'cropdetect=24:2', '-an', '-sn', '-ignore_unknown', '-f', 'null', '-' ], :err=>[:child, :out]) do |io| io.each do |line| seconds = Time.now.tv_sec if seconds - last_seconds >= 3 Console.warn '...' last_seconds = seconds end line.encode! 'UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '' if line =~ / crop=([0-9]+):([0-9]+):([0-9]+):([0-9]+)$/ d_width, d_height, d_x, d_y = $1.to_i, $2.to_i, $3.to_i, $4.to_i crop_width = d_width if crop_width < d_width crop_height = d_height if crop_height < d_height crop_x = d_x if crop_x > d_x crop_y = d_y if crop_y > d_y Console.debug line end end end rescue SystemCallError => e raise "crop detection failed: #{e}" end fail "crop detection failed" unless $CHILD_STATUS.exitstatus == 0 end { :top => crop_y, :bottom => height - (crop_y + crop_height), :left => crop_x, :right => width - (crop_x + crop_width) } end |
#drawbox_string(crop, width, height) ⇒ Object
116 117 118 119 120 121 |
# File 'lib/video_transcoding/crop.rb', line 116 def drawbox_string(crop, width, height) "#{crop[:left]}:" + "#{crop[:top]}:" + "#{width - (crop[:left] + crop[:right])}:" + "#{height - (crop[:top] + crop[:bottom])}" end |
#handbrake_string(crop) ⇒ Object
105 106 107 |
# File 'lib/video_transcoding/crop.rb', line 105 def handbrake_string(crop) "#{crop[:top]}:#{crop[:bottom]}:#{crop[:left]}:#{crop[:right]}" end |
#player_string(crop, width, height) ⇒ Object
109 110 111 112 113 114 |
# File 'lib/video_transcoding/crop.rb', line 109 def player_string(crop, width, height) "#{width - (crop[:left] + crop[:right])}:" + "#{height - (crop[:top] + crop[:bottom])}:" + "#{crop[:left]}:" + "#{crop[:top]}" end |