Class: OSDb::MovieFile

Inherits:
Object
  • Object
show all
Defined in:
lib/osdb/movie_file.rb

Constant Summary collapse

EXTENSIONS =
%w(avi mpg m4v mkv mov ogv mp4)
CHUNK_SIZE =

in bytes

64 * 1024

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ MovieFile

Returns a new instance of MovieFile.



8
9
10
# File 'lib/osdb/movie_file.rb', line 8

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/osdb/movie_file.rb', line 6

def path
  @path
end

Class Method Details

.compute_hash(path) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/osdb/movie_file.rb', line 37

def self.compute_hash(path)
  filesize = File.size(path)
  hash = filesize
  
  # Read 64 kbytes, divide up into 64 bits and add each
  # to hash. Do for beginning and end of file.
  File.open(path, 'rb') do |f|    
    # Q = unsigned long long = 64 bit
    f.read(CHUNK_SIZE).unpack("Q*").each do |n|
      hash = hash + n & 0xffffffffffffffff # to remain as 64 bit number
    end
    
    f.seek([0, filesize - CHUNK_SIZE].max, IO::SEEK_SET)
    
    # And again for the end of the file
    f.read(CHUNK_SIZE).unpack("Q*").each do |n|
      hash = hash + n & 0xffffffffffffffff
    end
  end
  
  sprintf("%016x", hash)
end

Instance Method Details

#has_sub?Boolean

Returns:

  • (Boolean)


12
13
14
15
16
# File 'lib/osdb/movie_file.rb', line 12

def has_sub?
  exist = false
  %w(.srt .sub).each{ |ext| exist ||= File.exist?(path.gsub(File.extname(path), ext)) }
  exist
end

#hashObject



22
23
24
# File 'lib/osdb/movie_file.rb', line 22

def hash
  @hash ||= self.class.compute_hash(path)
end

#nameObject



30
31
32
# File 'lib/osdb/movie_file.rb', line 30

def name
  @name ||= File.basename(path, File.extname(path))
end

#sizeObject



26
27
28
# File 'lib/osdb/movie_file.rb', line 26

def size
  @size ||= File.size(path)
end

#sub_path(format) ⇒ Object



18
19
20
# File 'lib/osdb/movie_file.rb', line 18

def sub_path(format)
  path.gsub(File.extname(path), ".#{format}")
end