API Sample:Focus on Specific Subjects in a Video
In this sample, we'll upload several video files and extract segments featuring a specific person by describing their attributes.
Overview
- Objective: Extract segments of a specific person from multiple videos.
- APIs Used:
- Asset Management API
- Timeline API
Steps
- Authenticate and set up necessary variables.
- Upload the video files using the Asset Management API.
- Generate a timeline focusing on the specific subject using the Timeline API.
- Retrieve and display the generated timeline.
- Export the timeline in FCP_XML format.
Detailed Steps with Code
Step 1: Authenticate and Set Up
Use the same authentication headers as in previous samples.
import requests
import json
# Replace with your actual API key
API_KEY = 'YOUR_API_KEY'
# Base URLs
BASE_URL = 'https://api.play-video.dev'
# Headers
headers = {
'X-API-Key': API_KEY,
'Content-Type': 'application/json'
}Step 2: Upload the Video Files
We'll upload 5 video files.
a) Create Assets and Upload Files
asset_uuids = []
for i in range(1, 6):
filename = f'video_{i}.mp4'
file_path = f'/path/to/your/{filename}' # Replace with your actual file paths
# Create an asset
create_asset_url = f"{BASE_URL}/asset/create"
create_asset_payload = {
'filename': filename,
'indexer_profile_uuid': 'YOUR_INDEXER_PROFILE_UUID' # Replace with your indexer profile UUID
}
response = requests.post(create_asset_url, headers=headers, json=create_asset_payload)
response.raise_for_status()
asset_response = response.json()
asset_uuid = asset_response['uuid']
s3_presigned_url = asset_response['s3_presigned_url']
print(f"Asset {i} UUID: {asset_uuid}")
# Upload the file to S3
with open(file_path, 'rb') as file_data:
s3_response = requests.put(s3_presigned_url, data=file_data)
s3_response.raise_for_status()
print(f"File {i} uploaded successfully to S3.")
# Add the asset UUID to the list
asset_uuids.append(asset_uuid)Step 3: Generate a Timeline Focusing on the Specific Subject
Describe the person's attributes to focus on their segments.
# Step 3: Generate timeline focusing on a specific subject
generate_timeline_url = f"{BASE_URL}/timelines/generate/subject"
# Request payload
generate_timeline_payload = {
'prompt': 'Extract all segments featuring the CEO, John Doe, who often wears glasses and speaks about company growth.',
'files': asset_uuids,
'subjects': ['John Doe', 'CEO', 'glasses', 'company growth']
}
timeline_response = requests.post(generate_timeline_url, headers=headers, json=generate_timeline_payload)
timeline_response.raise_for_status()
timeline_data = timeline_response.json()
# Extract the timeline UUID
timeline_uuid = timeline_data['uuid']
print(f"Generated Timeline UUID: {timeline_uuid}")Step 4: Retrieve and Display the Generated Timeline
Retrieve the generated timeline to inspect the segments.
# Step 4: Retrieve the generated timeline
get_timeline_url = f"{BASE_URL}/timelines/{timeline_uuid}"
timeline_details_response = requests.get(get_timeline_url, headers=headers)
timeline_details_response.raise_for_status()
timeline_details = timeline_details_response.json()
print("Generated Timeline Details:")
print(json.dumps(timeline_details, indent=2))Step 5: Export the Timeline as FCP_XML
Once the timeline is ready, export it in FCP_XML format.
# Step 5: Export the timeline to FCP_XML format
export_format = 'fcp_xml' # Can be 'otio' or 'fcp_xml'
export_timeline_url = f"{BASE_URL}/timelines/{timeline_uuid}/export/{export_format}"
export_response = requests.post(export_timeline_url, headers={'X-API-Key': API_KEY})
export_response.raise_for_status()
# Save the exported timeline to a file
output_filename = f"specific_subject_timeline.{export_format}"
with open(output_filename, 'wb') as f:
f.write(export_response.content)
print(f"Timeline exported as '{output_filename}'.")