Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation on how to access the timeline #14

Closed
kevkelsey opened this issue Sep 10, 2018 · 2 comments
Closed

Documentation on how to access the timeline #14

kevkelsey opened this issue Sep 10, 2018 · 2 comments

Comments

@kevkelsey
Copy link

I'm trying to iterate over the timeline and get all the start and end timecodes of every clip in the sequence. Is this achievable with this package and if so, how would that be accomplished? Thank you!

@markreidvfx
Copy link
Owner

Its definitely possible. Here is a rough example of how to get the start timecode of source clip in pyaaf1. It should be fairly similar to do it pyaaf2

markreidvfx/pyaaf#44 (comment)

I'll try and port a example to pyaaf2 when I get a chance.

@ron-fischer-foxvfxlab
Copy link

ron-fischer-foxvfxlab commented Jan 26, 2019

A quick translate of the older code is below.

With my example AAF (glad to send)
SourceClip.walk() dead ends in pyaaf2 components line 149 trying to call Sequence.component_at_time

So... I did a little workaround below replacing the walk and sequence_component_at_time methods.
Also assume encounting a TimelineMobSlot at top and grabbed the edit_rate from it. That's NOT the right way to do it though. The editrate should come from the mob at the end of the chain.

Hope that's useful!

import aaf2
import sys
import aaf2.components


def frames_to_timecode(frames, fps):
    s, f = divmod(frames, fps)
    m, s = divmod(s, 60)
    h, m = divmod(m, 60)
    d, h = divmod(h, 24)
    return '{0:02d}:{1:02d}:{2:02d}:{3:02d}:{4:02d}'.format(d, h, m, s, f)


def mob_timecode(mob, offset, edit_rate):
    timecode_list = []
    # search the slots for timecode segments
    # NOTE: PhysicalTrackNumber of the slots tells you the type of
    # timecode is its main, AUX etc, see aafeditprotocol spec
    for slot in mob.slots:
        if slot.segment.media_kind == 'Timecode':
            if len(slot.segment.components) == 1:
                timecode = slot.segment.components[0]

                if isinstance(timecode, aaf2.components.Timecode):
                    tc = frames_to_timecode(timecode['Start'].value + offset, edit_rate)
                    timecode_list.append(tc)
            else:
                # If the timecode track has a sequence of Timecode objects,
                # you calculate the timecode by finding the Timecode object
                # that covers the specified offset in the track and add to
                # its starting timecode the difference between the specified
                # offset and the starting position of the Timecode object in the track.
                for index, position, component in slot.segment.positions():
                    if isinstance(component, aaf2.components.Timecode):
                        if position + component.length > offset:
                            diff = offset - position
                            tc = frames_to_timecode(diff + component['Start'].value, edit_rate)
                            timecode_list.append(tc)
                            break

    return timecode_list


def sequence_component_at_time(segment, start):
    if len(segment.components) == 1:
        # handle the easy case (might still be wrong)
        return segment.components[0]
    else:
        # anticipated functionality ;-)
        return segment.component_at_time(start)


def walker(self):
    if not self.slot:
        return

    segment = self.slot.segment

    if isinstance(segment, aaf2.components.SourceClip):
        yield segment
        for item in walker(segment):
            yield item

    elif isinstance(segment, aaf2.components.Sequence):
        try:
            clip = sequence_component_at_time(segment, self.start)
        except AttributeError as e:
            print(e)
        else:
            if isinstance(clip, aaf2.components.SourceClip):
                yield clip
                for item in walker(clip):
                    yield item
            else:
                raise NotImplementedError("Sequence returned {} not "
                                          "implemented".format(
                    type(segment)))

    elif isinstance(segment, aaf2.components.EssenceGroup):
        yield segment

    elif isinstance(segment, aaf2.components.Filler):
        yield segment
    else:
        raise NotImplementedError("Walking {} not implemented".format(
            type(segment)))


def get_timecode(slot):
    edit_rate = int(slot.edit_rate)

    if isinstance(slot.segment, aaf2.components.Sequence):
        clip = slot.segment.components[0]
    else:
        clip = slot.segment

    mobs = [clip.mob]
    start = clip.start
    timecode_list = []

    # walk down the reference chain until you reach the
    # last clip with a NULL reference
    for c in walker(clip):
        print 'walking', c.name, c.start
        start += c.start
        if c.mob:
            mobs.append(c.mob)

    # the last mob in the chain has the timecode
    timecode_list.extend(mob_timecode(mobs[-1], start, edit_rate))

    return timecode_list


if __name__ == "__main__":
    #f = aaf2.open(sys.argv[1])
    f = aaf2.open('..\\..\\..\\tests\\data\\ml603_0p.aaf')

    for mob in f.content.mastermobs():
        print mob.name
        print "  ", get_timecode(mob.slot_at(1))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants