Acquire IP Camera Images in a Loop and Create an AVI File

This example shows how to acquire multiple image frames from an IP camera, and log the images to an AVI file using MATLAB® VideoWriter.

  1. Create an object, cam, using the URL of the IP camera. The URL must be for a Motion JPEG (mjpeg) HTTP or RTSP stream, so the URL must start with http. For information about finding the URL, see Tips for Finding the IP Camera URL.

    cam = ipcam('http://172.28.17.104/video/mjpg.cgi')
    cam = 
    
    Display Summary for ipcam:
    
                 URL: 'http://172.28.17.104/video/mjpg.cgi'
            Username: ''
            Password: ''
             Timeout: 10

    The ipcam function creates the object and connects it to the IP camera with the specified URL. The camera used in this example does not require user authentication. If your camera does, see Connect with User Authentication to create the object with user name and password.

  2. Optionally, preview the image from the camera.

    preview(cam)

    The preview window opens and displays live video stream from your camera. For more information about the preview window, see the preview function.

  3. You can close the preview window at any time.

    closePreview(cam)
  4. Create a VideoWriter object to open an AVI file for writing.

    vidWriter = VideoWriter('frames.avi');
    open(vidWriter);
  5. Acquire and store 20 frames. This loop writes the acquired frames to the specified AVI file for future processing.

    for index = 1:20
       % Acquire a single frame.
       Image = snapshot(cam);
    
       % Write frame to video.
       writeVideo(vidWriter, Image);
    
       end
  6. Release the camera by clearing the object.

    clear cam