Class: Musical::DVD

Inherits:
Object
  • Object
show all
Extended by:
Util
Includes:
Util, Singleton
Defined in:
lib/musical/dvd.rb

Defined Under Namespace

Classes: Chapter, Wav

Constant Summary collapse

DETECT_ERROR_MESSAGE =
'Not detect DVD, Try `DVD.load` and check your drive device path.'
DRIVE_NOT_FOUND_MESSAGE =
'DVD drive is not found.'
DVD_NOT_INSERTED_MESSAGE =
'DVD is not inserted.'
@@path =
nil

Constants included from Util

Util::REQUIRED_APPS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

check_env, execute_command, installed?

Instance Attribute Details

#artistObject

Returns the value of attribute artist.



10
11
12
# File 'lib/musical/dvd.rb', line 10

def artist
  @artist
end

#titleObject

Returns the value of attribute title.



10
11
12
# File 'lib/musical/dvd.rb', line 10

def title
  @title
end

#yearObject

Returns the value of attribute year.



10
11
12
# File 'lib/musical/dvd.rb', line 10

def year
  @year
end

Class Method Details

.detectObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/musical/dvd.rb', line 18

def self.detect
  drutil_out = execute_command('drutil status')

  raise RuntimeError.new DRIVE_NOT_FOUND_MESSAGE  unless drutil_out
  raise RuntimeError.new DVD_NOT_INSERTED_MESSAGE unless drutil_out.include?('Name:')

  file_system = drutil_out.split("\n").select do |line|
    line.include?('Name:')
  end.first.match(/Name: (.+)/)[1]
end

.load(options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/musical/dvd.rb', line 37

def self.load(options = {})
  if @@path.nil? || options[:forcibly]
    @@path = options[:path] || self.detect
  end

  dvd = DVD.instance
  dvd.title  = options[:title]  || Musical.configuration.title
  dvd.artist = options[:artist] || Musical.configuration.artist
  dvd.year   = options[:year]   || Musical.configuration.year

  if block_given?
    yield(dvd)
  end

  dvd.info
end

.pathObject



33
34
35
# File 'lib/musical/dvd.rb', line 33

def self.path
  @@path
end

.path=(path) ⇒ Object



29
30
31
# File 'lib/musical/dvd.rb', line 29

def self.path=(path)
  @@path = path
end

Instance Method Details

#infoObject



54
55
56
57
58
59
60
61
62
# File 'lib/musical/dvd.rb', line 54

def info
  raise RuntimeError.new DETECT_ERROR_MESSAGE unless @@path

  return @info if @info

  @info = execute_command("dvdbackup --info --input='#{@@path}'", true)
  raise RuntimeError.new DETECT_ERROR_MESSAGE if @info.empty?
  @info
end

#ripObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/musical/dvd.rb', line 83

def rip
  raise RuntimeError.new DETECT_ERROR_MESSAGE unless @@path
  save_dir = Musical.configuration.output
  FileUtils.mkdir_p save_dir

  chapters = []

  title_sets.each do |title_set|
    chapters << (1..title_set[:chapter]).map do |chapter_index|
      commands = []
      commands << 'dvdbackup'
      commands << "--input='#{@@path}'"
      commands << "--title='#{title_set[:title]}'"
      commands << "--start=#{chapter_index}"
      commands << "--end=#{chapter_index}"
      commands << "--output='#{Musical.configuration.working_dir}'"
      execute_command(commands.join(' '), true)

      vob_save_path = "#{save_dir}/TITLE_#{title_set[:title]}_#{chapter_index}.VOB"
      FileUtils.mv(vob_path, vob_save_path)

      yield if block_given?

      Chapter.new(vob_save_path, title_number: title_set[:title], chapter_number: chapter_index)
    end
  end
  chapters.flatten
end

#title_setsObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/musical/dvd.rb', line 64

def title_sets
  return @title_sets if @title_sets

  @title_sets = [].tap do |sets|
    sets_regexp = /\s*Title (\d) has (\d*) chapter/
    info.split("\n").each do |line|
      if line =~ sets_regexp
        sets << { title: $1.to_i, chapter: $2.to_i }
      end
    end
  end
end