Source code for cgl.plugins.otio.tools.fcp.fcp_extract_scenes
import os
import pathlib
import argparse
import opentimelineio as otio
[docs]
def extract_scenes(src_xml, output_dir):
xml_data = None
with open(src_xml, 'r', encoding='utf8') as f:
xml_data = f.read()
assert xml_data
timeline = otio.adapters.read_from_string(xml_data, 'fcp_xml_nested')
video_tracks = timeline.video_tracks()
scenes = {}
for track in video_tracks:
for item in track.find_children():
if isinstance(item, otio.schema.Stack):
if item.name and item.name.startswith("Scene "):
clip_id = item.metadata.get("fcp_xml", {}).get("@id", None)
clip_name = item.name
new_timeline = otio.schema.Timeline()
new_timeline.name = clip_name
for t in item:
new_timeline.tracks.append(t.deepcopy())
serialize = otio.adapters.write_to_string(new_timeline)
if clip_name in scenes:
if scenes[clip_name] != serialize:
alt_name = f"{clip_name}-{clip_id}"
scenes[alt_name] = serialize
print(f"found: {alt_name}")
else:
print(f"found: {clip_name}")
scenes[clip_name] = serialize
result = {}
for name, data in scenes.items():
output_file = os.path.join(output_dir, name + '.otio')
# print(f"{name} {output_file}")
with open(output_file, 'w', encoding='utf8') as f:
f.write(data)
result[name] = output_file
return result
[docs]
def run_cli():
parser = argparse.ArgumentParser(
prog = 'extract_fcp_scenes')
parser.add_argument('source_xml', type=pathlib.Path)
parser.add_argument('output_dir', type=pathlib.Path)
args = parser.parse_args()
assert os.path.exists(args.source_xml)
assert os.path.isdir(args.output_dir)
print(f"reading {args.source_xml}")
extract_scenes((args.source_xml), str(args.output_dir))
if __name__ == "__main__":
run_cli()