Class: Videos::Video

Inherits:
Object
  • Object
show all
Defined in:
lib/videos/video.rb

Constant Summary collapse

PREVIEW_WIDTH =

PREVIEW_WIDTH = 211 PREVIEW_HEIGHT = 332

274
PREVIEW_HEIGHT =
155
PREVIEW_SMALL_WIDTH =
98
PREVIEW_SMALL_HEIGHT =
154

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(videos_package, path) ⇒ Video

Returns a new instance of Video.



7
8
9
10
11
# File 'lib/videos/video.rb', line 7

def initialize(videos_package, path)
  @videos_package = videos_package
  @path = path
  @metadata = {}
end

Instance Attribute Details

#lengthObject

Returns the value of attribute length.



5
6
7
# File 'lib/videos/video.rb', line 5

def length
  @length
end

#metadataObject (readonly)

Returns the value of attribute metadata.



4
5
6
# File 'lib/videos/video.rb', line 4

def 
  @metadata
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/videos/video.rb', line 3

def path
  @path
end

#videos_packageObject (readonly)

Returns the value of attribute videos_package.



2
3
4
# File 'lib/videos/video.rb', line 2

def videos_package
  @videos_package
end

Class Method Details

.from_hash(videos_package, data) ⇒ Object



82
83
84
85
86
# File 'lib/videos/video.rb', line 82

def self.from_hash(videos_package, data)
  video = Videos::Video.new(videos_package, videos_package.url_to_pathname(Addressable::URI.parse(data["url"])))
  video.length = data["length"]
  video
end

Instance Method Details

#generate_thumbnailObject



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
# File 'lib/videos/video.rb', line 44

def generate_thumbnail
  return if thumbnail_path.exist?

  temp_directory = "/tmp/videos_#{Process.pid}"
  FileUtils.mkdir_p(temp_directory)

  system("mpv --really-quiet --start=50% --frames=1 --ao=null --vo=image:outdir=#{temp_directory.shellescape} #{path.to_s.shellescape}")

  out_path = "00000001.jpg"

  img = Magick::Image.read("#{temp_directory}/#{out_path}").first

  p_width = PREVIEW_WIDTH
  p_height = PREVIEW_HEIGHT

  img.resize_to_fill!(p_width, p_height)

  img.page = Magick::Rectangle.new(img.columns, img.rows, 0, 0)
  img = img.extent(p_width, p_height, 0, 0)
  img.excerpt!(0, 0, p_width, p_height)

  img.write(thumbnail_path)
rescue Exception => e
  puts "There was an error generating thumbnail: #{e.inspect}"
ensure
  FileUtils.rm_rf(temp_directory) if temp_directory
end

#get_metadataObject



72
73
74
75
76
77
78
79
80
# File 'lib/videos/video.rb', line 72

def 
  return unless .keys.empty?

  output = `mediainfo -f --Output=XML #{path.to_s.shellescape}`
  doc = Nokogiri::XML.parse(output)
  @metadata["length"] = (e = doc.search("File Duration").first) ? e.text.to_i : 0
  @metadata["width"] = (e = doc.search("File Width").first) ? e.text.to_i : 0
  @metadata["height"] = (e = doc.search("File Height").first) ? e.text.to_i : 0
end

#path_hashObject



13
14
15
# File 'lib/videos/video.rb', line 13

def path_hash
  Digest::SHA256.hexdigest(path.to_s)[0..16]
end

#thumbnail_pathObject



28
29
30
# File 'lib/videos/video.rb', line 28

def thumbnail_path
  videos_package.videos_path + "img/thumbnails/" + "#{path_hash}.jpg"
end

#thumbnail_urlObject



32
33
34
# File 'lib/videos/video.rb', line 32

def thumbnail_url
  videos_package.pathname_to_url(thumbnail_path, videos_package.videos_path)
end

#titleObject



21
22
23
24
25
26
# File 'lib/videos/video.rb', line 21

def title
  title = path.relative_path_from(videos_package.root_path).to_s
  title = title.chomp(path.extname.to_s)
  title = title.gsub("/", " / ")
  title
end

#to_hashObject



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/videos/video.rb', line 88

def to_hash
  {
    "url" => url,
    "title" => title,
    "publishedOn" => path.mtime.to_i,
    "thumbnailUrl" => thumbnail_url,
    "length" => (["length"] / 1000.0).round,
    "width" => ["width"],
    "height" => ["height"],
    "resolution" => approx_resolution,
    "key" => path_hash
  }
end

#urlObject



17
18
19
# File 'lib/videos/video.rb', line 17

def url
  videos_package.pathname_to_url(path, videos_package.videos_path)
end