Module: StickyFlag::Tags::MKV

Defined in:
lib/stickyflag/tags/mkv.rb

Class Method Summary collapse

Class Method Details

.clear(file_name, mkvextract_path = 'mkvextract', mkvpropedit_path = 'mkvpropedit') ⇒ Object



193
194
195
196
197
198
199
200
201
# File 'lib/stickyflag/tags/mkv.rb', line 193

def clear(file_name, mkvextract_path = 'mkvextract', mkvpropedit_path = 'mkvpropedit')
  xml_doc = get_tag_xml(file_name, mkvextract_path, mkvpropedit_path)
  stickyflag_tag = get_stickyflag_tag(xml_doc)
  
  unless stickyflag_tag.nil?
    stickyflag_tag.remove
    set_tag_xml(xml_doc, file_name, mkvextract_path, mkvpropedit_path)
  end
end

.config_valuesObject



15
16
17
# File 'lib/stickyflag/tags/mkv.rb', line 15

def config_values
  [ :mkvextract_path, :mkvpropedit_path ]
end

.extensionsObject



12
13
14
# File 'lib/stickyflag/tags/mkv.rb', line 12

def extensions
  [ '.mkv' ]
end

.get(file_name, mkvextract_path = 'mkvextract', mkvpropedit_path = 'mkvpropedit') ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/stickyflag/tags/mkv.rb', line 123

def get(file_name, mkvextract_path = 'mkvextract', mkvpropedit_path = 'mkvpropedit')
  xml_doc = get_tag_xml(file_name, mkvextract_path, mkvpropedit_path)
  stickyflag_tag = get_stickyflag_tag(xml_doc)
  return [] if stickyflag_tag.nil?
  
  tag_string = stickyflag_tag.content
  return [] if tag_string.nil? || tag_string.empty?
  
  tag_string.split(',').map { |t| t.empty? ? nil : t.strip }.compact
end

.get_stickyflag_root(xml_doc) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/stickyflag/tags/mkv.rb', line 67

def get_stickyflag_root(xml_doc)
  xml_doc.xpath("/Tags/Tag").each do |tag|
    targets = tag.at_xpath("Targets")
    next if targets.nil? # This shouldn't ever happen
    
    # If there's no UID elements under this tag, then we're good (note
    # that sometimes mkvpropedit will *force* in a TargetTypeValue or
    # a TargetType, and we don't want to block on those)
    good = true
    targets.children.each do |child|
      if child.name.end_with? 'UID'
        good = false
        break
      end
    end
    
    return tag if good
  end
  
  # Make one, then
  tag_tag = Nokogiri::XML::Node.new 'Tag', xml_doc
  targets_tag = Nokogiri::XML::Node.new 'Targets', xml_doc
  tag_tag.add_child targets_tag

  root_elem = xml_doc.at_xpath("/Tags")
  root_elem.add_child tag_tag
  
  tag_tag
end

.get_stickyflag_tag(xml_doc) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/stickyflag/tags/mkv.rb', line 97

def get_stickyflag_tag(xml_doc)
  tag_tag = get_stickyflag_root(xml_doc)
  if tag_tag.nil?
    # Should never happen!
    raise Thor::Error.new("INTERNAL ERROR: Failed to find the StickyFlag tag root in MKV XML")
  end
  
  tag_tag.at_xpath("Simple[Name = 'X_STICKYFLAG_FLAGS']/String")
end

.get_tag_xml(file_name, mkvextract_path = 'mkvextract', mkvpropedit_path = 'mkvpropedit') ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/stickyflag/tags/mkv.rb', line 19

def get_tag_xml(file_name, mkvextract_path = 'mkvextract', mkvpropedit_path = 'mkvpropedit')
  stdout_str = ''
  stderr_str = ''

  begin
    Open3.popen3("#{mkvextract_path} tags \"#{file_name}\" -q") do |i, o, e, t|
      out_reader = Thread.new { o.read }
      err_reader = Thread.new { e.read }
      i.close
      stdout_str = out_reader.value
      stderr_str = err_reader.value

      stdout_str.force_encoding("UTF-8") if RUBY_VERSION >= "1.9.0"
      stderr_str.force_encoding("UTF-8") if RUBY_VERSION >= "1.9.0"
    end
  rescue Exception
    raise Thor::Error.new("ERROR: Failed to get tags for #{file_name}; mkvextract call failed")
  end
  if stderr_str.start_with?("Error: ") || stderr_str.include?("Errno::ENOENT") || stdout_str.start_with?("Error: ")
    raise Thor::Error.new("ERROR: Failed to get tags for #{file_name}; mkvextract call failed")
  end
  
  if stdout_str == ''
    # This is what happens when a file has no tags whatsoever, we need
    # to build a skeleton document
    stdout_str = <<-XML
    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE Tags SYSTEM "matroskatags.dtd">

    <Tags>
      <Tag>
        <Targets>
        </Targets>
      </Tag>
    </Tags>
    XML
  end
  
  # Strip off newlines and BOM, these wreak havoc on the Java XML parser,
  # which considers them content before the prolog
  bom = "\xEF\xBB\xBF"
  bom.force_encoding("UTF-8") if RUBY_VERSION >= "1.9.0"
  stdout_str.strip!.gsub!(bom, '')
  
  Nokogiri::XML(stdout_str)        
end

.set(file_name, tag, mkvextract_path = 'mkvextract', mkvpropedit_path = 'mkvpropedit') ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/stickyflag/tags/mkv.rb', line 134

def set(file_name, tag, mkvextract_path = 'mkvextract', mkvpropedit_path = 'mkvpropedit')
  xml_doc = get_tag_xml(file_name, mkvextract_path, mkvpropedit_path)
  stickyflag_tag = get_stickyflag_tag(xml_doc)
  
  unless stickyflag_tag.nil?
    # We already have the right tag, check it
    tag_string = stickyflag_tag.content
    unless tag_string.nil? || tag_string.empty?
      tags = tag_string.split(',').map { |t| t.empty? ? nil : t.strip }.compact
      return if tags.include? tag
      tags << tag
      
      # Set the new string in the XML file
      new_tag_string = tags.join(', ')
      stickyflag_tag.content = new_tag_string
    else
      # Somehow the tag has no content, write it in
      stickyflag_tag.content = tag
    end
  else
    # No tag, add it
    root = get_stickyflag_root(xml_doc)
    simple_tag = Nokogiri::XML::Node.new 'Simple', xml_doc
    
    name_tag = Nokogiri::XML::Node.new 'Name', xml_doc
    name_tag.content = 'X_STICKYFLAG_FLAGS'
    string_tag = Nokogiri::XML::Node.new 'String', xml_doc
    string_tag.content = tag
    
    simple_tag.add_child(name_tag)
    simple_tag.add_child(string_tag)
    
    root.add_child(simple_tag)
  end
  
  set_tag_xml(xml_doc, file_name, mkvextract_path, mkvpropedit_path)
end

.set_tag_xml(xml_doc, file_name, mkvextract_path = 'mkvextract', mkvpropedit_path = 'mkvpropedit') ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/stickyflag/tags/mkv.rb', line 107

def set_tag_xml(xml_doc, file_name, mkvextract_path = 'mkvextract', mkvpropedit_path = 'mkvpropedit')
  # Write out this XML file and attach it to the MKV
  outfile = Tempfile.new_with_encoding ['sfmkvtag', '.xml']
  begin
    outfile.write(xml_doc.to_xml)
    outfile.close
    
    ret = system(mkvpropedit_path, file_name, '--tags', "all:#{outfile.path}", "-q")
    unless ret == true
      raise Thor::Error.new("ERROR: Failed to update tag for #{file_name}; mkvpropedit call failed")
    end
  ensure
    outfile.unlink
  end
end

.unset(file_name, tag, mkvextract_path = 'mkvextract', mkvpropedit_path = 'mkvpropedit') ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/stickyflag/tags/mkv.rb', line 172

def unset(file_name, tag, mkvextract_path = 'mkvextract', mkvpropedit_path = 'mkvpropedit')
  xml_doc = get_tag_xml(file_name, mkvextract_path, mkvpropedit_path)
  stickyflag_tag = get_stickyflag_tag(xml_doc)
  
  unless stickyflag_tag.nil?
    # We already have the right tag, check it
    tag_string = stickyflag_tag.content
    unless tag_string.nil? || tag_string.empty?
      tags = tag_string.split(',').map { |t| t.empty? ? nil : t.strip }.compact
      return unless tags.include? tag
      tags.delete(tag)
      
      # Set the new string in the XML file
      new_tag_string = tags.join(', ')
      stickyflag_tag.content = new_tag_string
      
      set_tag_xml(xml_doc, file_name, mkvextract_path, mkvpropedit_path)
    end
  end
end