Recording live video stream from IP camera issue

Discussion in 'Python' started by Sam Berry, Feb 6, 2013.

  1. Sam Berry

    Sam Berry Guest

    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
    Sam Berry, Feb 6, 2013
    #1
    1. Advertising

  2. 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
    Steven D'Aprano, Feb 6, 2013
    #2
    1. Advertising

  3. Sam Berry

    Sam Berry Guest

    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
    Sam Berry, Feb 6, 2013
    #3
  4. Sam Berry

    Xavier Ho Guest

    Fwd: Recording live video stream from IP camera issue

    On 6 February 2013 23:12, Sam Berry <> 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
    Xavier Ho, Feb 6, 2013
    #4
  5. Sam Berry

    Sam Berry Guest

    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
    Sam Berry, Feb 6, 2013
    #5
  6. Sam Berry

    Sam Berry Guest

    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
    Sam Berry, Feb 6, 2013
    #6
  7. Sam Berry

    Xavier Ho Guest

    On 7 February 2013 00:12, Sam Berry <> 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
    Xavier Ho, Feb 6, 2013
    #7
  8. Sam Berry

    Sam Berry Guest

    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
    Sam Berry, Feb 17, 2013
    #8
  9. Sam Berry

    Sam Berry Guest

    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
    Sam Berry, Feb 17, 2013
    #9
  10. Sam Berry

    Xavier Ho Guest

    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 <> 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
    >
    Xavier Ho, Feb 17, 2013
    #10
  11. Sam Berry

    Sam Berry Guest

    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
    Sam Berry, Feb 18, 2013
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eli
    Replies:
    6
    Views:
    7,906
    daniel08
    Apr 19, 2010
  2. Switchy

    Live Camera Streaming

    Switchy, Mar 17, 2006, in forum: HTML
    Replies:
    1
    Views:
    3,460
    Si Ballenger
    Mar 17, 2006
  3. Newbie
    Replies:
    6
    Views:
    465
    The Artist Formerly Known as Kap'n Salty
    Feb 2, 2005
  4. azsx
    Replies:
    3
    Views:
    446
    Allen
    Aug 25, 2006
  5. Pola
    Replies:
    1
    Views:
    104
    Jeff Cochran
    Jun 29, 2004
Loading...

Share This Page