video-preview 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +20 -0
- data/README.md +13 -0
- data/lib/video-preview.rb +57 -0
- data/test/test.rb +3 -0
- data/test/trailer.mp4 +0 -0
- metadata +52 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c85dd313a750c1cd6110dedece7abb5596e8ec45
|
4
|
+
data.tar.gz: 561fe81f517243d03b272d1abf28bd5ef07f47f7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7b82cc63feadc643b8996e9abfbe53e1732ea18206b658725f742fab916dd61548cc5405f3ee1ded331432ddce0f58b4a467ccffe06495390209a06ea06a5e62
|
7
|
+
data.tar.gz: 50db100530b04ae7ae90ba61bd1258536f036fac2bde0458d08d1d25e5b009de3fa3973aa3c0f29ca76687900fb0944868b7b5988b402235f04f67947bfe9533
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 Dmitry Filimonov
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Video Preview
|
2
|
+
|
3
|
+
This little library allows you to generate video previews in a wise manner. Specifically, it picks N frames from a video and then ranks them by the contrast ratio of an image.
|
4
|
+
|
5
|
+
To install it, simply run `gem install video-preview`. The library has two main dependencies: `ffmpeg` and `imagemagick`. Make sure you have them as well.
|
6
|
+
|
7
|
+
Here's an example on how to use it:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
require 'video-preview'
|
11
|
+
VideoPreview.get_preview 'trailer.mp4'
|
12
|
+
# => #<Magick::Image:0x007fd5738869b8>
|
13
|
+
```
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'tmpdir'
|
3
|
+
require 'RMagick'
|
4
|
+
|
5
|
+
module VideoPreview
|
6
|
+
|
7
|
+
VERSION = "0.0.1"
|
8
|
+
|
9
|
+
def self.rank image
|
10
|
+
total_red = 0
|
11
|
+
total_blue = 0
|
12
|
+
total_green = 0
|
13
|
+
|
14
|
+
average_red = 0
|
15
|
+
average_blue = 0
|
16
|
+
average_green = 0
|
17
|
+
|
18
|
+
distance_red = 0
|
19
|
+
distance_blue = 0
|
20
|
+
distance_green = 0
|
21
|
+
|
22
|
+
pixel_count = image.rows * image.columns
|
23
|
+
hist = image.color_histogram
|
24
|
+
|
25
|
+
hist.each do |pixel, count|
|
26
|
+
total_red += pixel.red*count
|
27
|
+
total_blue += pixel.blue*count
|
28
|
+
total_green += pixel.green*count
|
29
|
+
end
|
30
|
+
|
31
|
+
average_red = total_red / pixel_count
|
32
|
+
average_blue = total_blue / pixel_count
|
33
|
+
average_green = total_green / pixel_count
|
34
|
+
|
35
|
+
hist.each do |pixel, count|
|
36
|
+
distance_red += (average_red - pixel.red).abs
|
37
|
+
distance_blue += (average_blue - pixel.blue).abs
|
38
|
+
distance_green += (average_green - pixel.green).abs
|
39
|
+
end
|
40
|
+
|
41
|
+
return distance_green + distance_blue + distance_red
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.get_preview filepath, opts = {}
|
45
|
+
opts[:N] ||= 20
|
46
|
+
json = JSON.parse `ffprobe -print_format json -show_streams #{filepath} 2> /dev/null`
|
47
|
+
if json['streams']
|
48
|
+
stream = json['streams'].select {|stream| stream['codec_type'] == 'video'}.first
|
49
|
+
if stream && stream['duration']
|
50
|
+
Dir.mktmpdir do |dir|
|
51
|
+
`ffmpeg -i #{filepath} -f image2 -vf fps=fps=1/#{stream['duration'].to_f / opts[:N]} #{dir}/%010d.png 2> /dev/null`
|
52
|
+
return Dir.glob(File.join(dir, "*.png")).map {|x| Magick::ImageList.new(x)[0] }.max_by {|x| rank x }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/test/test.rb
ADDED
data/test/trailer.mp4
ADDED
Binary file
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: video-preview
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dmitry Filimonov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-25 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- me@dfilimonov.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/video-preview.rb
|
21
|
+
- LICENSE
|
22
|
+
- README.md
|
23
|
+
- test/test.rb
|
24
|
+
- test/trailer.mp4
|
25
|
+
homepage: https://github.com/petethepig/video-preview
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements:
|
44
|
+
- ImageMagick
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 2.1.11
|
47
|
+
signing_key:
|
48
|
+
specification_version: 4
|
49
|
+
summary: Library that allows you to generate video previews in a wise manner.
|
50
|
+
test_files:
|
51
|
+
- test/test.rb
|
52
|
+
- test/trailer.mp4
|