Recording live video stream from IP camera issue

S

Sam Berry

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) it just flashes an image instead of a video file, even though the .avi file is around 20mb.

Is there some extra lines of code i need to include or change? Perhaps a timing option instead of a file size?

Any help or insight would be much appreciated!!

Thanks, Sam
 
S

Steven D'Aprano

Sam said:
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.
 
S

Sam Berry

Thank you for the quick reply!

I found this code just from multiple google searches, but yer it mentioned online video streaming, however my IP camera would be streaming in Mjpeg format (I thought been able to convert between the two formats seemed way to easy!)

So i'm guessing its just 20mb of a picture and then nothing.

Could you give any insight into how to record an mjpeg stream? External libraries for me to consider etc?

Thanks, Sam
 
X

Xavier Ho

I have no vast knowledge of python, but i came across this code to capture
video from my IP camera

This code works, however when i try to playback the .avi file in VLC
player...


I've been working with several IP cameras with Python and OpenCV. It's a
bit of a hassle, but it is not difficult.

However I will say that VLC can open a network location and save the video
at the same time. If all you want to do is capture and save, Python is not
necessary.

Cheers,
Xav
 
S

Sam Berry

Hi,

This is for a university project.

My issue is that i have built an App using pythons Kivy module, and i need to be able to stream and record from an IP camera upon request.

I have just used the VLC.exe to stream the video feed. But it is the recording i am having problems with.

I'l look into OpenCV, thanks for the help!

Sam
 
S

Sam Berry

Hi,

This is for a university project.

My issue is that i have built an App using pythons Kivy module, and i need to be able to stream and record from an IP camera upon request.

I have just used the VLC.exe to stream the video feed. But it is the recording i am having problems with.

I'l look into OpenCV, thanks for the help!

Sam
 
X

Xavier Ho

Hi,

This is for a university project.

My issue is that i have built an App using pythons Kivy module, and i need
to be able to stream and record from an IP camera upon request.

I have just used the VLC.exe to stream the video feed. But it is the
recording i am having problems with.

I'l look into OpenCV, thanks for the help!

Sam

I see. In that case, cv2.VideoCapture(ip_address) should return you a
video object, which you can check for status and use its .read() method to
fetch a frame. :]

Cheers,
Xav
 
S

Sam Berry

Hi Xav,

Iv been looking into OpenCV, i can easily stream my laptops webcam using this code.

import cv2

cv2.namedWindow("preview")

vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False

while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break

However using the cv2.Videocapture(ip_address) method will not load my IP camera video feed into the new window. I can view the stream in any web browser or VLC using the IP address http://192.168.1.72:1025/videostream.cgi?user=&pwd=&resolution=8.

Did you have this issue? Is there some lines of code i may need to add?

Any help would be appreciated!

Thanks, Sam
 
S

Sam Berry

Hi Xav,

Iv been looking into OpenCV, i can easily stream my laptops webcam using this code.

import cv2

cv2.namedWindow("preview")

vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False

while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break

However using the cv2.Videocapture(ip_address) method will not load my IP camera video feed into the new window. I can view the stream in any web browser or VLC using the IP address http://192.168.1.72:1025/videostream.cgi?user=&pwd=&resolution=8.

Did you have this issue? Is there some lines of code i may need to add?

Any help would be appreciated!

Thanks, Sam
 
X

Xavier Ho

Hi Sam,

Did you compile your OpenCV with gstreamer? That's where I'd look first.

Cheers,
Xav
 
S

Sam Berry

Thanks for the advice! I looked into it, seems using windows was an issue, switched to ubuntu and it worked!

Just wondering if you have used opencv for recording purposes?

Or if you know of any python documentation for opencv2?

Information is hard to come by!

Thanks again, Sam
 

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top