Class: DVDConverter::Converter

Inherits:
Object
  • Object
show all
Includes:
Log4r
Defined in:
lib/dvd_converter/converter.rb

Instance Method Summary collapse

Constructor Details

#initialize(track, audio_tracks, subtitle_tracks = [], options = {}) ⇒ Converter

Returns a new instance of Converter.



10
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
# File 'lib/dvd_converter/converter.rb', line 10

def initialize(track, audio_tracks, subtitle_tracks = [], options = {})
  # Store the tracks
  @track = track
  @audio_tracks = audio_tracks
  @subtitle_tracks = subtitle_tracks
  @options = options

  # Build the environment
  @temp_dir = "#{TEMP_DIR}-#{track.track_id}"
  Dir.mkdir @temp_dir

  # Build the logger
  @logger = Logger.new 'converter_log'

  formatter = PatternFormatter.new :pattern => "Track #{track.track_id}: %m"

  console_out = StdoutOutputter.new 'console'
  console_out.level = INFO
  console_out.formatter = formatter
  @logger.outputters << console_out

  file_out = FileOutputter.new 'file', :filename => File.join(@temp_dir, 'converter.log')
  file_out.level = DEBUG
  file_out.formatter = formatter
  @logger.outputters << file_out
end

Instance Method Details

#cleanupObject



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

def cleanup
  # Build some paths
  output_file = File.join @temp_dir, @track.track_id.to_s + '.mkv'
  merged_file = File.join @temp_dir, @track.track_id.to_s + '-with-subs.mkv'
  logger_file = File.join @temp_dir, 'converter.log'

  # Move out the files
  if File.exists? merged_file
    FileUtils.mv merged_file, "#{@track.track_id}.mkv"
  else
    FileUtils.mv output_file, "#{@track.track_id}.mkv"
  end
  FileUtils.mv logger_file, "#{@track.track_id}.log"

  # Remove the temp folder
  FileUtils.rm_rf @temp_dir
end

#convert_movieObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dvd_converter/converter.rb', line 55

def convert_movie
  # Build some paths
  output_file = File.join @temp_dir, @track.track_id.to_s + '.mkv'

  # Build the command to extract the movie
  command  = "#{HANDBRAKECLI_PATH} -i \"#{@track.movie.source}\" -t #{@track.track_id} "
  command += "-o \"#{output_file}\" -f mkv "
  command += "-e x264 -q 0.65 -b 2000 -2 -T -m "
  command += "-9 " if @options[:detelecine]
  command += "-5 " if @options[:decomb]
  command += "-a #{@audio_tracks.map { |a| a.track_id }.join(',')} "
  command += "-E #{@audio_tracks.map { |a| a.audio_type }.join(',')} "
  command += "-c #{@options[:preview]} " if @options[:preview]
  command += "2>&1"

  # Run the command
  @logger.info "Extracting Track #{@track.track_id}:"
  @logger.info command

  f = IO.popen(command)
  bytes = []
  f.each_byte do |b|
    if b == 13 || b == 10 # Carriage Return or New Line
      line = bytes.pack('c*')
      if line =~ /Encoding: task \d+ of \d+/
        @logger.info line
      else
        @logger.debug line
      end
      bytes = []
    else
      bytes << b
    end
  end
  @logger.debug bytes.pack('c*') unless bytes.empty?
end

#convert_subtitlesObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dvd_converter/converter.rb', line 92

def convert_subtitles
  output_file = File.join @temp_dir, @track.track_id.to_s + '.mkv'

  @subtitle_tracks.each do |subtitle|
    # Build some paths
    subtitle_file = File.join @temp_dir, 'subs-' + subtitle.code + '-' + subtitle.track_id.to_s
    vobsub_file = File.join @temp_dir, 'vobsubs-' + subtitle.code + '-' + subtitle.track_id.to_s
    vts_file = File.join @track.movie.source, 'VIDEO_TS', 'VTS_' + sprintf('%02d', @track.track_id) + '_0.IFO'

    # Build the command to convert the subtitle
    command  = "#{SUBTITLE2VOBSUB_PATH} -o \"#{vobsub_file}\" -i "
    command += "\"#{vts_file}\" "
    command += "< \"#{subtitle_file}\""

    # Run the command to convert the subtitle
    @logger.info "Converting Subtitle #{subtitle.track_id}, #{subtitle.code}"
    @logger.info command

    f = IO.popen(command)
    f.each_line do |line|
      @logger.debug line.strip
    end
  end
end

#extract_subtitlesObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/dvd_converter/converter.rb', line 117

def extract_subtitles
  output_file = File.join @temp_dir, @track.track_id.to_s + '.mkv'

  @subtitle_tracks.each do |subtitle|
    # Build some paths
    subtitle_file = File.join @temp_dir, 'subs-' + subtitle.code + '-' + subtitle.track_id.to_s

    # Build the command to extract the subtitle
    command  = "#{TCCAT_PATH} -i \"#{@track.movie.source}\" -T #{@track.track_id},-1 2>/dev/null | "
    command += "#{TCEXTRACT_PATH} -x ps1 -t vob -a 0x#{subtitle.hex} "
    command += "> \"#{subtitle_file}\""

    # Run the command to extract the subtitle
    @logger.info "Extracting Subtitle #{subtitle.track_id}, #{subtitle.code}"
    @logger.info command

    f = IO.popen(command)
    f.each_line do |line|
      @logger.debug line.strip
    end
  end
end

#merge_subtitlesObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/dvd_converter/converter.rb', line 140

def merge_subtitles
  output_file = File.join @temp_dir, @track.track_id.to_s + '.mkv'
  merged_file = File.join @temp_dir, @track.track_id.to_s + '-with-subs.mkv'

  # Build the command to merge the subtitles
  command = "#{MKVMERGE_PATH} -o \"#{merged_file}\" \"#{output_file}\" "

  # Add the subtitles to the command
  @subtitle_tracks.each do |subtitle|
    vobsub_file = File.join @temp_dir, 'vobsubs-' + subtitle.code + '-' + subtitle.track_id.to_s + '.idx'
    command += "--language 0:#{subtitle.code} \"#{vobsub_file}\" "
  end

  # Run the command to merge the subtitles
  @logger.info "Merging Subtitles in to movie"
  @logger.info command

  f = IO.popen(command)
  f.each_line do |line|
    @logger.debug line.strip
  end
end

#processObject



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/dvd_converter/converter.rb', line 163

def process
  convert_movie
  
  unless @subtitle_tracks.empty?
    extract_subtitles
    convert_subtitles
    merge_subtitles
  end

  cleanup
end