User:Lucia Dossin/Protyping/Assignment 3/Image Sequence

From XPUB & Lens-Based wiki

Modifications on the code uploaded to the Assignment page:

1.Instead of making the line move each 10 pixels, I used an interval of 320/25, so that the line would reach the right margin of the file at the last frame (well, I just saw it actually doesn't).

2.Added colors to the portion of the file on the left side of the line. In the old version of the code, each line would contain N black pixels, 1 white pixel and Z more black pixels. In the new version, N random color pixels, 1 white pixel, Z black pixels. This has increased both the render time and and file size tremendously (from 18Kb to 1.2Mb).

The code:

import struct, sys, random
 
width = 320
height = 240
header = struct.pack("<BBBHHBHHHHBB",0,0,2,0,0,8,0,0,width,height,32,1<<5)

 
totalframes = 25
for frame in xrange(totalframes):
    out = open("colors/frame_%02d.tga" % frame, "wb")
    out.write(header)
 
    for y in xrange(height):
        for x in xrange(width):
            if ( x == frame*(320/25)):
                r = 255
                g = 255
                b = 255
                a = 255
            else:
                if (x < frame *(320/25)):
                    r = random.randint(0, 255)
                    g = random.randint(0, 255)
                    b = random.randint(0, 255)
                    a = 255
                else:
                    r = 0
                    g = 0
                    b = 0
                    a = 255
            out.write(struct.pack('B', b))
            out.write(struct.pack('B', g))
            out.write(struct.pack('B', r))
            out.write(struct.pack('B', a))
 
    out.close()

Colors lucia.gif

Then, by using ffmpeg I combined the same Image sequence with one of the python generated sound files into a mp4.

The command that worked is this one:

ffmpeg -loop 1 -i frame_%02d.tga -i truck.wav -vcodec mjpeg -acodec aac -strict experimental -b:a 192k -shortest output.mp4


The initial command had less parameters but I ended up getting errors (mostly related to wrong audio codec, which made no sense). I was able to make a video either with the images or with the audio - but not both combined. At some point, it seemed that there should be some kind of 'incompatibility' between the files durations.

Timo has helped me find the parameters that were missing (-loop 1 -shortest and the audio bitrate -b:a 192k). I still couldn't check each one of them separately to be sure of the role of each one of them in this command.

The wiki does not allow mp4 upload - I just tried outputting another video format, but then I get codec errors again... More search is needed. So, no output for now. :) The code above does work though, it just does not output in a format that the wiki accepts.