Recording live video stream from IP camera issue

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Feb 6 13:26:10 CET 2013


Sam Berry wrote:

> Hey,
> 
> I have no vast knowledge of python, but i came across this code to capture
> video from my IP camera
> 
> import urllib2
> import time
> import logging
> 
> print "Recording video..."
> response = urllib2.urlopen("IP Address")
> filename = time.strftime("%Y%m%d%H%M%S",time.localtime())+".avi"
> f = open(filename, 'wb')
> 
> video_file_size_start = 0
> video_file_size_end = 1048576 * 20  # end in 20 mb
> block_size = 1024
> 
> while True:
>     try:
>         buffer = response.read(block_size)
>         if not buffer:
>             break
>         video_file_size_start += len(buffer)
>         if video_file_size_start > video_file_size_end:
>             break
>         f.write(buffer)
> 
>     except Exception, e:
>         logging.exception(e)
> f.close()
> 
> This code works, however when i try to playback the .avi file in VLC
> player (file wont open in any other media player) 

Seems fairly clear evidence that it is not, in fact, an AVI file, no matter
what file extension you give the file.

Where did you get this code?


> it just flashes an image 
> instead of a video file, even though the .avi file is around 20mb.

Does your camera actually generate video? If your camera is just generating
a single still image every few seconds, then the recorded file will be one
frame followed by many frames of nothing, then you'll hit the 20MB limit
before the next still picture is taken.

If your camera does generate video, what sort of video does it generate? If
it generates (say) mp4 and you are shoving it into a avi file, it could
very likely confuse most media players.

If you are on Linux, try this:

- rename the file *without* the .avi at the end;

- at the shell command line, give the command "file name-of-your-video-file"
and see what the file command thinks it contains. 



-- 
Steven




More information about the Python-list mailing list