mkv 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ v0.0.3
2
+ ------
3
+ * Added subtitle extraction options to filter by language (by alxbrun)
4
+ * Some fixes (by alxbrun)
5
+ * More information provided by the extract method
6
+
1
7
  v0.0.2
2
8
  ------
3
9
  * Added default value for some of track's properties
data/lib/mkv.rb CHANGED
@@ -27,9 +27,9 @@ module MKV
27
27
  # @return [Logger]
28
28
  def self.logger
29
29
  return @logger if @logger
30
- logger = Logger.new(STDOUT)
31
- logger.level = Logger::INFO
32
- @logger = logger
30
+ log = Logger.new(STDOUT)
31
+ log.level = Logger::INFO
32
+ @logger = log
33
33
  end
34
34
 
35
35
  # Set the path of the mkvinfo binary.
@@ -26,7 +26,7 @@ module MKV
26
26
  MKV::Track.new track_data
27
27
  end
28
28
 
29
- @invalid = true if @tracks.any?
29
+ @invalid = true unless @tracks.any?
30
30
  @invalid = true if output.include?("is not supported")
31
31
  @invalid = true if output.include?("could not find codec parameters")
32
32
  end
@@ -39,23 +39,40 @@ module MKV
39
39
  def has_video? ; tracks.select { |t| t.type == 'video' }.any? ; end
40
40
  def has_audio? ; tracks.select { |t| t.type == 'audio' }.any? ; end
41
41
 
42
- def extract_subtitles(destination_dir)
43
- tracks.select { |t| t.type == 'subtitles' }.each do |track|
44
- destination_filename = File.basename(@path).gsub(/\.mkv$/i, %Q[.#{track.language}.srt])
45
- command = %Q[#{MKV.mkvextract_binary} tracks "#{@path}" #{track.mkv_info_id}:"#{File.join(destination_dir, destination_filename)}"]
42
+ def has_subtitles?(language)
43
+ tracks.any? { |t| t.type == 'subtitles' && t.language == language}
44
+ end
45
+
46
+ def extract_subtitles(options={})
47
+ # Compatibility with legacy method accepting a String for destination_dir (deprecated)
48
+ if options.class == String
49
+ options = { :destination_dir => options }
50
+ end
46
51
 
47
- MKV.logger.debug "Executing command: #{command}"
52
+ options[:language] ||= []
53
+ options[:language] = [options[:language]].flatten.map(&:to_sym) if options[:language]
54
+
55
+ track_filter = lambda { |t| t.type == 'subtitles' && (options[:language].include?(t.language.to_sym) || options[:language].empty?) }
56
+
57
+ tracks.select(&track_filter).each do |track|
58
+ destination_fileextension = (options[:language].count == 1 ? "" : ".#{track.mkv_info_id}.#{track.language}") + ".srt"
59
+ destination_filename = File.basename(@path).gsub(/\.mkv$/i, destination_fileextension)
60
+ destination_dir = options[:destination_dir] || File.dirname(@path)
61
+
62
+ command = %Q[#{MKV.mkvextract_binary} tracks "#{@path}" #{track.mkv_info_id}:"#{File.join(destination_dir, destination_filename)}"]
63
+ MKV.logger.info(command)
48
64
 
49
65
  output = ""
66
+ start_time = Time.now.to_i
50
67
  Open3.popen3(command) do |stdin, stdout, stderr, wait_thr|
51
68
  begin
52
- yield(0.0) if block_given?
69
+ yield(0.0, 0, destination_filename) if block_given?
53
70
  next_line = Proc.new do |line|
54
71
  output << line
55
72
  if line =~ /(\d+)%/
56
73
  progress = $1.to_i
57
74
 
58
- yield(progress) if block_given?
75
+ yield(progress, Time.now.to_i - start_time, destination_filename) if block_given?
59
76
  end
60
77
 
61
78
  if line =~ /Unsupported codec/
@@ -10,7 +10,7 @@ module MKV
10
10
  attr_reader :language, :enabled, :default, :forced
11
11
 
12
12
  def initialize(data)
13
- (@number, @mkv_info_id) = data.match(/track number:\s(\d+)\s\(track ID for mkvmerge & mkvextract: (\d+)\)/i)[1..2]
13
+ (@number, @mkv_info_id) = data.match(/track number:\s(\d+)\s\(track ID for mkvmerge & mkvextract: (\d+)\)/i)[1..2]
14
14
  @uid = data.match(/track uid: (\d+)/i)[1]
15
15
  @lacing = (data.match(/lacing flag: (\d+)/i) || [0, 0])[1] != '0'
16
16
  @type = data.match(/track type: (\w+)/i)[1]
@@ -23,16 +23,27 @@ module MKV
23
23
 
24
24
  if @type == 'audio'
25
25
  @sampling_frequency = data.match(/sampling frequency: (\d+)/i)[1].to_i
26
- @channels = data.match(/channels: (\d+)/i)[1].to_i
26
+ @channels = (data.match(/channels: (\d+)/i) || [nil, '2'])[1].to_i
27
27
  end
28
28
 
29
29
  if @type == 'audio' || @type == 'subtitles'
30
- @language = data.match(/language: (\w+)/i)[1]
30
+ @language = (data.match(/language: (\w+)/i) || [nil, 'eng'])[1]
31
31
  @enabled = (data.match(/enabled: (\d+)/i) || [0, 1])[1] != '0'
32
32
  @default = (data.match(/default flag: (\d+)/i) || [0, 0])[1] != '0'
33
33
  @forced = (data.match(/forced flag: (\d+)/i) || [0, 0])[1] != '0'
34
34
  end
35
35
  end
36
36
 
37
+ def is_video?
38
+ @type == 'video'
39
+ end
40
+
41
+ def is_audio?
42
+ @type == 'audio'
43
+ end
44
+
45
+ def is_subtitle?
46
+ @type =~ /subtitles?/
47
+ end
37
48
  end
38
49
  end
@@ -1,3 +1,3 @@
1
1
  module MKV
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mkv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-20 00:00:00.000000000 Z
12
+ date: 2013-01-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec