Giter Site home page Giter Site logo

Comments (2)

ron-fischer-foxvfxlab avatar ron-fischer-foxvfxlab commented on June 19, 2024 1

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))

from pyaaf2.

markreidvfx avatar markreidvfx commented on June 19, 2024

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.

from pyaaf2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.