Anonymous avatar Anonymous committed 456cc34

Add MPEG2-TS parser

Comments (0)

Files changed (2)

hachoir-parser/hachoir_parser/video/__init__.py

 from hachoir_parser.video.flv import FlvFile
 from hachoir_parser.video.mov import MovFile
 from hachoir_parser.video.mpeg_video import MPEGVideoFile
+from hachoir_parser.video.mpeg_ts import MPEG_TS
 

hachoir-parser/hachoir_parser/video/mpeg_ts.py

+"""
+MPEG-2 Transport Stream parser.
+
+Author: Victor Stinner
+Creation date: 13 january 2007
+"""
+
+from hachoir_parser import Parser
+from hachoir_core.field import (FieldSet, ParserError,
+    UInt8, Bit, Bits, RawBytes)
+from hachoir_core.endian import BIG_ENDIAN
+from hachoir_core.text_handler import hexadecimal
+
+class Packet(FieldSet):
+    def __init__(self, *args):
+        FieldSet.__init__(self, *args)
+        if self["has_error"].value:
+            self._size = 204*8
+        else:
+            self._size = 188*8
+
+    def createFields(self):
+        yield Bits(self, "sync", 8)
+        if self["sync"].value != 0x47:
+            raise ParserError("MPEG-2 TS: Invalid synchronization byte")
+        yield Bit(self, "has_error")
+        yield Bit(self, "payload_unistart")
+        yield Bit(self, "priority")
+        yield Bits(self, "pid", 13)
+        yield Bits(self, "scrambling_control", 2)
+        yield Bits(self, "adaptation_field_control", 2)
+        yield Bits(self, "counter", 4)
+        yield RawBytes(self, "payload", 184)
+        if self["has_error"].value:
+            yield RawBytes(self, "error_correction", 16)
+
+class MPEG_TS(Parser):
+    tags = {
+        "file_ext": ("ts",),
+        "min_size": 188*8,
+        "description": u"MPEG-2 Transport Stream"
+    }
+    endian = BIG_ENDIAN
+
+    def validate(self):
+        return True
+
+    def createFields(self):
+        while not self.eof:
+            yield Packet(self, "packet[]")
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.