Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Recording live video stream from IP camera issue

Reply
Thread Tools

Recording live video stream from IP camera issue

 
 
Sam Berry
Guest
Posts: n/a
 
      02-06-2013
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())+".a vi"
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
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-06-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())+".a vi"
> 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

 
Reply With Quote
 
 
 
 
Sam Berry
Guest
Posts: n/a
 
      02-06-2013
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
 
Reply With Quote
 
Xavier Ho
Guest
Posts: n/a
 
      02-06-2013
On 6 February 2013 23:12, Sam Berry <(E-Mail Removed)> wrote:

> 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

 
Reply With Quote
 
Sam Berry
Guest
Posts: n/a
 
      02-06-2013
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
 
Reply With Quote
 
Sam Berry
Guest
Posts: n/a
 
      02-06-2013
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
 
Reply With Quote
 
Xavier Ho
Guest
Posts: n/a
 
      02-06-2013
On 7 February 2013 00:12, Sam Berry <(E-Mail Removed)> wrote:

> 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

 
Reply With Quote
 
Sam Berry
Guest
Posts: n/a
 
      02-17-2013
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...=&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
 
Reply With Quote
 
Sam Berry
Guest
Posts: n/a
 
      02-17-2013
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...=&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
 
Reply With Quote
 
Xavier Ho
Guest
Posts: n/a
 
      02-17-2013
Hi Sam,

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

Cheers,
Xav


On 18 February 2013 01:15, Sam Berry <(E-Mail Removed)> wrote:

> 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...=&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
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to improve the live stream audio /video quality Makhdoom Computer Support 9 03-16-2009 11:54 AM
Questions about recording Video on a Sony Cyber-shot DSC-W1 Digital Camera Mike Digital Photography 6 09-15-2004 03:35 AM
Problem recording cd from live stream ShantiP1 Computer Information 4 05-14-2004 12:17 AM
Recording live video streams Len Computer Support 6 02-16-2004 06:22 AM



Advertisments