1.11M Members

Image to Video Converter Code Snippet

 
0
 

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

 
0
 

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

 
0
 

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

 
0
 

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 ] )
 
0
 

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()
 
0
 

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.

 
0
 

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()
 
0
 

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()
 
0
 

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.

Question Answered as of 5 Years Ago by sneekula, jlm699, vegaseat and 1 other
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article