Class: MKV::Movie

Inherits:
Object
  • Object
show all
Defined in:
lib/mkv/movie.rb

Constant Summary collapse

@@timeout =
200

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Movie

Returns a new instance of Movie.

Raises:

  • (Errno::ENOENT)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mkv/movie.rb', line 10

def initialize(path)
  raise Errno::ENOENT, "the file '#{path}' does not exist" unless File.exists?(path)

  @path = path

  # mkvinfo will output to stdout
  command = "#{MKV.mkvinfo_binary} #{Shellwords.escape(path)}"
  MKV.logger.info(command)
  output = Open3.popen3(command) { |stdin, stdout, stderr| stdout.read}

  match = output.gsub(/\n/, '$$').match /\|\+\ssegment tracks(.*?)\|\+\s(?:chapters|cluster)/i
  tracks = match[1].gsub(/\$\$/, "\n")
  match_tracks = tracks.gsub(/\n/, '$$').scan(/a track(.*?)(?:\|\s\+|$)/i)
  match_tracks = match_tracks.map { |x| x.first.gsub(/\$\$/, "\n") }

  @tracks = match_tracks.map do |track_data|
    MKV::Track.new track_data
  end

  @invalid = true if @tracks.any?
  @invalid = true if output.include?("is not supported")
  @invalid = true if output.include?("could not find codec parameters")
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/mkv/movie.rb', line 7

def path
  @path
end

#tracksObject (readonly)

Returns the value of attribute tracks.



8
9
10
# File 'lib/mkv/movie.rb', line 8

def tracks
  @tracks
end

Instance Method Details

#extract_subtitles(destination_dir) ⇒ Object



42
43
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
71
72
73
74
75
76
77
78
79
# File 'lib/mkv/movie.rb', line 42

def extract_subtitles(destination_dir)
  tracks.select { |t| t.type == 'subtitles' }.each do |track|
    destination_filename = File.basename(@path).gsub(/\.mkv$/i, %Q[.#{track.language}.srt])
    command = %Q[#{MKV.mkvextract_binary} tracks "#{@path}" #{track.mkv_info_id}:"#{File.join(destination_dir, destination_filename)}"]

    MKV.logger.debug "Executing command: #{command}"

    output = ""
    Open3.popen3(command) do |stdin, stdout, stderr, wait_thr|
      begin
        yield(0.0) if block_given?
        next_line = Proc.new do |line|
          output << line
          if line =~ /(\d+)%/
            progress = $1.to_i

            yield(progress) if block_given?
          end

          if line =~ /Unsupported codec/
            MKV.logger.error "Failed encoding...\nCommand\n#{command}\nOutput\n#{output}\n"
            raise "Failed encoding: #{line}"
          end
        end

        if @@timeout
          stdout.each_with_timeout(wait_thr.pid, @@timeout, "r", &next_line)
        else
          stdout.each("r", &next_line)
        end

      rescue Timeout::Error => e
        MKV.logger.error "Process hung...\nCommand\n#{command}\nOutput\n#{output}\n"
        raise MKV::Error, "Process hung. Full output: #{output}"
      end
    end
  end
end

#has_audio?Boolean

Returns:

  • (Boolean)


40
# File 'lib/mkv/movie.rb', line 40

def has_audio? ; tracks.select { |t| t.type == 'audio' }.any? ; end

#has_subtitles?Boolean

Returns:

  • (Boolean)


38
# File 'lib/mkv/movie.rb', line 38

def has_subtitles? ; tracks.select { |t| t.type == 'subtitles' }.any? ; end

#has_video?Boolean

Returns:

  • (Boolean)


39
# File 'lib/mkv/movie.rb', line 39

def has_video? ; tracks.select { |t| t.type == 'video' }.any? ; end

#valid?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/mkv/movie.rb', line 34

def valid?
  not @invalid
end