# Extract sequence of frames from imported MP4 - scripted?

I would like to extract a sequence of images files from an imported mp4, ideally using a script. I'll then analyze the images with python to start extracting cues and other information, so they don't necessarily need to be absolute best possible quality.

For example:

frame_numbers = range(15*60*24, 30*60*24, 96)


returns 225 frame numbers between 15:00 and 30:00 at 24fps.

Can this be scripted?

If not, what is the quickest way to do this manually? (ouch!)

You can do this using the compositor.

All you need is to prepare a simple node setup where you have a Movie Clip (Input) Node connected to the Composite (Output) Node. Load your video into the movie clip node, set a few small params in the script below (namely the output folder where you'd like to save the extracted frames and the frame range), and run the script.

The script will copy the video dimensions and set up your render settings accordingly (but will not alter the output file format).

import bpy
from os.path import join

S = bpy.context.scene
n = S.node_tree

vidNode = n.nodes['Movie Clip']

# Set Render Settings dimensions according to video
S.render.resolution_percentage = 100
S.render.resolution_y          = vidNode.clip.size[1]
S.render.resolution_x          = vidNode.clip.size[0]

# Render desired frame numbers ( UPDATE THESE AS NEEDED )
start = 5
end   = 200
step  = 3
frame_numbers = range(start, end, step)

outputFolder = "C:/tmp/test"    # <=== Update this
for f in frame_numbers:
S.frame_set( f )
fileName = str(f) + S.render.file_extension
S.render.filepath = join( outputFolder, fileName )
bpy.ops.render.render( write_still = True )


Copy this to a new text file in the text editor, change the values in the outputFolder variable and the start, end and step for generating frame_numbers, and press "Run Script".

If you'd like, you can also automate the creation of the node setup and load the video to the movie clip node; there are several nice answers here in StackExchange showing how to create and manipulate nodes via python.

• Sweet! I'll try it out in a few hours. I'm curious - does "movie clip" mean it is assumed to be short? My mp4 file is 500MB and about 1 hour long. (nice rug btw) – uhoh Dec 23 '15 at 8:23
• Movie clip is the term dedicated to any video files (as opposed to image sequences, the other, perhaps more common node for manipulating a sequence of frames). – TLousky Dec 23 '15 at 8:28
• No problem, glad it works :) – TLousky Dec 23 '15 at 11:27
• @uhoh - the dimensions prop was the wrong one. I fixed the script to read from the Movie Clip Node's clip.size property, which provides the right dimensions, and even doesn't require flipping x and y :) – TLousky Dec 23 '15 at 15:17
• Yipeee!! It virtually works right out of the box! Thanks again @TLousky for an excellent and complete answer. I'm a happy camper. – uhoh Dec 23 '15 at 15:36