Sunday, January 15, 2012

Weave All Wobbles

Back in July, I wrote about some of the techniques I used to simulate old 16mm film entirely using free software.

One technique I mentioned was using Kdenlive to simulate gate weave – that strangely pleasing effect whereby picture moves almost imperceptibly around the screen as you watch. If you’re not familiar with what gate weave looks like, here’s an example:


I mentioned in my previous article that I discovered I could simulate gate weave manually using the Kdenlive “Pan and Zoom” filter.

I did this by zooming in on my video slightly…

Video resized by 108% to zoom in on it slightly

…and then moving the picture around randomly at 5 frame intervals.

Keyframes added a five frame intervals; X and Y changed randomly

Once this is done, I could save this portion gate weave as a custom effect so I could re-use it:

Save effect button in Kdenlive
 
When you do this, your zoom, keyframes and random movements are stored in the ~/.kde/share/apps/kdenlive/effects folder as an XML file. The XML file created by Kdenlive for some manually created gate weave looks like this:
<effect tag="affine" type="custom" id="test">
<name>test</name>
<description>Adjust size and position of clip</description>
<author>Charles Yates</author>
 <parameter opacity="false" 
    default="0%,0%:100%x100%" 
    type="geometry" 
    value="-29,-23:778x622:100;
      25=-30,-25:778x622:100;
      50=-29,-22:778x622:100;
      75=-31,-24:778x622:100;
      100=-28,-24:778x622:100;
      123=-31,-21:778x622:100" 
    name="transition.geometry">
  <name>Rectangle</name>
 </parameter>
</effect>
Obviously, creating gate weave by hand for a long piece of video using the Kdenlive interface would take hours. Luckily, because the resulting gate weave custom effect is stored as a simple XML file, you can write a quick script to create the gate weave instead.

So I thought I'd use this post show you the script I use to create “automatic” gate weave.

When I do scripting jobs I prefer to use Python if possible. For this task I needed to get it to write out an XML file. Python comes with a selection of  complex but hugely flexible ways to do this. However, to make this as quick and easy as possible, I used a lovely Python module called pyfo which was developed by Luke Arno and did everything I needed.

Although you can install pyfo if you want to, there’s no need; you can just extract the pyfo.py file and put it in the same folder as your Python scripts that use it.

As you can see below, my Python script is pretty self-explanatory. The script below is suitable for adding gate weave to PAL 4:3 video.

The variable value_string determines how much the video is zoomed in by initially. For wide-screen PAL 16:9 video I adjust this value to "-29,-23:1034x622:100;". The value of the step_value variable determines how often the video frame moves. I think every 5 frames often works best.

You can run the script multiple times to create different xml files, but if you do remember that you will need to change the value of the custom_effect_name variable each time to something different so you’ll be able to tell your gate weave custom effects apart.

#!/usr/bin/env python2.4

""" This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see http://www.gnu.org/licenses/."""

import random
from pyfo import pyfo

custom_effect_name = "weave"

#Number of seconds of gate weave required
frames_required = 750

#Initial value of frame
value_string = "-29,-23:778x622:100;"

#Origin
origin = {'x':-29, 'y':-23}

#Step value (in frames)
step_value = 5

#Maximum weave distances
max_distance = {'x': 1, 'y': 1}

for i in range(0, frames_required, step_value):
    x = random.randrange(origin['x'] - max_distance['x'],
            origin['x'] + max_distance['x'] + 1)
    y = random.randrange(origin['y'] - max_distance['y'], 
            origin['y'] + max_distance['y'] + 1)
    value_string += str(i) + "=" + str(x) + "," + str(y) + ":778x622:100;"

xml_output = \
    ('effect', [
    ('name', custom_effect_name),
    ('description', 'Adjust size and position of clip'),
    ('author', 'Charles Yates'),
    ('parameter', ('name', 'Rectangle'),
    {'opacity':'false', 'default':'0%,0%:100%x100%', 
    'type':'geometry', 'value':value_string, 'name':'transition.geometry'}),
    ], {'id':custom_effect_name, 'type':'custom', 'tag':'affine'})

result = pyfo(xml_output, pretty=True, prolog=False, encoding='ascii')
print result.encode('ascii', 'xmlcharrefreplace')

As it is, my Python script writes the XML file it produces to the console window. You can copy and paste the resulting XML into a blank text file and save it to ~/.kde/share/apps/kdenlive/effects folder,

However, you can pipe the output into straight into an XML file instead. For instance:
$ python gateweave.py > gateweave.xml
Obviously, my Gate Weave solution isn’t very elegant, but who cares – it works, and it’s all free software!

No comments: