使用 xmlstarlet 从 xml 解析持续时间

使用 xmlstarlet 从 xml 解析持续时间

该文件看起来像

<MediaInfo xmlns="https://mediaarea.net/mediainfo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://mediaarea.net/mediainfo https://mediaarea.net/mediainfo/mediainfo_2_0.xsd" version="2.0">
    <creatingLibrary version="19.07" url="https://mediaarea.net/MediaInfo">MediaInfoLib</creatingLibrary>
    <media ref="hddvd_demo_1080p.mkv">
        <track type="General">
            <UniqueID>202679344279770072847249189931093807575</UniqueID>
            <VideoCount>1</VideoCount>
            <AudioCount>2</AudioCount>
            <FileExtension>mkv</FileExtension>
            <Format>Matroska</Format>
            <Format_Version>1</Format_Version>
            <FileSize>291843159</FileSize>
            <Duration>121.897</Duration>
            <OverallBitRate>19153427</OverallBitRate>
            <IsStreamable>Yes</IsStreamable>
            <Encoded_Date>UTC 2007-04-07 03:28:47</Encoded_Date>
            <File_Modified_Date>UTC 2019-04-30 05:08:12</File_Modified_Date>
            <File_Modified_Date_Local>2019-04-30 10:38:12</File_Modified_Date_Local>
            <Encoded_Application>mkvmerge v2.0.2 ('You're My Flame') built on Feb 21 2007 23:40:55</Encoded_Application>
            <Encoded_Library>libebml v0.7.7 + libmatroska v0.8.1</Encoded_Library>
        </track>
        <track type="Video">
            <StreamOrder>0</StreamOrder>
            <ID>1</ID>
            <UniqueID>1</UniqueID>
            <Format>VC-1</Format>
            <Format_Profile>Advanced</Format_Profile>
            <Format_Level>3</Format_Level>
            <CodecID>V_MS/VFW/FOURCC / WVC1</CodecID>
            <BitRate>17492274</BitRate>
            <Width>1920</Width>
            <Height>1080</Height>
            <Sampled_Width>1920</Sampled_Width>
            <Sampled_Height>1080</Sampled_Height>
            <PixelAspectRatio>1.000</PixelAspectRatio>
            <DisplayAspectRatio>1.778</DisplayAspectRatio>
            <FrameRate_Mode>VFR</FrameRate_Mode>
            <FrameRate_Original>29.970</FrameRate_Original>
            <ColorSpace>YUV</ColorSpace>
            <ChromaSubsampling>4:2:0</ChromaSubsampling>
            <BitDepth>8</BitDepth>
            <ScanType>Progressive</ScanType>
            <Compression_Mode>Lossy</Compression_Mode>
            <Delay>0.000</Delay>
            <Title>1080p VC-1</Title>
            <Language>en</Language>
            <Default>Yes</Default>
            <Forced>No</Forced>
            <BufferSize>1843200</BufferSize>
        </track>
        <track type="Audio" typeorder="1">
            <StreamOrder>1</StreamOrder>
            <ID>2</ID>
            <UniqueID>418009001</UniqueID>
            <Format>AC-3</Format>
            <Format_Commercial_IfAny>Dolby Digital</Format_Commercial_IfAny>
            <Format_Settings_Endianness>Big</Format_Settings_Endianness>
            <CodecID>A_AC3</CodecID>
            <Duration>121.897</Duration>
            <BitRate_Mode>CBR</BitRate_Mode>
            <BitRate>640000</BitRate>
            <Channels>2</Channels>
            <ChannelPositions>Front: L R</ChannelPositions>
            <ChannelLayout>L R</ChannelLayout>
            <SamplesPerFrame>1536</SamplesPerFrame>
            <SamplingRate>48000</SamplingRate>
            <SamplingCount>5851056</SamplingCount>
            <FrameRate>31.250</FrameRate>
            <BitDepth>16</BitDepth>
            <Compression_Mode>Lossy</Compression_Mode>
            <Delay>0.000</Delay>
            <Delay_Source>Container</Delay_Source>
            <StreamSize>9751760</StreamSize>
            <StreamSize_Proportion>0.03341</StreamSize_Proportion>
            <Title>Dolby Digital 2.0 640kbps</Title>
            <Language>en</Language>
            <ServiceKind>CM</ServiceKind>
            <Default>Yes</Default>
            <Forced>No</Forced>
            <extra>
                <bsid>8</bsid>
                <dialnorm>-31</dialnorm>
                <dsurmod>0</dsurmod>
                <acmod>2</acmod>
                <lfeon>0</lfeon>
                <dialnorm_Average>-31</dialnorm_Average>
                <dialnorm_Minimum>-31</dialnorm_Minimum>
            </extra>
        </track>
        <track type="Audio" typeorder="2">
            <StreamOrder>2</StreamOrder>
            <ID>3</ID>
            <UniqueID>2</UniqueID>
            <Format>E-AC-3</Format>
            <Format_Commercial_IfAny>Dolby Digital Plus</Format_Commercial_IfAny>
            <Format_Settings_Endianness>Big</Format_Settings_Endianness>
            <CodecID>A_EAC3</CodecID>
            <Duration>121.897</Duration>
            <BitRate_Mode>CBR</BitRate_Mode>
            <BitRate>640000</BitRate>
            <Channels>6</Channels>
            <ChannelPositions>Front: L C R, Side: L R, LFE</ChannelPositions>
            <ChannelLayout>L R C LFE Ls Rs</ChannelLayout>
            <SamplesPerFrame>768</SamplesPerFrame>
            <SamplingRate>48000</SamplingRate>
            <SamplingCount>5851056</SamplingCount>
            <FrameRate>62.500</FrameRate>
            <Compression_Mode>Lossy</Compression_Mode>
            <Delay>0.033</Delay>
            <Delay_Source>Container</Delay_Source>
            <StreamSize>9751760</StreamSize>
            <StreamSize_Proportion>0.03341</StreamSize_Proportion>
            <Title>Dolby Digital Plus 5.1 640kbps</Title>
            <Language>en</Language>
            <ServiceKind>CM</ServiceKind>
            <Default>No</Default>
            <Forced>No</Forced>
            <extra>
                <bsid>16</bsid>
                <dialnorm>-27</dialnorm>
                <compr>-0.28</compr>
                <acmod>7</acmod>
                <lfeon>1</lfeon>
                <dialnorm_Average>-27</dialnorm_Average>
                <dialnorm_Minimum>-27</dialnorm_Minimum>
                <compr_Average>-2.99</compr_Average>
                <compr_Minimum>-6.58</compr_Minimum>
                <compr_Maximum>3.15</compr_Maximum>
                <compr_Count>805</compr_Count>
            </extra>
        </track>
    </media>
</MediaInfo>

我想得到的值音频1的使用 xmlstarlet 的持续时间“121.897”。

尝试使用以下命令,

xmlstarlet sel -t -v 'MediaInfo/media/track/Duration' a.xml 

None of the XPaths matched; to match a node in the default namespace
use '_' as the prefix (see section 5.1 in the manual).
For instance, use /_:node instead of /node

答案1

您的 XML 文件使用命名空间,因此您需要指定所需的命名空间并相应地调整查询:

xmlstarlet sel -N 'mi=https://mediaarea.net/mediainfo' -t -v 'mi:MediaInfo/mi:media/mi:track/mi:Duration' a.xml

或依赖默认名称空间,如错误消息所示:

xmlstarlet sel -t -v '_:MediaInfo/_:media/_:track/_:Duration' a.xml

要限制轨道选择,可以添加 XPath 属性过滤器,例如:

xmlstarlet sel -t -v '_:MediaInfo/_:media/_:track[@type="General"]/_:Duration' a.xml

或者,检索第一个“音频”曲目:

xmlstarlet sel -t -v '_:MediaInfo/_:media/_:track[@type="Audio"][1]/_:Duration' a.xml

(使用 XPath 索引),或者

xmlstarlet sel -t -v '_:MediaInfo/_:media/_:track[@type="Audio" and @typeorder="1"]/_:Duration' a.xml

(使用typeorder属性)。

相关内容