| 1 |
from urllib.request import urlopen |
| 2 |
from xml.dom import minidom |
| 3 |
|
| 4 |
from mpegdash.nodes import MPEGDASH |
| 5 |
from mpegdash.utils import parse_child_nodes, write_child_node |
| 6 |
from mpegdash.prettyprinter import pretty_print |
| 7 |
|
| 8 |
|
| 9 |
class MPEGDASHParser(object): |
| 10 |
@classmethod |
| 11 |
def load_xmldom(cls, string_or_url): |
| 12 |
if '<MPD' in string_or_url: |
| 13 |
mpd_string = string_or_url |
| 14 |
else: |
| 15 |
try: |
| 16 |
mpd_string = urlopen(string_or_url).read() |
| 17 |
except ValueError: |
| 18 |
with open(string_or_url, 'r') as f: |
| 19 |
mpd_string = f.read() |
| 20 |
|
| 21 |
return minidom.parseString(mpd_string) |
| 22 |
|
| 23 |
@classmethod |
| 24 |
def parse(cls, string_or_url): |
| 25 |
xml_root_node = cls.load_xmldom(string_or_url) |
| 26 |
return parse_child_nodes(xml_root_node, 'MPD', MPEGDASH)[0] |
| 27 |
|
| 28 |
@classmethod |
| 29 |
def get_as_doc(cls, mpd): |
| 30 |
xml_doc = minidom.Document() |
| 31 |
write_child_node(xml_doc, 'MPD', mpd) |
| 32 |
return xml_doc |
| 33 |
|
| 34 |
@classmethod |
| 35 |
def write(cls, mpd, filepath): |
| 36 |
with open(filepath, 'w') as f: |
| 37 |
cls.get_as_doc(mpd).writexml(f, indent=' ', addindent=' ', newl='\n') |
| 38 |
|
| 39 |
@classmethod |
| 40 |
def toprettyxml(cls, mpd): |
| 41 |
return pretty_print(cls.get_as_doc(mpd).toxml()) |