6 Replies Latest reply: Jul 17, 2014 5:43 AM by VikingOSX
MichaelRow12 Level 1 Level 1 (0 points)

I have a XML file which I get from Mediainfo, and I want to get the value of Format of the Audio Track from it (In this case, ALAC) using AppleScript. Could anybody help me with this? Thanks.

<?xml version="1.0" encoding="UTF-8"?>
<Mediainfo version="0.7.69">
<track type="General">
<Complete_name>/Volumes/Macintosh HDD/Movies/Strike the Blood 12.mov</Complete_name>
<Codec_ID>qt  </Codec_ID>
<File_size>1.64 GiB</File_size>
<Duration>23mn 41s</Duration>
<Overall_bit_rate>9 887 Kbps</Overall_bit_rate>
<Movie_name>[ReinForce] Strike the Blood - 12 (BDRip 1920x1080 x264 FLAC)</Movie_name>
<Encoded_date>UTC 1904-01-01 00:00:00</Encoded_date>
<Tagged_date>UTC 1904-01-01 00:00:00</Tagged_date>

<track type="Video">
<Format_Info>Advanced Video Codec</Format_Info>
<Format_settings__ReFrames>4 frames</Format_settings__ReFrames>
<Codec_ID_Info>Advanced Video Coding</Codec_ID_Info>
<Duration>23mn 41s</Duration>
<Bit_rate>9 084 Kbps</Bit_rate>
<Width>1 920 pixels</Width>
<Height>1 080 pixels</Height>
<Frame_rate>23.976 fps</Frame_rate>
<Bit_depth>8 bits</Bit_depth>
<Stream_size>1.50 GiB (92%)</Stream_size>
<Writing_library>x264 core 142</Writing_library>
<Encoding_settings>cabac=1 / ref=4 / deblock=1:-2:-2 / analyse=0x3:0x113 / me=umh / subme=9 / psy=1 / psy_rd=0.70:0.00 / mixed_ref=0 / me_range=32 / chroma_me=1 / trellis=2 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=32 / lookahead_threads=5 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=9 / b_pyramid=2 / b_adapt=2 / b_bias=0 / direct=3 / weightb=1 / open_gop=0 / weightp=2 / keyint=250 / keyint_min=23 / scenecut=40 / intra_refresh=0 / rc_lookahead=40 / rc=crf / mbtree=1 / crf=16.0 / qcomp=0.70 / qpmin=10 / qpmax=20 / qpstep=4 / ip_ratio=1.40 / aq=1:1.00</Encoding_settings>
<Encoded_date>UTC 1904-01-01 00:00:00</Encoded_date>
<Tagged_date>UTC 1904-01-01 00:00:00</Tagged_date>

<track type="Audio">
<Codec_ID_Info>Apple Lossless Audio Codec</Codec_ID_Info>
<Duration>23mn 41s</Duration>
<Bit_rate>799 Kbps</Bit_rate>
<Channel_s_>2 channels</Channel_s_>
<Channel_positions>Front: L R</Channel_positions>
<Sampling_rate>48.0 KHz</Sampling_rate>
<Bit_depth>16 bits</Bit_depth>
<Stream_size>135 MiB (8%)</Stream_size>
<Encoded_date>UTC 1904-01-01 00:00:00</Encoded_date>
<Tagged_date>UTC 1904-01-01 00:00:00</Tagged_date>

  • Tony T1 Level 6 Level 6 (8,720 points)

    set theFile to choose file

    set AudioTrack to do shell script "sed -n '/<track type=\"Audio\">/,/<\\/track>/ s/^<Format>\\(.*\\)<\\/Format>.*$/\\1/p' " & quoted form of POSIX path of theFile

    display dialog AudioTrack

  • Frank Caggiano Level 7 Level 7 (25,715 points)

    Look at the dictionary entry for System Events under XML suite. Also see this Storing and Accessing Data with AppleScript - MacTech ... (about 2/3 of the war down the page) for routines to read data from an XML file.

  • Hiroto Level 5 Level 5 (5,620 points)



    You may try something like the following script. It is indeed a Perl script wrapped in AppleScript, though.



    set f to (choose file with prompt "Choose the XML file")'s POSIX path
    on get_audio_format(f)
            string f : POSIX path of source XML file
        do shell script "/usr/bin/perl -CSDA -w <<'EOF' - " & f's quoted form & "
    use strict;
    use XML::LibXML;
    my $parser = XML::LibXML->new();
    my $doc = $parser->parse_file($ARGV[0]);
    print $doc->find('/Mediainfo/File/track[@type=\"Audio\"]/Format');
    end get_audio_format



    And a littel more general-purpose script is -



    set f to (choose file with prompt "Choose the XML file")'s POSIX path
    get_value_at_xpath(f, "/Mediainfo/File/track[@type=\"Audio\"]/Format")
    on get_value_at_xpath(f, xpath)
            string f : POSIX path of source XML file
            string xpath : XPath of target node
        do shell script "/usr/bin/perl -CSDA -w <<'EOF' - " & f's quoted form & " " & xpath's quoted form & "
    # $ARGV[0] : xml file
    # $ARGV[1] : xpath
    use strict;
    use XML::LibXML;
    my $parser = XML::LibXML->new();
    my $doc = $parser->parse_file($ARGV[0]);
    print $doc->find($ARGV[1]);
    end get_value_at_xpath


    Hope this may help,





    PS. If copied code has extra spaces in front of every line, which appears to be the case with some browsers including Firefox, please remove them before running the script.

  • twtwtw Level 5 Level 5 (4,900 points)

    In applescript, you do it this way:


    tell application "System Events"

      set theXMLFile to XML file "/path/to/xml file.xml"

      tell theXMLFile's XML element "Mediainfo"'s XML element "File"

      set audioTrack to first XML element whose XML attribute "type"'s value is "audio"

      set audioFormat to audioTrack's XML element "Format"'s value

      end tell

    end tell


    no need to mess with unix.

  • MichaelRow12 Level 1 Level 1 (0 points)

    @Tony T1 @Hiroto @twtwtw

    Thanks for the scripts, it works.


    @Frank Caggiano

    Thanks for the tutorial.

  • VikingOSX Level 6 Level 6 (9,360 points)

    Glad this got solved for you in AppleScript. Artful work by twtwtw.


    For mental entertainment, I thought I would solve this in Python, using the standard shipping version (2.7.5) in OS X 10.9.4, and sticking to a standard Python library. I used your XML data in a file called Mediainfo.xml. It uses XPath to search for any tag element that has type=‘Audio’. It then obtains the text of the associated Format tag. The print statement transforms the returned List element to string. Works with Python 2.7.5 thru 3.4.1 without modification.


    import xml.etree.cElementTree as etree
    dom = etree.parse("Mediainfo.xml")
    afmt = [v.text for v in dom.findall(".//*[@type='Audio']Format")]
    print('Audio Format: {:s}'.format(''.join(afmt)))