Author: mosu
Date: 2004-09-08 22:13:18 +0400 (Wed, 08 Sep 2004)
New Revision: 766
Added:
trunk/RbMatroska/matroska-utils.rb
trunk/RbMatroska/matroska.rb
trunk/RbMatroska/utilstest.rb
Log:
Added a new class - Matroska::Utils::File for easy access to Matroska files. Gets all segment children by evaluating the seek heads and aborting after finding the first cluster (fast) or skipping over all elements present (slow).
Added: trunk/RbMatroska/matroska-utils.rb
===================================================================
--- trunk/RbMatroska/matroska-utils.rb 2004-09-08 18:06:41 UTC (rev 765)
+++ trunk/RbMatroska/matroska-utils.rb 2004-09-08 18:13:18 UTC (rev 766)
@@ -0,0 +1,98 @@
+#!/usr/bin/ruby
+
+#
+# Distributed under the MIT license
+# see the file COPYING for details
+# or visit http://www.opensource.org/licenses/mit-license.php
+#
+# $Id$
+#
+# A utility class for easy access to a Matroska file.
+#
+# Written by Moritz Bunkus <[email protected]>.
+#
+
+require "ebml"
+require "matroska"
+
+module Matroska
+module Utils
+ class SegmentElement
+ attr_accessor :id
+ attr_accessor :pos
+
+ def initialize(id, pos)
+ @id = id
+ @pos = pos
+ end
+ end
+
+ class File
+ def initialize(io)
+ @io = io
+ @elements = Array.new
+ end # def initialize
+
+ def add_element(id, pos)
+ @elements.each { |e| return if (e.pos == pos) }
+ @elements.push(SegmentElement.new(id, pos))
+ end
+
+ def parse(fully = false)
+ @elements = Array.new
+
+ f = Ebml::File.new(@io)
+ f.map_ids("Matroska")
+ @io.pos = 0
+
+ element = f.read_element_head
+ if ((element.class == NilClass) or
+ (element.class.global_id != Ebml::Head.global_id))
+ raise Ebml::DataError, "No EBML file"
+ end
+ segment_offset = 0
+ while (true)
+ element.skip(f)
+ element = f.read_element_head
+ break if (element.class == NilClass)
+ if (element.class == Matroska::Segment)
+ segment_offset = element.pos + element.head_size
+
+ segelement = f.read_element_head
+ while ((segelement != nil) and
+ (fully or (segelement.id != Matroska::Cluster.global_id)))
+ add_element(segelement.id, segelement.pos)
+ segelement.skip(f)
+ segelement = f.read_element_head
+ end # while (segelement...
+ end # if (element.class...)
+ end # while (true)
+
+ @elements.each do |e|
+ next unless (e.id == Matroska::SeekHead.global_id)
+ f.io.pos = e.pos
+ f.read_element_head.read(f).each(Matroska::Seek.global_id) do |s|
+ id = s.target_id
+ pos = s.target_pos
+ add_element(id, pos + segment_offset) if (id and pos)
+ end # sh.each
+ end # @elements.each
+
+ return sort
+ end # def parse
+
+ def each(filter = Ebml::Id.none)
+ @elements.each do |e|
+ yield(e) if ((filter == Ebml::Id.none) or (filter == e.id))
+ end
+ end # def each
+
+ def sort
+ @elements.sort! { |a, b| a.pos <=> b.pos }
+ return self
+ end # def sort
+
+ end # class File
+
+end # module Utils
+end # module Matroska
Property changes on: trunk/RbMatroska/matroska-utils.rb
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: trunk/RbMatroska/matroska.rb
===================================================================
--- trunk/RbMatroska/matroska.rb 2004-09-08 18:06:41 UTC (rev 765)
+++ trunk/RbMatroska/matroska.rb 2004-09-08 18:13:18 UTC (rev 766)
@@ -0,0 +1,43 @@
+#!/usr/bin/ruby
+
+#
+# Distributed under the MIT license
+# see the file COPYING for details
+# or visit http://www.opensource.org/licenses/mit-license.php
+#
+# $Id$
+#
+# Extensions of the base Matroska classes.
+#
+# Written by Moritz Bunkus <[email protected]>.
+#
+
+require "ebml"
+require "matroska-baseclasses.rb"
+
+module Matroska
+ class Seek
+ def target_id
+ e = find(SeekID)
+ return e.to_id if (e)
+ return Ebml::Id.none
+ end
+
+ def target_pos
+ e = find(SeekPosition)
+ return e.to_i if (e)
+ return nil
+ end
+ end # class Seek
+
+ class SeekID
+ def to_id
+ if ((@data_size > 4) or (@data_size < 1))
+ raise DataError, "Data size invalid for an Ebml::Id"
+ end
+ v = 0
+ @value.unpack("C*").each { |b| v = (v << 8) + b }
+ return Ebml::Id.new(v, @data_size)
+ end
+ end # class SeekID
+end # module Matroska
Property changes on: trunk/RbMatroska/matroska.rb
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: trunk/RbMatroska/utilstest.rb
===================================================================
--- trunk/RbMatroska/utilstest.rb 2004-09-08 18:06:41 UTC (rev 765)
+++ trunk/RbMatroska/utilstest.rb 2004-09-08 18:13:18 UTC (rev 766)
@@ -0,0 +1,30 @@
+#!/usr/bin/ruby
+
+#
+# Distributed under the MIT license
+# see the file COPYING for details
+# or visit http://www.opensource.org/licenses/mit-license.php
+#
+# $Id$
+#
+# A test application for the Matroska::Utils classes.
+#
+# Written by Moritz Bunkus <[email protected]>.
+#
+
+require "ebml"
+require "matroska-utils"
+
+ebml = Ebml::File.new(nil)
+ebml.map_ids("Matroska")
+
+ARGV.each do |name|
+ io = File.new(name)
+ f = Matroska::Utils::File.new(io)
+ f.parse.each do |e|
+ puts("Id " + e.id.to_s + " at " + e.pos.to_s + ", name " +
+ ebml.find_class_for_id(e.id).name)
+ end
+
+ io.close
+end
Property changes on: trunk/RbMatroska/utilstest.rb
___________________________________________________________________
Name: svn:executable
+ *
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.