WyattBlue / auto-editor

Auto-Editor: Efficient media analysis and rendering
https://auto-editor.com
The Unlicense
2.82k stars 413 forks source link

Using OpenTimelineIO for exporting to XML #55

Closed ethan-ou closed 4 years ago

ethan-ou commented 4 years ago

After taking a look at exporting from Premiere to XML myself, I began using this library called OpenTimelineIO, which was started by engineers at Pixar. The original purpose of the library is to create an open standard of video timelines so that it simplifies the difficulty of importing between video editors, but it's also useful just for converting between video editing formats.

In OpenTimelineIO, you can make the whole timeline programmatically. You create a Timeline object, then you add the clips to the timeline. Once you've created the timeline, you can export to different formats.

There's however a couple downsides I found from using it:

I'm using it myself in my own auto-editing tool, so I can share some code samples on how to make timelines that can be imported by Premiere.

WyattBlue commented 4 years ago

How do you generate new files in this program? I couldn't figure it out by reading the docs.

ethan-ou commented 4 years ago

It's pretty complicated because you need to make a whole timeline from scratch. Here's a small section of code I wrote to make a timeline:

import opentimelineio as otio

# First create a timeline
timeline = otio.schema.Timeline('Project 1')

# Then, add tracks to the timeline
video_track_1 = otio.schema.Track(kind=otio.schema.track.TrackKind.Video, name="V1")
audio_track_1 = otio.schema.Track(kind=otio.schema.track.TrackKind.Audio, name="A1")

timeline.tracks.extend([video_track_1])
timeline.tracks.append(audio_track_1)

# Create a video/audio clip object. This is like importing a video or audio file into Premiere.
# This video clip is referenced by the edits that are created below.
video_clip_1 = otio.schema.ExternalReference(
  target_url="C:/C0001.mp4",
  available_range=otio.opentime.TimeRange(
                start_time=otio.opentime.RationalTime(value=0, rate=25), 
                duration=otio.opentime.RationalTime(value=1000, rate=25)
            )
)

# Create edits, adding the time where the cut begins and its duration.
# OpenTimelineIO supports frame-based edits such as those below, and also edits based on seconds.
video_cut_1 = otio.schema.Clip(
    name='C0001',
    media_reference=video_clip_1, # The media reference you just created.
    source_range=otio.opentime.TimeRange(
        start_time=otio.opentime.RationalTime(value=112, rate=25), # Start of cut
        duration=otio.opentime.RationalTime(value=40, rate=25) # End of cut
    ) 
)

video_cut_2 = otio.schema.Clip(
    name='C0001',
    media_reference=video_clip_1,
    source_range=otio.opentime.TimeRange(
        start_time=otio.opentime.RationalTime(value=112, rate=25),
        duration=otio.opentime.RationalTime(value=40, rate=25)
    )
)

# Do the same thing for audio edits. The video and audio tracks need to be handled separately right now.
audio_cut_1 = otio.schema.Clip(
    name='C0001',
    media_reference=video_clip_1,
    source_range=otio.opentime.TimeRange(
        start_time=otio.opentime.RationalTime(value=112, rate=25),
        duration=otio.opentime.RationalTime(value=40, rate=25)
    )
  )

audio_cut_2 = otio.schema.Clip(
    name='C0001',
    media_reference=video_clip_1,
    source_range=otio.opentime.TimeRange(
        start_time=otio.opentime.RationalTime(value=112, rate=25),
        duration=otio.opentime.RationalTime(value=40, rate=25)
    ), 
  )

# Add the cuts to the audio and video tracks.
video_track_1.extend([
  video_cut_1,
  video_cut_2
])

audio_track_1.extend([audio_cut_1, audio_cut_2])

# Export the files to XML:
result = otio.adapters.write_to_string(
            timeline,
            "fcp_xml" # You can also put in "cmx_3600" for edl files, and a bunch of other filetypes too.
        )

print(result)

This example was simplified so that the main concepts are clear, however for things to work in Premiere you also need extra metadata to specify things like the dimensions and frame rate of the sequence, and metadata about video clips. For that, take a look at this colab: https://colab.research.google.com/drive/1iEtx736YPneU7wckPbf-krydmEGylYds?usp=sharing

WyattBlue commented 4 years ago

This is a good suggestion but I don't think OpenTimelineIO will help achieve our goal of providing a cross-platform solution for creating xmls files. Therefore, I am closing this issue. This method stills needs different exceptions for different editors so it's basically the same as what is currently implemented, just with less characters. I believe having a generic xml writer module would be better than this program if file size ever became a concern.