MCAP Video ingest in Python

This guide will walk you through the process of downloading a sample MCAP file, inspecting it to find video topics, and ingesting the video into Nominal.

Prerequisites

Make sure you have the following Python packages installed:

  • huggingface_hub
  • mcap
  • nominal

You can install them all using:

1pip3 install huggingface_hub mcap nominal

Step 1: Download the Sample MCAP File

First, download a sample MCAP file using the huggingface_hub library.

1from huggingface_hub import hf_hub_download
2
3video_path = hf_hub_download(
4 repo_id="nominal-io/xplane",
5 filename="xplane.mcap",
6 repo_type='dataset'
7)
8
9print(f"File saved to: {video_path}")

Step 2: Inspect the MCAP File to Find Video Topics

Next, use the mcap library to inspect the MCAP file and identify available video topics.

1from mcap.reader import make_reader
2
3with open(video_path, "rb") as f:
4 reader = make_reader(f)
5
6 schemas = reader.get_summary().schemas
7 channels = reader.get_summary().channels
8
9 def schema_name(channel):
10 schema = schemas.get(channel.schema_id)
11 if not schema:
12 return None
13 return schema.name
14
15 video_topics = [
16 channel.topic for channel in channels.values()
17 if schema_name(channel) == "foxglove.CompressedVideo"
18 ]
19
20 print("Video topics: ", video_topics)

This script will output the video topics available in the MCAP file, which you’ll use in the next step.

Step 3: Connect to Nominal

Before ingesting the video, ensure you’re connected to Nominal.

When using the Nominal client library, there are two primary ways of authenticating:

First, run the following in your terminal and follow on-screen prompts to insert the base_url and API key:

$$ python -m nominal auth set-token
>
># Alternatively, use the globally installed CLI
>$ nom auth set-token

This will store your API key in a config file ~/.nominal.yml. The API key will automatically be used when using the client again.

1import nominal
2
3# Simply grab the "default" client using your stored credentials
4client = nominal.get_default_client()
5
6# Get details about the currently logged-in user to validate authentication
7# Will display an object like: `User(display_name='your_email@your_company.com', ...)`
8print(client.get_user())
1import nominal
2
3# Set login details for the user
4nominal.set_token("<insert api key>")
5
6# Get an instance of the client using provided credentials
7client = nominal.get_default_client()
8
9# Get details about the currently logged-in user to validate authentication
10# Will display an object like: `User(display_name='your_email@your_company.com', ...)`
11print(client.get_user())

NOTE: you should never share your Nominal API key with anyone. We therefore recommend that you not save it in your code and/or scripts.

  • If you trust the computer you are on, use nom to store the credential to disk.
  • Otherwise, use a password manager such as 1password or bitwarden to keep your token safe.
If you’re not sure whether your company has a Nominal tenant, please reach out to us.

Step 4: Ingest the Video

Finally, upload the video to Nominal using a topic from the list.

1import nominal as nm
2
3video = nm.upload_mcap_video(video_path, "video")
4print(video)