views:

39

answers:

3

I have a directory with a set of YYYY-MM-DD-dated files in as so:

pictures/
    2010-08-14.png
    2010-08-17.png
    2010-08-18.png

How can I use Python GStreamer to turn these files into a video? The filenames must remain the same.

I have a program that can turn incrementally numbered PNGs into a video, I just need to adapt it to use dated files instead.

A: 

Hi

The easiest would be to create link/rename those file to a sequence number (that should be easily doable with a n=0 for f in $(ls * | sort); do ln -s $f $n && $n=$((n+1))

Then you should be able to do:

gst-launch multifilesrc location=%d ! pngdec ! theoraenc ! oggmux ! filesink location=movie.ogg

It would have more sense to use a different encoder than theora perhaps, to have all pictures as keyframe, perhaps with MJPEG?

elmarco
The theory's helpful but it looks like the code's in Bash/Sh.
Josh
do you have trouble converting the given pipeline to a python program?
elmarco
@elmarco: Nope, I've already done it. Thanks for the code anyway =)
Josh
yw, glad you managed :)
elmarco
A: 

It's easy enough to sort the filenames by date:

import datetime, os

def key( filename ):
    return datetime.datetime.strptime( 
        filename.rsplit( ".", 1 )[ 0 ], 
        "%Y-%m-%d"
    )

foo = sorted( os.listdir( ... ), key = key )

Maybe you want to rename them?

count = 0
def renamer( name ):
    os.rename( name, "{0}.png".format( count ) )
    count += 1

map( renamer, foo )
katrielalex
A: 

Based on the Bash code elmarco posted, here's some basic Python code that will symlink the dated files to sequentially numbered ones in a temporary directory:

# Untested example code. #

import os tempfile shutil

# Make a temporary directory: `temp`:
temp = tempfile.mkdtemp()  

# List photos:
files = os.listdir(os.path.expanduser('~/.photostory/photos/'))

# Sort photos (by date):
files.sort()

# Symlink photos to `temp`:
for i in range(len(files)):
    os.symlink(files[i], os.path.join(temp, str(i)+'.png')  

# Perform GStreamer operations on `temp`. #

# Remove `temp`:
shutil.rmtree(temp)
Josh