Record video

Updated: April 19, 2023

You can record video to a file.

Here, we show the steps to record video from a single camera to a file in compressed video format. 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.” We don't do so in this example, but you can configure the video encoding; for example, by selecting a local or remote video encoder to use, as shown in Configure the video encoding.”
  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 to work with the Camera API.
  4. Start the viewfinder.
  5. Use Screen to access the viewfinder window and make it visible.
  6. If you change the format, width, and height of the viewfinder from their 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 video framerate, which you can set to record at an integer multiple of the viewfinder framerate, as long as this new 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 any of the equivalent video properties using camera_set_video_property().

  7. Open a file to record the video. The filename must be unique. You can call camera_roll_open_video() to generate a unique name for your file, while specifying the format to use through the fmt argument. In our case, we set this argument to CAMERA_ROLL_VIDEO_FMT_MP4 to record compressed video to a .mp4 file, but you can also record to an uncompressed file format; for details, see the camera_roll_video_fmt_t reference.
  8. Start recording video by calling camera_start_video().
  9. When you're ready to stop recording video, call camera_stop_video() to stop the recording and camera_roll_close_video() to close the file in the camera roll.
  10. Stop the viewfinder and disconnect from the camera when you're done using it. For more information, see Stop the viewfinder.”
  11. Clean up the memory for the video file and the Screen objects.
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.
...
...
camera_handle_t cameraHandle;
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 work 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 access the viewfinder window and 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.

// 6. Retrieve the relevant viewfinder properties and set the equivalent parameters
// for video recording.
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_vf_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;
     }
}

// 7. 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);

// 8. 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;
    }
}
// 9. Stop recording video and close the file in the camera roll
camera_stop_video(cameraHandle);
camera_roll_close_video(fd);

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

// 11. Clean up memory allocated for the video file, and Screen objects (not shown)
free(rollFilename);
...
...