# Create a QuickTime Movie¶

## Problem¶

You have 200 frames of Patches playing with his ball, and you wish to combine them into a QuickTime movie. The individual frames are named using the format Patches_ball_###.jpg, where ### is a three digit frame number in the range 001 to 200.

## Solution¶

import Draft
from DraftParamParser import ReplaceFilenameHashesWithNumber

encoder = Draft.VideoEncoder( 'Patches.mov' )   # Initialize the video encoder.

for currFrame in range( 1, 201 ):
currFile = ReplaceFilenameHashesWithNumber( 'Patches_ball_###.jpg', currFrame )
encoder.EncodeNextFrame( frame )    # Add each frame to the video.

encoder.FinalizeEncoding()    # Finalize and save the resulting video.


## Discussion¶

In order to create a QuickTime movie, we need a video encoder. The line:

encoder = Draft.VideoEncoder( 'Patches.mov' )


creates a video encoder using default values for most parameters, and ‘Patches.mov’ specifies what filename the movie will be saved to. The optional parameters include, among others, fps, codec, and quality. The frames per second, fps, defaults to 24, the codec defaults to ‘MJPEG’, and the quality defaults to 85. If you wish to use values other than the defaults, simply use in the parameters. For example, we can increase the frame rate to 30, and decrease the quality to 70, using:

encoder = Draft.VideoEncoder( 'Patches.mov', fps=30, quality=70 )


The fps frame rate can be specified as an integer, floating point number, or Fraction. quality can be set to any integer in the range of 0 to 100, where higher numbers represent higher quality. (Note that you cannot simultaneously set both quality and kbitRate, another optional parameter.) In addition to ‘MJPEG’, valid codec values include ‘MPEG4’, ‘H264’, ‘RAWVIDEO’.

Once the encoder is initialized, we can add our frames. Since there are many frames, we use a loop plus some logic to read in all of the frames one at a time. The function ReplaceFilenameHashesWithNumber() from the DraftParamParser library takes a filename pattern and replaces hash symbols with the frame number, padded with zeros to the appropriate number of digits indicated by the number of hash symbols. If no hash symbols are present, ReplaceFilenameHashesWithNumber() will insert the frame number before the file extension (including period). We then use the constructed file name to read in the frame, and add it to the video using:

encoder.EncodeNextFrame( frame )


You may wonder why we used range( 1, 201 ) in our for loop over the frames. This is because the second parameter to range specifies one past the end, not the actual last number. Since we want to include frame 200, we have to specify that 201 is where the range stops.

Once we have added all of our frames, we finalize the encoding, which finishes any remaining processing and saves the video to the file we specified previously. No parameters are required for the finalization:

encoder.FinalizeEncoding()


For more information on the import statement, ReadFromFile(), and WriteToFile(), see the Creating an Image section of this Cookbook
For more information on additional optional parameters for the video encoder, see Draft.VideoEncoder.
More information on Fraction is available in the Python Documentation.