API Sample: Find Interview Segments in Long Form Recording
In this sample, we'll demonstrate how to upload a video file and generate a timeline that only includes segments where an interview takes place.
Overview
- Objective: Extract interview segments from a long-form recording.
- APIs Used:
- Asset Management API
- Timeline API
Steps
- Authenticate and set up necessary variables.
- Upload the video file using the Asset Management API.
- Generate a highlight timeline focusing on interview segments using the Timeline API.
- Retrieve and display the generated timeline.
- Export the timeline in CMX3600 EDL format.
Detailed Steps with Code
Step 1: Authenticate and Set Up
First, set up your API key and necessary variables.
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 File
Use the Asset Management API to create an asset and upload your video file.
a) Create an Asset
import uuid
# Step 2a: Create an asset
create_asset_url = f"{BASE_URL}/asset/create"
filename = 'long_form_recording.mp4'
# Request payload
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()
# Extract the asset UUID and presigned URL
asset_uuid = asset_response['uuid']
s3_presigned_url = asset_response['s3_presigned_url']
print(f"Asset UUID: {asset_uuid}")
print(f"S3 Presigned URL: {s3_presigned_url}")b) Upload the Video File to S3
# Step 2b: Upload the video file to S3
file_path = '/path/to/your/long_form_recording.mp4' # Replace with your actual file path
with open(file_path, 'rb') as file_data:
s3_response = requests.put(s3_presigned_url, data=file_data)
s3_response.raise_for_status()
print("File uploaded successfully to S3.")Note: Depending on the processing time, you might need to poll the asset status or wait until it's ready before retrieving it.
c) Poll asset status
# Step 2c: Poll the asset status
import requests
# Helper function to poll asset status
def poll_asset_status(asset_uuid, interval=10, timeout=1200):
status_url = f"{BASE_URL}/asset/{asset_uuid}/status"
elapsed_time = 0
while elapsed_time < timeout:
response = requests.get(status_url, headers=headers)
response.raise_for_status()
status = response.json()['status']
print(f"Asset {asset_uuid} status: {status}")
if status == 'READY':
return True
elif status == 'FAILED':
raise Exception(f"Asset {asset_uuid} processing failed.")
time.sleep(interval)
elapsed_time += interval
raise TimeoutError(f"Asset {asset_uuid} did not become READY within {timeout} seconds.")
try:
poll_asset_status(asset_uuid)
print(f"Asset {asset_uuid} is READY.")
except Exception as e:
print(str(e))
exit(1)Step 3: Generate a Highlight Timeline Focusing on Interviews
Use the Timeline API to generate a timeline that highlights interview segments.
# Step 3: Generate timeline highlight
generate_timeline_url = f"{BASE_URL}/timelines/generate/highlight"
# Request payload
generate_timeline_payload = {
'prompt': 'Extract all interview segments where someone is being interviewed.',
'files': [asset_uuid]
# Optionally create subjects using the person/face API to limit generation to their occurences
#,'subjects': []
}
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"Timeline UUID: {timeline_uuid}")Step 4: Retrieve and Display the Generated Timeline
You can retrieve the generated timeline and inspect its contents.
# 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 CMX3600 EDL
Once the timeline is ready, export it in CMX3600 EDL format.
# Step 5: Export the timeline to CMX3600 EDL format
export_format = 'cmx_3600' # Can also 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"interview_segments.{export_format}"
with open(output_filename, 'wb') as f:
f.write(export_response.content)
print(f"Timeline exported as '{output_filename}'.")