PDA

View Full Version : streaming video whit it is being generated + a failed CGI approach.



usfish
07 Mar 2011, 11:20 PM
I have a program that can generate video in real time. Now I would like to stream this video online while it is being generated. Does anybody know an easy way to do it? I tried the following method using CGI but somehow it didn't work. Note that I am open to any other solution that does not use CGI. I was just posting my failed CGI approach in case someone knows why it fails.....

I set the content-type to mpeg for example, and print out a chunk of data in the mpeg file periodically. But the video only lasts for very short amount of time and stop streaming. My code is something like this (in Python, but it should be very easy to read).



print "Content-type: video/mpeg"
print
f = open("test2.mpg")
while (True):
st = f.read(1024*1024) # read in 1M of data in this file
sys.stdout.write(st)
time.sleep(0.5) # sleep for 0.5 second


Though this would work fine. I really don't see why the output of these two programs are different. But obviously I can't use this approach since i can't wait until the entire file is generated before reading in.



print "Content-type: video/mpeg"
print
f = open("test2.mpg")
print f.read() #directly print out all the data in the file.