Sunday, April 11, 2010

Using bash to manually double the length of a (time lapse) image sequence

This small program came to be after not being able to render videos at 15fps in Adobe Premiere Pro CS4 with an MPEG2 codec. I reckoned that if I simply doubled the time the files would be displayed, in this case, using each frame twice in the sequence, I could achieve this effect. After deciding that a windows batch script would take too much time to code, I fell back to the shell language, Bash.

Input files: (30fps)
images000001.jpg (Frame 1)
images000002.jpg (Frame 2)
images000003.jpg (Frame 3)
...
images001913.jpg (Frame 4)

Resulting files: (15fps)
new_images000001.jpg (Frame 1)
new_images000002.jpg (Frame 1)
new_images000003.jpg (Frame 2)
new_images000004.jpg (Frame 2)
...
new_images003825.jpg (Frame 1913)
new_images003826.jpg (Frame 1913)


#!/bin/bash

for filename in ./images*
do
num1=${filename:8:5}
prefix=new_images
suffix=.jpg
num2="$(echo $num1 | sed 's/0*//')"

finnum=0
cpynum=0

finnum=$[ ($num2 * 2) - 1 ]
cpynum=$[ $finnum + 1 ]

#echo $filename "=>" $prefix$finnum$suffix
#echo " =>" $prefix$cpynum$suffix

mv $filename $prefix$finnum$suffix
cp $prefix$finnum$suffix $prefix$cpynum$suffix
done;