【发布时间】:2020-09-30 21:30:36
【问题描述】:
您好,我有下一个 xml 文件:
<?xml version="1.0"?>
<!-- MPD file Generated with GPAC version 0.7.2-DEV-rev986-gb3353c225-master at 2020-04-05T02:33:59.948Z-->
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" minBufferTime="PT1.500S" type="static" mediaPresentationDuration="PT0H3M0.000S" maxSubsegmentDuration="PT0H0M7.000S" profiles="urn:mpeg:dash:profile:isoff-on-demand:2011">
<ProgramInformation moreInformationURL="http://gpac.io">
<Title>video.mpd generated by GPAC</Title>
</ProgramInformation>
<Period duration="PT0H3M0.000S">
<AdaptationSet segmentAlignment="true" maxWidth="2592" maxHeight="1080" maxFrameRate="24" par="2592:1080" lang="eng" startWithSAP="1" subsegmentAlignment="true" subsegmentStartsWithSAP="1">
<Representation id="1" mimeType="video/mp4" codecs="avc1.640032" width="2592" height="1080" frameRate="24" sar="1:1" bandwidth="1729262">
<BaseURL>video-1080_dashinit.mp4</BaseURL>
<SegmentBase indexRangeExact="true" indexRange="920-1263">
<Initialization range="0-919"/>
</SegmentBase>
</Representation>
<Representation id="2" mimeType="video/mp4" codecs="avc1.640020" width="1728" height="720" frameRate="24" sar="1:1" bandwidth="862289">
<BaseURL>video-720_dashinit.mp4</BaseURL>
<SegmentBase indexRangeExact="true" indexRange="919-1262">
<Initialization range="0-918"/>
</SegmentBase>
</Representation>
<Representation id="3" mimeType="video/mp4" codecs="avc1.64001E" width="864" height="360" frameRate="24" sar="1:1" bandwidth="444517">
<BaseURL>video-360_dashinit.mp4</BaseURL>
<SegmentBase indexRangeExact="true" indexRange="919-1262">
<Initialization range="0-918"/>
</SegmentBase>
</Representation>
</AdaptationSet>
<AdaptationSet segmentAlignment="true" lang="eng" startWithSAP="1" subsegmentAlignment="true" subsegmentStartsWithSAP="1">
<Representation id="4" mimeType="audio/mp4" codecs="mp4a.40.2" audioSamplingRate="44100" bandwidth="129580">
<AudioChannelConfiguration schemeIdUri="urn:mpeg:dash:23003:3:audio_channel_configuration:2011" value="2"/>
<BaseURL>video-audio_dashinit.mp4</BaseURL>
<SegmentBase indexRangeExact="true" indexRange="863-1338">
<Initialization range="0-862"/>
</SegmentBase>
</Representation>
</AdaptationSet>
</Period>
</MPD>
我正在尝试从 Python 中的标签中获取属性 segmentAlignment 的值,如下所示:
tree = ET.parse(MPDPath)
root = tree.getroot()
Periods = root.findall('Period')
for Period in Periods:
AdaptationSets = Period.findall('AdaptationSet')
for AdaptationSet in AdaptationSets:
print(AdaptationSet.attrib['segmentAlignment'])
但它总是从该属性中返回 None 或没有提取字符串...
我做错了什么?我知道 MPD 路径是正确的。
【问题讨论】:
-
XML 文档使用默认命名空间(由
xmlns="urn:mpeg:dash:schema:mpd:2011"定义)。这是需要考虑的。一种方法是在所有findall搜索中使用命名空间通配符:root.findall("{*}Period")。见stackoverflow.com/a/62117710/407651
标签: python python-3.x xml elementtree