Image to Video Converter Code Snippet

Thread Solved

Join Date: Feb 2009
Posts: 11
Reputation: karenmaye is an unknown quantity at this point 
Solved Threads: 0
karenmaye karenmaye is offline Offline
Newbie Poster

Image to Video Converter Code Snippet

 
0
  #1
Feb 27th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Image to Video Converter Code Snippet

 
0
  #2
Feb 27th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 11
Reputation: karenmaye is an unknown quantity at this point 
Solved Threads: 0
karenmaye karenmaye is offline Offline
Newbie Poster

Re: Image to Video Converter Code Snippet

 
0
  #3
Feb 27th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,013
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 929
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Image to Video Converter Code Snippet

 
0
  #4
Mar 2nd, 2009
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
...
  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 ] )
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 1
Reputation: milkyway462 is an unknown quantity at this point 
Solved Threads: 1
milkyway462 milkyway462 is offline Offline
Newbie Poster

Re: Image to Video Converter Code Snippet

 
0
  #5
Mar 5th, 2009
I've tried many Video Converter download from internet and choose the best to share here, Daniusoft Video Converter which could help you convert convert video format to MPEG/WMV/AVI and other popular video formats such as: MP4, FLV, 3GP, ASF, MOV, M4V, MPG, VOB, XviD with high speed and excellent quality. Also let you set the movie effects.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 11
Reputation: karenmaye is an unknown quantity at this point 
Solved Threads: 0
karenmaye karenmaye is offline Offline
Newbie Poster

Re: Image to Video Converter Code Snippet

 
0
  #6
Mar 19th, 2009
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()
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Image to Video Converter Code Snippet

 
0
  #7
Mar 19th, 2009
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.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 11
Reputation: karenmaye is an unknown quantity at this point 
Solved Threads: 0
karenmaye karenmaye is offline Offline
Newbie Poster

Re: Image to Video Converter Code Snippet

 
0
  #8
Mar 19th, 2009
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.

  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()
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 11
Reputation: karenmaye is an unknown quantity at this point 
Solved Threads: 0
karenmaye karenmaye is offline Offline
Newbie Poster

Re: Image to Video Converter Code Snippet

 
0
  #9
Mar 19th, 2009
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?

  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()
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Image to Video Converter Code Snippet

 
0
  #10
Mar 19th, 2009
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.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC