Reputation: 135
There's an XML file generated video editing software, which contains all the data for clip exhange. The XML is valid for the software. The only thing it lacks is an element for fielddominance (I need to set 'upper' value there) The hard part for me that the file structure. Here's like this looks like:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xmeml>
<xmeml version="5">
<sequence>
<name>to_color (Resolve)</name>
<duration>121597</duration>
<rate>
<!-- ... -->
</rate>
<in>-1</in>
<out>-1</out>
<timecode>
<!-- ... -->
</timecode>
<media>
<video>
<track>
<clipitem id="983_0121_01_1_5FF70094C4A64669BC77.mov 0">
<name>983_0121_01_1_5FF70094C4A64669BC77.mov</name>
<duration>271</duration>
<rate>
<timebase>25</timebase>
<ntsc>false</ntsc>
</rate>
<start>0</start>
<end>221</end>
<enabled>true</enabled>
<in>25</in>
<out>246</out>
<file id="983_0121_01_1_5FF70094C4A64669BC77.mov 2">
<duration>271</duration>
<rate>
<!-- ... -->
</rate>
<name>983_0121_01_1_5FF70094C4A64669BC77.mov</name>
<pathurl>file://Capture2/Capture2/SHARED/DAVINCI_Render/111.mxf</pathurl>
<timecode>
<!-- ... -->
</timecode>
<media>
<video>
<duration>271</duration>
<samplecharacteristics>
<width>1920</width>
<height>1080</height>
</samplecharacteristics>
</video>
</media>
</file>
<compositemode>normal</compositemode>
<filter>
<enabled>true</enabled>
<start>0</start>
<end>271</end>
<effect>
<name>Opacity</name>
<effectid>opacity</effectid>
<effecttype>motion</effecttype>
<mediatype>video</mediatype>
<effectcategory>motion</effectcategory>
<parameter>
<name>opacity</name>
<parameterid>opacity</parameterid>
<value>100</value>
<valuemin>0</valuemin>
<valuemax>100</valuemax>
</parameter>
</effect>
</filter>
</clipitem>
</track>
<format>
<samplecharacteristics>
<!-- this is what I need to add START -->
<fielddominance>upper</fielddominance>
<!-- this is what I need to add END-->
<width>1920</width>
<height>1080</height>
<pixelaspectratio>square</pixelaspectratio>
<rate>
<!-- ... -->
</rate>
<codec>
<!-- ... -->
</codec>
</samplecharacteristics>
</format>
</video>
<audio>
<track>
<!-- ... -->
</track>
</audio>
</media>
</sequence>
</xmeml>
The element I need to add is in sequence - media - video - format - samplecharacteristics
field.
But this tag also exists in sequence - media - video - track - clipitem - file - media - video - samplecharacteristics
I use xml.etree.ElementTree as a parser. Here's what I am trying, and of course I'm doing it all wrong:
import xml.etree.ElementTree as ET
tree = ET.parse(r'\\capture2\11\in.xml')
root = tree.getroot()
fieldd = ET.Element('fielddominance')
fieldd.set('field','upper')
for tag in root.iter('samplecharacteristics'):
tag.append(fieldd)
output_file = r'\\capture2\11\new.xml'
with open(output_file, 'wb') as out:
tree.write(out, encoding='utf-8')
This adds fielddominance to every samplecharacteristics tag. And I have no idea how to set 'upper' value inside tag, not as attribute.
What I would like is to write <fielddominance>upper</fielddominance>
only to format
tag, but omit <track>
tags
Upvotes: 0
Views: 311
Reputation: 168626
You can use ElementTree's limited XPath support to isolate the samplecharacteristics
you want to modify. Also, setting the text of an element is done simply by setting its .text
attribute.
Try this:
import xml.etree.ElementTree as ET
tree = ET.parse('in.xml')
fieldd = ET.Element('fielddominance')
fieldd.text = 'upper'
for tag in tree.findall("./sequence/media/video/format/samplecharacteristics"):
tag.append(fieldd)
tree.write('new.xml', "UTF-8", True)
Upvotes: 1