Is it possible to dynamically spawn functions?
I have written a function that parses this example.xml file and returns the attributes for the segment_1 element as a list. Now this example contains four segments however this will change for different assets, one could only have 1 segment and another could have 10+.
Here is the example XML it has four segments:
<?xml version="1.0" encoding="utf-8"?>
<manifest task_id="00000000112">
<asset_metadata>
<material_id>LB000001</material_id>
<series_title>test asset 1</series_title>
<season_title>Number 1</season_title>
<season_number>1</season_number>
<episode_title>ET 1</episode_title>
<episode_number>1</episode_number>
<start_date>18-11-2016</start_date>
<end_date>30-11-2016</end_date>
<ratings>15</ratings>
<synopsis>This is a test asset</synopsis>
</asset_metadata>
<file_info>
<source_filename>LB000001</source_filename>
<number_of_segments>4</number_of_segments>
<segment_1 seg_1_in="00:00:00.000" seg_1_out="00:01:00.000" seg_1_dur="00:01:00.000"/>
<segment_2 seg_2_in="00:02:00.000" seg_2_out="00:03:00.000" seg_2_dur="00:01:00.000"/>
<segment_3 seg_3_in="00:04:00.000" seg_3_out="00:05:00.000" seg_3_dur="00:01:00.000"/>
<segment_4 seg_4_in="00:06:00.000" seg_4_out="00:07:00.000" seg_4_dur="00:01:00.000"/>
<conform_profile definition="hd" aspect_ratio="16f16">ffmpeg -progress LOG_FILE.txt -i S_PATH/F_NAME.mp4 SEG_CONFORM</conform_profile>
<transcode_profile profile_name="amazon" package_type="tar">ffmpeg -safe 0 -progress LOG_FILE.txt -f concat -i T_PATH/CONFORM_LIST TRC_PATH/F_NAME.mp4</transcode_profile>
<target_path>F:/profiles/amazon</target_path>
</file_info>
</manifest>
I have written a 70 line if statement that will process assets with up to 4 segment's and I can always add to that code for assets that have more segments, however that is not very elegant so I am working on a function to dynamically create the segment lists based on this element in the XML:
<number_of_segments>4</number_of_segments>
Ideally it would generate x amount of:
return seg_element(root, path)
in the parse_xml() function based on how many segments the asset has.
Here are the functions:
import xml.etree.ElementTree as et
file = 'example.xml'
# Seg_element
def seg_element(xml_root, element_path):
list_a = []
for elem in xml_root.iterfind(element_path):
a = elem.attrib
for i in a:
list_a.append([i + ' = ' + a[i]])
return list_a
# Parse_xml
def parse_xml(file_input, number_of_segments):
tree = et.parse(file_input)
root = tree.getroot()
path = 'file_info/segment_1'
return seg_element(root, path)
for i in parse_xml(file, 1):
print(i)
Here is the output:
['seg_1_dur = 00:01:00.000']
['seg_1_in = 00:00:00.000']
['seg_1_out = 00:01:00.000']
Of the name of the list created by each iteration of seg_element() would be different.
The reason for doing this to create an command to submit to FFMPEG, here is the 70 line if statement that creates the command http://pastebin.com/jgawC48Y
There is no need to generate functions, you just want to do a loop over the number of segments, and call the seg_element
function accordingly:
def parse_xml(file_input, number_of_segments):
tree = et.parse(file_input)
root = tree.getroot()
segments_no = int(root.find('file_info/number_of_segments').text)
segments = []
for i in range(segments_no):
path = 'file_info/segment_%d' % (i+1)
segments.append(seg_element(root, path))
return segments
(code above is not tested, but I think you'll get it)