Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38268 > unrolled thread

Recording live video stream from IP camera issue

Started bySam Berry <sambez_14@hotmail.co.uk>
First post2013-02-06 04:12 -0800
Last post2013-02-18 05:03 -0800
Articles 11 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Recording live video stream from IP camera issue Sam Berry <sambez_14@hotmail.co.uk> - 2013-02-06 04:12 -0800
    Re: Recording live video stream from IP camera issue Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-06 23:26 +1100
      Re: Recording live video stream from IP camera issue Sam Berry <sambez_14@hotmail.co.uk> - 2013-02-06 04:36 -0800
    Fwd: Recording live video stream from IP camera issue Xavier Ho <contact@xavierho.com> - 2013-02-06 23:39 +1100
      Re: Recording live video stream from IP camera issue Sam Berry <sambez_14@hotmail.co.uk> - 2013-02-06 05:12 -0800
        Re: Recording live video stream from IP camera issue Xavier Ho <contact@xavierho.com> - 2013-02-07 01:35 +1100
          Re: Recording live video stream from IP camera issue Sam Berry <sambez_14@hotmail.co.uk> - 2013-02-17 06:15 -0800
            Re: Recording live video stream from IP camera issue Xavier Ho <contact@xavierho.com> - 2013-02-18 06:57 +1100
          Re: Recording live video stream from IP camera issue Sam Berry <sambez_14@hotmail.co.uk> - 2013-02-17 06:15 -0800
      Re: Recording live video stream from IP camera issue Sam Berry <sambez_14@hotmail.co.uk> - 2013-02-06 05:12 -0800
    Re: Recording live video stream from IP camera issue Sam Berry <sambez_14@hotmail.co.uk> - 2013-02-18 05:03 -0800

#38268 — Recording live video stream from IP camera issue

FromSam Berry <sambez_14@hotmail.co.uk>
Date2013-02-06 04:12 -0800
SubjectRecording live video stream from IP camera issue
Message-ID<ffa5fb6e-2d2c-484c-bfae-1014f1d0ebcc@googlegroups.com>
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

[toc] | [next] | [standalone]


#38269

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-02-06 23:26 +1100
Message-ID<51124be3$0$29991$c3e8da3$5496439d@news.astraweb.com>
In reply to#38268
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

[toc] | [prev] | [next] | [standalone]


#38270

FromSam Berry <sambez_14@hotmail.co.uk>
Date2013-02-06 04:36 -0800
Message-ID<b48ba11d-627d-4d28-8b55-6f1747fe1fe5@googlegroups.com>
In reply to#38269
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

[toc] | [prev] | [next] | [standalone]


#38271

FromXavier Ho <contact@xavierho.com>
Date2013-02-06 23:39 +1100
Message-ID<mailman.1407.1360154390.2939.python-list@python.org>
In reply to#38268

[Multipart message — attachments visible in raw view] — view raw

On 6 February 2013 23:12, Sam Berry <sambez_14@hotmail.co.uk> 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

[toc] | [prev] | [next] | [standalone]


#38273

FromSam Berry <sambez_14@hotmail.co.uk>
Date2013-02-06 05:12 -0800
Message-ID<9c18d916-839c-4335-a6fa-c61fd87bd380@googlegroups.com>
In reply to#38271
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

[toc] | [prev] | [next] | [standalone]


#38278

FromXavier Ho <contact@xavierho.com>
Date2013-02-07 01:35 +1100
Message-ID<mailman.1411.1360161337.2939.python-list@python.org>
In reply to#38273

[Multipart message — attachments visible in raw view] — view raw

On 7 February 2013 00:12, Sam Berry <sambez_14@hotmail.co.uk> 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

[toc] | [prev] | [next] | [standalone]


#39029

FromSam Berry <sambez_14@hotmail.co.uk>
Date2013-02-17 06:15 -0800
Message-ID<b52c7294-e0c5-449f-802a-1383214a77ce@googlegroups.com>
In reply to#38278
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

[toc] | [prev] | [next] | [standalone]


#39035

FromXavier Ho <contact@xavierho.com>
Date2013-02-18 06:57 +1100
Message-ID<mailman.1902.1361131104.2939.python-list@python.org>
In reply to#39029

[Multipart message — attachments visible in raw view] — view raw

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 <sambez_14@hotmail.co.uk> 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.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
>

[toc] | [prev] | [next] | [standalone]


#39030

FromSam Berry <sambez_14@hotmail.co.uk>
Date2013-02-17 06:15 -0800
Message-ID<mailman.1895.1361110534.2939.python-list@python.org>
In reply to#38278
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

[toc] | [prev] | [next] | [standalone]


#38274

FromSam Berry <sambez_14@hotmail.co.uk>
Date2013-02-06 05:12 -0800
Message-ID<mailman.1409.1360156360.2939.python-list@python.org>
In reply to#38271
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

[toc] | [prev] | [next] | [standalone]


#39087

FromSam Berry <sambez_14@hotmail.co.uk>
Date2013-02-18 05:03 -0800
Message-ID<2611b0f8-3471-4df4-a6a8-d605676838ef@googlegroups.com>
In reply to#38268
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web