3
$\begingroup$

I am trying to do some video editing using blender in headless mode as a python module. My first problem was that when I tried to import a video using movie_strip_add it always failed even when I tried multiple formats. I tried importing an image and that works but when I go to check the length of the strip or even find the added strip I get array out of bounds errors. Does anyone have an example of how to do this.

Second question is there anything special I have to do to add movie strips

def __init__(self):
    self.numRunning += 1
    self.scene = bpy.context.scene
    self.ops = bpy.ops
    self.data = bpy.data.scenes

def compress(self,filename):
    newname = os.path.splitext(filename)[0] + 'Compressed'
    (filepath,vidname)=os.path.split(filename)

    self.scene.camera = None
    for obj in self.scene.objects:
        self.scene.objects.unlink(obj)


    self.scene.sequence_editor_create()
    self.ops.scene.new(type='NEW')
    bpy.data.scenes['Scene'].render.resolution_x=1280

    file=[]
    #for i in vidname:
    frame={"name":vidname}
    file.append(frame)
    scene_name=self.data[0].name

    self.ops.sequencer.image_strip_add(directory = filepath, files = file, channel =1, frame_start = 0, filemode = 1)
    #self.ops.sequencer.movie_strip_add(filepath = filepath,files = file,frame_start=0, channel=1)

    stripname=file[0].get("name")
    print(self.scene.frame_end)
    print(self.data[scene_name].sequence_editor.sequences[stripname].frame_end)
$\endgroup$
3
$\begingroup$

Blender uses ffmpeg to handle video formats, you can check that it was built with ffmpeg enabled by entering getattr(bpy.app.build_options,'codec_ffmpeg') into blender's Python console.

For your script --

self.scene.camera = None
for obj in self.scene.objects:
    self.scene.objects.unlink(obj)

This will remove all objects from the current scene yet you follow it with creating a new scene -

self.scene.sequence_editor_create()
self.ops.scene.new(type='NEW')
bpy.data.scenes['Scene'].render.resolution_x=1280

You create a new scene and set the resolution on the scene called 'Scene' - as that is the name of the first scene in blender this won't be the name of the scene you just created. By creating a new scene you will have a scene without objects - sounds better than the previous step of unlinking everything.

Try

myScene = bpy.data.scenes.new('myscene')
myScene.render.resolution_x = 1280

Operators depend on the context to perform tasks, normally this includes the current window, which you aren't going to have if you are running the script headless.

Instead of using operators -

myScene.sequence_editor_create()

myImageStrip = myScene.sequence_editor.sequences.new_image(name = 'imgStrip', filepath = '/tmp/image001.png', channel = 1, frame_start = 1)
myImageStrip.frame_final_duration = 5

myMovieStrip = myScene.sequence_editor.sequences.new_movie(name = 'firstClip', filepath = '/tmp/test.mp4', channel = 2, frame_start = 1)
$\endgroup$

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.