Module: VideoTranscoding::Crop

Extended by:
Crop
Included in:
Crop
Defined in:
lib/video_transcoding/crop.rb

Instance Method Summary collapse

Instance Method Details

#constrain(raw_crop, width, height) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/video_transcoding/crop.rb', line 75

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
# File 'lib/video_transcoding/crop.rb', line 11

def detect(path, duration, width, height)
  Console.info "Detecting crop with mplayer..."
  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([
        MPlayer.command_name,
        '-quiet',
        '-benchmark',
        '-vo', 'null',
        '-ao', 'null',
        '-vf', 'cropdetect=24:2',
        path,
        '-ss', (interval * step).to_s,
        '-frames', '10'
      ], :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

          if line =~ /^\[CROP\] .* 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

#handbrake_string(crop) ⇒ Object



99
100
101
# File 'lib/video_transcoding/crop.rb', line 99

def handbrake_string(crop)
  "#{crop[:top]}:#{crop[:bottom]}:#{crop[:left]}:#{crop[:right]}"
end

#mplayer_string(crop, width, height) ⇒ Object



103
104
105
106
107
108
# File 'lib/video_transcoding/crop.rb', line 103

def mplayer_string(crop, width, height)
  "#{width - (crop[:left] + crop[:right])}:" +
  "#{height - (crop[:top] + crop[:bottom])}:" +
  "#{crop[:left]}:" +
  "#{crop[:top]}"
end