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
|