2
$\begingroup$

Trying to get to grips with the abilities of blender when used purely as a python library (without any GUI interactions). (Forgive my lack of knowledge and misuse of words)

The code examples and documentation I am finding seem geared towards scripts to apply when content is already present within the blender GUI. (e.g. Extract sequence of frames from imported MP4 - scripted?)

Does an example exist in documentation for loading external files into a blender virtual stage? e.g. 1 picture node and 1 video node and (secondarily) exporting the rendered output (e.g. video with an image overlaid) as an image sequence.

I've been looking through https://www.blender.org/api/blender_python_api_2_72_release/info_quickstart.html and http://wiki.blender.org/index.php/Dev:Py/Scripts but I haven't been able so far to find a good starting point for my knowledge level.

$\endgroup$
  • $\begingroup$ For a simple conversion from video files to image sequences consider using ffmpeg. $\endgroup$ – cegaton Feb 9 '16 at 16:52
2
$\begingroup$

This script accepts the movie file and output directory as command line arguments, then loads the video file as a video sequence editor (VSE) clip and renders it out as frames (by default as PNG, change if needed).

It automatically sets the render resolution and frame duration according to the video file.

import bpy, sys

# Take movie clip filepath and output directory as command line arguments
movieFilePath, OutputPath = sys.argv[-2:]

S  = bpy.context.scene
mc = bpy.data.movieclips.load( movieFilePath )

# Set render resolution to match movie clip
S.render.resolution_x          = mc.size[0]
S.render.resolution_y          = mc.size[1]
S.render.resolution_percentage = 100

# Set number of frames to match movie clip length
S.frame_start = 0
S.frame_end   = mc.frame_duration

# Set render format and output folder
S.render.image_settings.file_format = 'PNG'
S.render.filepath                   = OutputPath

# Load clip to sequencer
se = bpy.context.scene.sequence_editor_create()
se.sequences.new_clip( "MyClip", mc, 0, 0 )

# Render animation to create image sequence
bpy.ops.render.render( animation = True, write_still = True )

To use it, save the script file, then run blender from the command line and provide the filepath and output directory as arguments:

blender -b -P movie_to_images.py -- C:/MyMovie.avi C:/OutputFolder/
$\endgroup$
  • $\begingroup$ How would I go about overlaying the current frame number onto the sequence? (apologies if I shouldn't be asking follow up questions in comments) $\endgroup$ – OhNoez Woez Feb 9 '16 at 12:41
  • $\begingroup$ In blender 2.76 there are new "text" effect strips in the Video Sequence Editor. You can add these for each frame with the frame number as the text. You can see good examples of this in use in the textSync addon: blendernation.com/2016/02/04/… $\endgroup$ – TLousky Feb 9 '16 at 12:58
  • $\begingroup$ do the command line arguments expect a blend file? Should I be using a different blender executable? This is the output ``` blender -b test.py Sockets.mp4 ~/out found bundled python: /Applications/blender.app/Contents/MacOS/../Resources/2.76/python read blend: .../test.py Error: Failed to read blend file '.../test.py', not a blend file Blender quit ` $\endgroup$ – OhNoez Woez Feb 9 '16 at 16:08
  • $\begingroup$ Yeah, and another parameter. Forgot! Edited answer to reflect the necessary changes. $\endgroup$ – TLousky Feb 9 '16 at 16:08
  • $\begingroup$ Does the blend file need to contain anything? $\endgroup$ – OhNoez Woez Feb 9 '16 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.