Embed a Timecode in a Video File

Problem

You want to extract a timecode from a EXR image sequences and embed it in a video file.

Solution

import Draft
from DraftParamParser import ReplaceFilenameHashesWithNumber   # Import ReplaceFilenameHashesWithNumber from the DraftParamParser library

# Create a Draft.ImageInfo object used to extract a Draft.Timecode object
imageInfo = Draft.ImageInfo()

# Main encoding loop
for currFrame in range( 1, 200 ):
    currFile = ReplaceFilenameHashesWithNumber( 'movie_frame####.exr', currFrame )
    frame = Draft.Image.ReadFromFile( currFile, imageInfo )

    # If first frame and a timecode is found, specify the timecode parameter when creating the encoder
    if currFrame == 1:
        if( imageInfo.timecode ):
            encoder = Draft.VideoEncoder( '//path/to/video/save.mov', timecode = imageInfo.timecode )
        else:
            encoder = Draft.VideoEncoder( '//path/to/video/save.mov' )

    encoder.EncodeNextFrame( frame )

encoder.FinalizeEncoding()

Discussion

In order to embed a timecode in a video file, you can extract the timecode embedded in the first frame of an image sequence. To do so, you first need to check if there’s one. If a Timecode is set in imageInfo object, the if clause will execute and we’ll initialize the video encoder and embed the first frame’s timecode using:

encoder = Draft.VideoEncoder( '//path/to/video/save.mov', timecode = imageInfo.timecode )

If no timecode is found, we’ll have:

imageInfo.timecode = None

and the else clause will execute, initializing the encoder using:

encoder = Draft.VideoEncoder( '//path/to/video/save.mov' )

The remaining encoding steps are the same as usual, finishing with the line:

encoder.FinalizeEncoding()

to finalize and save the resulting video to file.

See Also

You can consult the sections Timecode in Concepts and Embed a Timecode in an Image File in the Basic Cookbook.