Effortlessly Split Videos with Scene Detection Actions

26 Apr 2025
Effortlessly Split Videos with Scene Detection Actions

In today's digital landscape, video content is more prevalent than ever, and the ability to process and manipulate video efficiently is crucial for developers and content creators. The Cog Scenedetect API provides a powerful solution for splitting videos into individual scenes using advanced scene detection techniques. By leveraging the adaptive-threshold method, developers can automate the task of identifying and segmenting video clips, saving time and effort while enhancing productivity.

This service is particularly beneficial for use cases such as editing promotional videos, creating highlight reels, or even producing educational content where specific segments need to be isolated. With Cog Scenedetect, developers can focus on crafting their narratives without getting bogged down in the tedious manual process of video editing.

Prerequisites

To utilize the Cog Scenedetect API, you will need an API key and a basic understanding of how to make API calls.

Split Video by Scene Detection

The Split Video by Scene Detection action allows you to divide a video into its individual clips by detecting cuts. This is accomplished using an adaptive-threshold method that identifies scene changes, making it easier for developers to manage video content effectively.

Input Requirements

The input for this action requires a CompositeRequest object, which must include the following:

  • video (string): The URI of the video file to be split into segments. The video must be accessible through the provided URL (e.g., https://replicate.delivery/pbxt/KPfTDFloQdPnw1hMkHbZjMfLNl9xWjiuS63IDeMwMEVt9q89/mitten-astronaut.mp4).
  • lumaOnly (boolean, optional): If set to true, only the luma (brightness) channel is used for scene detection. The default is false.
  • minSceneLength (integer, optional): The minimum length in seconds for each detected scene. The default value is 15 seconds.
  • adaptiveThreshold (number, optional): This threshold value controls the sensitivity of scene changes. The default is 3.

Expected Output

Upon successful execution, the action returns a list of segmented video clips in the form of URIs, allowing developers to access each individual scene directly. For example, the output might look like this:

{
  "videos": [
    "https://assets.cognitiveactions.com/invocations/ab1e7f33-88b4-496a-b03e-d4c9b57658fb/08f34270-f53d-4f98-8cc0-48dc78bde744.mp4",
    "https://assets.cognitiveactions.com/invocations/ab1e7f33-88b4-496a-b03e-d4c9b57658fb/dbd224e1-7e98-4311-9da4-668fec2fa9eb.mp4",
    // More video URIs...
  ]
}

Use Cases for this Action

  • Video Editing: Automate the process of splitting long videos into manageable clips, making it easier to edit and rearrange content.
  • Highlight Reels: Quickly create highlight reels by isolating significant scenes from recorded events or performances.
  • Content Analysis: Analyze specific segments of a video for insights, such as viewer engagement or emotional responses, by focusing on distinct scenes.
import requests
import json

# Replace with your actual Cognitive Actions API key and endpoint
# Ensure your environment securely handles the API key
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
# This endpoint URL is hypothetical and should be documented for users
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute"

action_id = "4b293359-7559-42b4-8cfd-453c3380320e" # Action ID for: Split Video by Scene Detection

# Construct the exact input payload based on the action's requirements
# This example uses the predefined example_input for this action:
payload = {
  "video": "https://replicate.delivery/pbxt/KPfTDFloQdPnw1hMkHbZjMfLNl9xWjiuS63IDeMwMEVt9q89/mitten-astronaut.mp4"
}

headers = {
    "Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
    "Content-Type": "application/json",
    # Add any other required headers for the Cognitive Actions API
}

# Prepare the request body for the hypothetical execution endpoint
request_body = {
    "action_id": action_id,
    "inputs": payload
}

print(f"--- Calling Cognitive Action: {action.name or action_id} ---")
print(f"Endpoint: {COGNITIVE_ACTIONS_EXECUTE_URL}")
print(f"Action ID: {action_id}")
print("Payload being sent:")
print(json.dumps(request_body, indent=2))
print("------------------------------------------------")

try:
    response = requests.post(
        COGNITIVE_ACTIONS_EXECUTE_URL,
        headers=headers,
        json=request_body
    )
    response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)

    result = response.json()
    print("Action executed successfully. Result:")
    print(json.dumps(result, indent=2))

except requests.exceptions.RequestException as e:
    print(f"Error executing action {action_id}: {e}")
    if e.response is not None:
        print(f"Response status: {e.response.status_code}")
        try:
            print(f"Response body: {e.response.json()}")
        except json.JSONDecodeError:
            print(f"Response body (non-JSON): {e.response.text}")
    print("------------------------------------------------")

Conclusion

The Cog Scenedetect API's ability to split videos by scene detection streamlines the video editing process, providing developers with an efficient tool to manage video content. By automating scene detection, you can save time and enhance your workflow, whether you're creating promotional content, educational materials, or entertainment.

To get started, integrate the Split Video by Scene Detection action into your projects, and unlock the potential of automated video processing.