943,623 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 4076
  • Python RSS
Feb 27th, 2009
0

Image to Video Converter Code Snippet

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
karenmaye is offline Offline
11 posts
since Feb 2009
Feb 27th, 2009
0

Re: Image to Video Converter Code Snippet

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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Feb 27th, 2009
0

Re: Image to Video Converter Code Snippet

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
karenmaye is offline Offline
11 posts
since Feb 2009
Mar 2nd, 2009
0

Re: Image to Video Converter Code Snippet

The last PyMedia release I saw was for Python24:
http://downloads.sourceforge.net/pym...e_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/pym...irror=internap
...
python Syntax (Toggle Plain Text)
  1. #! /bin/env python
  2. import sys, os, time
  3. import pymedia.video.vcodec as vcodec
  4. import pygame
  5.  
  6. def makeVideo( inPattern, outFile, outCodec ):
  7. # Get video stream dimensions from the image size
  8. pygame.init()
  9. i= 1
  10. # Opening mpeg file for output
  11. e= None
  12. i= 1
  13. fw= open( outFile, 'wb' )
  14. while i:
  15. if os.path.isfile( inPattern % i ):
  16. s= pygame.image.load( inPattern % i )
  17. if not e:
  18. if outCodec== 'mpeg1video':
  19. bitrate= 2700000
  20. else:
  21. bitrate= 9800000
  22.  
  23. params= { \
  24. 'type': 0,
  25. 'gop_size': 12,
  26. 'frame_rate_base': 125,
  27. 'max_b_frames': 0,
  28. 'height': s.get_height(),
  29. 'width': s.get_width(),
  30. 'frame_rate': 2997,
  31. 'deinterlace': 0,
  32. 'bitrate': bitrate,
  33. 'id': vcodec.getCodecID( outCodec )
  34. }
  35. print 'Setting codec to ', params
  36. e= vcodec.Encoder( params )
  37. t= time.time()
  38.  
  39. # Create VFrame
  40. ss= pygame.image.tostring(s, "RGB")
  41. bmpFrame= vcodec.VFrame( vcodec.formats.PIX_FMT_RGB24, s.get_size(), (ss,None,None))
  42. yuvFrame= bmpFrame.convert( vcodec.formats.PIX_FMT_YUV420P )
  43. d= e.encode( yuvFrame )
  44. fw.write( d.data )
  45. i+= 1
  46. else:
  47. print '%d frames written in %.2f secs( %.2f fps )' % ( i, time.time()- t, float( i )/ ( time.time()- t ) )
  48. i= 0
  49.  
  50. fw.close()
  51. pygame.quit()
  52.  
  53.  
  54. if __name__== '__main__':
  55. if len( sys.argv )!= 4:
  56. print "Usage: make_video <in_file_pattern> <out_file> <format>\n\tformat= { mpeg1video | mpeg2video }"
  57. else:
  58. makeVideo( sys.argv[ 1 ], sys.argv[ 2 ], sys.argv[ 3 ] )
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Mar 19th, 2009
0

Re: Image to Video Converter Code Snippet

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()
Reputation Points: 10
Solved Threads: 0
Newbie Poster
karenmaye is offline Offline
11 posts
since Feb 2009
Mar 19th, 2009
0

Re: Image to Video Converter Code Snippet

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.
Last edited by jlm699; Mar 19th, 2009 at 9:14 am.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Mar 19th, 2009
0

Re: Image to Video Converter Code Snippet

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.

python Syntax (Toggle Plain Text)
  1. self.Bind(wx.EVT_BUTTON, self.makeVideo, self.ConverterButton)
  2.  
  3. def makeVideo(self, evt): #make_video.py
  4.  
  5. outFile = 'C:/Documents and Settings/12345/Desktop/nyeh.mpg'
  6. inPattern = 'C:/Documents and Settings/12345/My Documents/Files/ACADEMICS/ECE 198/GUI/cam_pics'
  7. outCodec = 'mpeg1video'
  8.  
  9. # Get video stream dimensions from the image size
  10. pygame.init()
  11. i= 1
  12. # Opening mpeg file for output
  13. e= None
  14. i= 1
  15. fw= open( outFile, 'wb' )
  16.  
  17. while i:
  18. if os.path.isfile( inPattern % i ):
  19. s= pygame.image.load( inPattern % i )
  20. if not e:
  21. if outCodec== 'mpeg1video':
  22. bitrate= 2700000
  23. else:
  24. bitrate= 9800000
  25.  
  26. params= { \
  27. 'type': 0,
  28. 'gop_size': 12,
  29. 'frame_rate_base': 10,
  30. 'max_b_frames': 0,
  31. 'height': s.get_height(),
  32. 'width': s.get_width(),
  33. 'frame_rate': 2997,
  34. 'deinterlace': 0,
  35. 'bitrate': bitrate,
  36. 'id': vcodec.getCodecID( outCodec )
  37. }
  38.  
  39. print 'Setting codec to ', params
  40. e= vcodec.Encoder( params )
  41. t= time.time()
  42.  
  43. # Create VFrame
  44.  
  45. ss= pygame.image.tostring(s, "RGB")
  46. bmpFrame= vcodec.VFrame( vcodec.formats.PIX_FMT_RGB24, s.get_size(), (ss,None,None))
  47. yuvFrame= bmpFrame.convert( vcodec.formats.PIX_FMT_YUV420P )
  48. d= e.encode( yuvFrame )
  49. fw.write( d.data )
  50. i+= 1
  51.  
  52. else:
  53. print '%d frames written in %.2f secs( %.2f fps )' % ( i, time.time()- t, float( i )/ ( time.time()- t ) )
  54. i= 0
  55.  
  56. fw.close()
  57. pygame.quit()
Reputation Points: 10
Solved Threads: 0
Newbie Poster
karenmaye is offline Offline
11 posts
since Feb 2009
Mar 19th, 2009
0

Re: Image to Video Converter Code Snippet

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?

python Syntax (Toggle Plain Text)
  1. self.Bind(wx.EVT_BUTTON, self.makeVideo, self.ConverterButton)
  2.  
  3. def makeVideo(self, evt): #make_video.py
  4.  
  5. outFile = 'C:/Documents and Settings/12345/Desktop/nyeh.mpg'
  6. inPattern = 'C:/Documents and Settings/12345/My Documents/Files/ACADEMICS/ECE 198/GUI/cam_pics'
  7. outCodec = 'mpeg1video'
  8.  
  9. # Get video stream dimensions from the image size
  10. pygame.init()
  11. i= 1
  12. # Opening mpeg file for output
  13. e= None
  14. i= 1
  15. fw= open( outFile, 'wb' )
  16.  
  17. while i:
  18. if os.path.isfile( inPattern % i ):
  19. s= pygame.image.load( inPattern % i )
  20. if not e:
  21. if outCodec == 'mpeg1video':
  22. bitrate= 2700000
  23. else:
  24. bitrate= 9800000
  25.  
  26. params= { \
  27. 'type': 0,
  28. 'gop_size': 12,
  29. 'frame_rate_base': 10,
  30. 'max_b_frames': 0,
  31. 'height': s.get_height(),
  32. 'width': s.get_width(),
  33. 'frame_rate': 2997,
  34. 'deinterlace': 0,
  35. 'bitrate': bitrate,
  36. 'id': vcodec.getCodecID( outCodec )
  37. }
  38.  
  39. print 'Setting codec to ', params
  40. e= vcodec.Encoder( params )
  41. t= time.time()
  42.  
  43. # Create VFrame
  44.  
  45. ss= pygame.image.tostring(s, "RGB")
  46. bmpFrame= vcodec.VFrame(vcodec.formats.PIX_FMT_RGB24, s.get_size(), (ss,None,None))
  47. yuvFrame= bmpFrame.convert(vcodec.formats.PIX_FMT_YUV420P)
  48. d= e.encode( yuvFrame )
  49. fw.write( d.data )
  50. i+= 1
  51.  
  52. else:
  53. print '%d frames written in %.2f secs( %.2f fps )' % (i,time.time()- t, float( i )/(time.time()- t))
  54. i= 0
  55.  
  56. fw.close()
  57. pygame.quit()
Reputation Points: 10
Solved Threads: 0
Newbie Poster
karenmaye is offline Offline
11 posts
since Feb 2009
Mar 19th, 2009
0

Re: Image to Video Converter Code Snippet

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.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Python search command ???
Next Thread in Python Forum Timeline: Script arguments parsing.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC