Record video

Updated: May 06, 2022

You can record video to a file.

If you have multiple cameras, the process is the same as using a single camera; full details are given in Synchronized recording of video from multiple cameras.” Here, we show the steps to record video to a file. You can record to either H.264-encoded video or uncompressed video (UCV or MOV) format.
  1. Connect to the camera. Ensure that you open the camera with a camera mode that includes CAMERA_MODE_ROLL.
  2. Set the viewfinder mode for the camera.
  3. Set up Screen. See Connect Screen with the Camera API for more information.
  4. Start the viewfinder and configure camera settings.
  5. Get access to the viewfinder window and to make it visible. See Use Screen to show the viewfinder window for details.
  6. If you change the format, width, and height of the viewfinder from its default settings, you must set the matching video properties to those new values because the default properties for video should match the viewfinder properties that were overwritten. The exception is framerate, where you can set the viewfinder to record at an integer multiple of the viewfinder framerate, as long as the framerate isn't higher. For example, if your viewfinder framerate is set to 30 fps, you can record 15 fps (multiple of two) or 10 fps (multiple of three).
    You can get the viewfinder properties from the camera using camera_get_vf_property(). Here are the typical properties that you should check:
    • CAMERA_IMGPROP_FORMAT
    • CAMERA_IMGPROP_WIDTH
    • CAMERA_IMGPROP_HEIGHT
    • CAMERA_IMGPROP_ROTATION
    • CAMERA_IMGPROP_FRAMERATE
    • CAMERA_IMGPROP_VIDEOCODEC
      Note: If you set this property to CAMERA_VIDEOCODEC_NONE, you record to uncompressed video. You can specify whether to record to MOV or UCV (UnCompressed Video) when you call camera_roll_open_video(); for more details, see the next step.
    • CAMERA_IMGPROP_AUDIOCODEC

    Then, you can set the properties using camera_set_video_property(). You can set any of the properties listed above using this function.

  7. Open a file to record the video. The filename must be unique. If you requested to record to uncompressed video, you can specify the format to use for the recording by including .mov or .ucv as the file extension. To make this easier, you can call camera_roll_open_video() to generate a unique name for your file and specify the uncompressed format to use, by passing into the fmt argument either CAMERA_ROLL_VIDEO_FMT_MOV for .mov files or CAMERA_ROLL_VIDEO_FMT_UCV for .ucv files.
  8. Start recording video by calling camera_start_video().
  9. When you're ready to stop recording video, call camera_stop_video().
  10. Close the video file by calling camera_roll_close_video().

    This stops the viewfinder and hence, the image buffer from streaming. You don't need to disconnect from (close) the camera until you are done using it. For example, you can stop and then start the viewfinder again. For more information, see Stop the viewfinder.”

The following code snippet presumes that you're using a camera configured as CAMERA_UNIT_1 and shows how to record video from a single camera. For more information, see the source for the Camera example application provided with this release.
...
...
int err;

// 1. To prepare to start recording, connect to the camera
if (err = camera_open(CAMERA_UNIT_1, 
                      CAMERA_MODE_RW | CAMERA_MODE_ROLL, 
                      &cameraHandle) != CAMERA_EOK) {
    printf("Failed to open the camera: err = %d\n", err);
    return err;
}
// 2. Set the viewfinder image stream mode
if (err = camera_set_vf_mode(cameraHandle, CAMERA_VFMODE_VIDEO) != CAMERA_EOK) {
    printf("Failed to set the viewfinder mode: err = %d\n", err);
    camera_close(cameraHandle);
    return err;
}

// 3. Set up Screen to connect it with the Camera API.
// This entails using the exact same code shown in "Connect Screen with the Camera API".
// You must insert that code at this point, and follow it immediately with the code shown next.

// 4. Start the viewfinder -- in this sample, we don't configuration any camera settings
err = camera_start_viewfinder(cameraHandle, NULL, NULL, NULL);
if (err != CAMERA_EOK) {
    printf("Failed to start viewfinder: err = %d\n", err);
    camera_close(cameraHandle);
    return err;
}

// 5. We now need to use Screen to get access to the viewfinder window and to make it visible.
// This entails using the exact same code shown in "Use Screen to show the viewfinder window".
// You must insert that code at this point, and follow it immediately with the code shown next.

// Retrieve viewfinder properties and the default framerate, video codec, and audio codec
camera_frametype_t format;
uint32_t width, height, rotation;
err = camera_get_vf_property(cameraHandle,
                             CAMERA_IMGPROP_FORMAT, &format,
                             CAMERA_IMGPROP_WIDTH, &width,
                             CAMERA_IMGPROP_HEIGHT, &height,
                             CAMERA_IMGPROP_ROTATION, &rotation);

double framerate;
camera_videocodec_t videoCodec;
camera_audiocodec_t audioCodec;
err = camera_get_video_property(cameraHandle,
                                CAMERA_IMGPROP_FRAMERATE, &framerate,
                                CAMERA_IMGPROP_VIDEOCODEC, &videoCodec,
                                CAMERA_IMGPROP_AUDIOCODEC, &audioCodec);

// Set the equivalent properties for the camera
err = camera_set_video_property(cameraHandle,
                                CAMERA_IMGPROP_FORMAT, format,
                                CAMERA_IMGPROP_WIDTH, width,
                                CAMERA_IMGPROP_HEIGHT, height,
                                CAMERA_IMGPROP_ROTATION, rotation,
                                CAMERA_IMGPROP_FRAMERATE, framerate,
                                CAMERA_IMGPROP_VIDEOCODEC, videoCodec,
                                CAMERA_IMGPROP_AUDIOCODEC, audioCodec);

int exitExample = 0;
static char userString[80];
while (exitExample == 0) {
     printf("\tPress c to start recording:\n");
     fgets(userString, sizeof(userString), stdin);
     if ( (userString[0] == 'c') || (userString[0] == 'C') ) {
         exitExample = 1;
     }
}
// Open a file for recording
char* rollFilename = (char*) malloc(sizeof(char) * CAMERA_ROLL_NAMELEN);
int fd;
camera_roll_open_video(cameraHandle, 
                        &fd, rollFilename, CAMERA_ROLL_NAMELEN, CAMERA_ROLL_VIDEO_FMT_MP4);

// Start recording video
camera_start_video(cameraHandle, rollFilename, NULL, NULL, NULL);

exitExample = 0;
while (exitExample == 0) {
    printf("\tPress c to stop recording:\n");
    fgets(userString, sizeof(userString), stdin);
    if ( (userString[0] == 'c') || (userString[0] == 'C') ) {
        exitExample = 1;
    }
}
// Stop recording video and close the file in the camera roll
camera_stop_video(cameraHandle);
camera_roll_close_video(fd);

// Stop receiving image buffers and disconnect from the camera
camera_stop_viewfinder(cameraHandle);
camera_close(cameraHandle);

// Clean up memory allocated for the video file
free(rollFilename);
          
// Clean up the Screen objects
screen_destroy_window(viewfinder);
screen_destroy_window(window);
screen_destroy_context(context);
...
...