Does anyone here have a wxPython code that converts a folder of images to a .wmv file just by clicking a button?

What I did in my program was I load an Image-to-Video Converter Software by clicking a button but I feel that it would be better if I can directly convert the images to a video just by clicking the button.

Hope someone can help me out here. Thanks so much.

-Karen

Interesting concept. Two potential problems:

(1) What if the images are not all the same size?

(2) How does the code decide what framerate to use?

Jeff

Hi Jeff,

In the Image-to-Video software that I am using, the size of the images does not matter and the frame rate is set by the user. So I was thinking, I can just set the frame rate to be fixed say 5 frames/sec so when the button is clicked, it will automatically convert the images to video. However, I can also add that feature to my GUI such that when the button is clicked, it will ask for the desired frame rate first before actually converting. But I guess that will make things more complicated because to begin with, I have no idea how the conversion code should look like. :(

-Karen

The last PyMedia release I saw was for Python24:
http://downloads.sourceforge.net/pymedia/pymedia-1.3.5.0-pre2.win32-py2.4.exe?use_mirror=voxel
However, it is possible to create a video with it from within a Python program. Here is an example called make_video.py they list at
http://downloads.sourceforge.net/pymedia/pymedia-examples-1.3.7.zip?use_mirror=internap
...

#! /bin/env python
import sys, os, time
import pymedia.video.vcodec as vcodec
import pygame

def makeVideo( inPattern, outFile, outCodec ):
  # Get video stream dimensions from the image size
  pygame.init()
  i= 1
  # Opening mpeg file for output
  e= None
  i= 1
  fw= open( outFile, 'wb' )
  while i:
    if os.path.isfile( inPattern % i ):
      s= pygame.image.load( inPattern % i )
      if not e:
        if outCodec== 'mpeg1video':
          bitrate= 2700000
        else:
          bitrate= 9800000
        
        params= { \
          'type': 0,
          'gop_size': 12,
          'frame_rate_base': 125,
          'max_b_frames': 0,
          'height': s.get_height(),
          'width': s.get_width(),
          'frame_rate': 2997,
          'deinterlace': 0,
          'bitrate': bitrate,
          'id': vcodec.getCodecID( outCodec )
        }
        print 'Setting codec to ', params
        e= vcodec.Encoder( params )
        t= time.time()
      
      # Create VFrame
      ss= pygame.image.tostring(s, "RGB")
      bmpFrame= vcodec.VFrame( vcodec.formats.PIX_FMT_RGB24, s.get_size(), (ss,None,None))
      yuvFrame= bmpFrame.convert( vcodec.formats.PIX_FMT_YUV420P )
      d= e.encode( yuvFrame )
      fw.write( d.data )
      i+= 1
    else:
      print '%d frames written in %.2f secs( %.2f fps )' % ( i, time.time()- t, float( i )/ ( time.time()- t ) )
      i= 0
  
  fw.close()
  pygame.quit()


if __name__== '__main__':
  if len( sys.argv )!= 4:
    print "Usage: make_video <in_file_pattern> <out_file> <format>\n\tformat= { mpeg1video | mpeg2video }"
  else:
    makeVideo( sys.argv[ 1 ], sys.argv[ 2 ], sys.argv[ 3 ] )

I tried running make_video.py by including it in my main code but I can't seem to make it work. Here's the code. The error says that not all arguments converted during string formatting at this line:

        if os.path.isfile( inPattern % i ):

Can someone please help me? Can this program convert .jpg images to a video assuming that all images have the same size and the fps is set to be 0.5 ?

Thank you so much.

self.Bind(wx.EVT_BUTTON, self.makeVideo, self.ConverterButton)

def makeVideo(self, evt):

        outFile = 'C:/Documents and Settings/12345/Desktop/nyeh.mpg'
        inPattern = 'C:/Documents and Settings/12345/My Documents/Files/ACADEMICS/ECE 198/GUI/cam_pics'
        outCodec = 'mpeg1video'

    # Get video stream dimensions from the image size
        pygame.init()
        i= 1
    # Opening mpeg file for output
        e= None
        i= 1
        fw= open( outFile, 'wb' )

        while i:
            if os.path.isfile( inPattern % i ):
                s= pygame.image.load( inPattern % i )
                if not e:
                    if outCodec== 'mpeg1video':
                        bitrate= 2700000
                    else:
                        bitrate= 9800000

                    params= { \
                        'type': 0,
                        'gop_size': 12,
                        'frame_rate_base': 10,
                        'max_b_frames': 0,
                        'height': s.get_height(),
                        'width': s.get_width(),
                        'frame_rate': 2997,
                        'deinterlace': 0,
                        'bitrate': bitrate,
                        'id': vcodec.getCodecID( outCodec )
                        }

                    print 'Setting codec to ', params
                    e= vcodec.Encoder( params )
                    t= time.time()

           # Create VFrame

                ss= pygame.image.tostring(s, "RGB")
                bmpFrame= vcodec.VFrame( vcodec.formats.PIX_FMT_RGB24, s.get_size(), (ss,None,None))
                yuvFrame= bmpFrame.convert( vcodec.formats.PIX_FMT_YUV420P )
                d= e.encode( yuvFrame )
                fw.write( d.data )
                i+= 1

            else:
                print '%d frames written in %.2f secs( %.2f fps )' % ( i, time.time()- t, float( i )/ ( time.time()- t ) )
                i= 0

        fw.close()
        pygame.quit()

Karen,

Please use code tags when posting code, or else it will become unreadable as it has above.

To use the code tags, wrap your code like this:
[code=python] # Code inside here

[/code]

This will turn on syntax highlighting and preserve your indentation. Also, forum members are much more inclined to help you if your post isn't a huge mess of plain text.

Thanks Jim. Here it is. I am not sure if I correctly set inPattern and outFile that's why the code isn't working. Hope someone can help me. Thanks.

self.Bind(wx.EVT_BUTTON, self.makeVideo, self.ConverterButton)

def makeVideo(self, evt): #make_video.py

outFile = 'C:/Documents and Settings/12345/Desktop/nyeh.mpg'
inPattern = 'C:/Documents and Settings/12345/My Documents/Files/ACADEMICS/ECE 198/GUI/cam_pics'
outCodec = 'mpeg1video'

# Get video stream dimensions from the image size
pygame.init()
i= 1
# Opening mpeg file for output
e= None
i= 1
fw= open( outFile, 'wb' )

while i:
if os.path.isfile( inPattern % i ):
s= pygame.image.load( inPattern % i )
if not e:
if outCodec== 'mpeg1video':
bitrate= 2700000
else:
bitrate= 9800000

params= { \
'type': 0,
'gop_size': 12,
'frame_rate_base': 10,
'max_b_frames': 0,
'height': s.get_height(),
'width': s.get_width(),
'frame_rate': 2997,
'deinterlace': 0,
'bitrate': bitrate,
'id': vcodec.getCodecID( outCodec )
}

print 'Setting codec to ', params
e= vcodec.Encoder( params )
t= time.time()

# Create VFrame

ss= pygame.image.tostring(s, "RGB")
bmpFrame= vcodec.VFrame( vcodec.formats.PIX_FMT_RGB24, s.get_size(), (ss,None,None))
yuvFrame= bmpFrame.convert( vcodec.formats.PIX_FMT_YUV420P )
d= e.encode( yuvFrame )
fw.write( d.data )
i+= 1

else:
print '%d frames written in %.2f secs( %.2f fps )' % ( i, time.time()- t, float( i )/ ( time.time()- t ) )
i= 0

fw.close()
pygame.quit()

Thanks Jim. Here it is. I am not sure if I correctly set inPattern and outFile that's why the code isn't working. Hope someone can help me. And can make_video.py convert a folder of .jpg images to a .mpg video file?

self.Bind(wx.EVT_BUTTON, self.makeVideo, self.ConverterButton)

def makeVideo(self, evt): #make_video.py

     outFile = 'C:/Documents and Settings/12345/Desktop/nyeh.mpg'
     inPattern = 'C:/Documents and Settings/12345/My Documents/Files/ACADEMICS/ECE 198/GUI/cam_pics'
     outCodec = 'mpeg1video'

     # Get video stream dimensions from the image size
     pygame.init()
     i= 1 
    # Opening mpeg file for output
     e= None
     i= 1
     fw= open( outFile, 'wb' )

     while i:
         if os.path.isfile( inPattern % i ):
            s= pygame.image.load( inPattern % i )
                if not e:
                    if outCodec == 'mpeg1video':
                       bitrate= 2700000    
                    else: 
                       bitrate= 9800000

                    params= { \
                       'type': 0,
                       'gop_size': 12,
                       'frame_rate_base': 10, 
                       'max_b_frames': 0,
                       'height': s.get_height(),
                       'width': s.get_width(),
                       'frame_rate': 2997,
                       'deinterlace': 0,
                       'bitrate': bitrate,
                       'id': vcodec.getCodecID( outCodec )
                        }

                    print 'Setting codec to ', params
                    e= vcodec.Encoder( params )
                    t= time.time()

           # Create VFrame

                ss= pygame.image.tostring(s, "RGB")
                bmpFrame= vcodec.VFrame(vcodec.formats.PIX_FMT_RGB24, s.get_size(), (ss,None,None))
                yuvFrame= bmpFrame.convert(vcodec.formats.PIX_FMT_YUV420P)
                d= e.encode( yuvFrame )
                fw.write( d.data )
                i+= 1

         else:
                print '%d frames written in %.2f secs( %.2f fps )' % (i,time.time()- t, float( i )/(time.time()- t))
                i= 0

     fw.close()
     pygame.quit()

Running PyGame from within wxPython will create a conflict, since each use their own event loops, and the event queue will now contain events that won't match.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.