Tutorial

How to Detect Shot Changes in Videos Using Python

Learn how to detect shot changes in videos using Python and the Eden AI API. This guide walks you through launching and retrieving asynchronous video analysis jobs with real code examples.

How to Detect Shot Changes in Videos Using Python
TABLE OF CONTENTS

Detecting shot changes is essential in video processing, where you need to identify when one scene changes to another.

For example, when a new camera angle is used or a scene transitions, this is considered a shot change. Detecting these changes helps with tasks like video summarization, scene segmentation, and more.

In this tutorial, we will explain how to implement shot change detection using Python and the Eden AI API. This API allows us to run shot change detection asynchronously with minimal code.

What is Video Shot Change Detection?

Video Shot Change Detection annotates a video with segments that are generated by detecting abrupt shot changes within the video.

This process helps break the video into distinct sections, making it easier to analyze and edit.

How to Detect Shot Changes in Videos

Get Access to Eden AI API

1. Sign up: Visit Eden AI and create an account for free to get started. Once registered, navigate to the API section to find your personal API key which gives you access to a wide range of AI services, including Shot Change Detection.

2. Navigate to Video Technologies – Log in and head to the Video Technologies section.

3. Select Shot Change Detection– From there, select the Shot Change Detection option, or dive into more advanced features based on your requirements.

Live Test Models on Eden AI

Eden AI provides a platform where you can test different AI models before integrating them, allowing you to evaluate and select the provider that best aligns with your needs.

Implementing Shot Change Detection in Python

Step 1: Install Python Requests Module

Before implementing the solution, ensure you have the Requests library installed in Python. If you haven’t already installed it, you can do so by running:


pip install requests

This module is required to send HTTP requests to the Eden AI API.

Code Explanation

We will now break down the code into two parts: POST and GET.

POST - Launch the Job

The first step involves launching the shot change detection job. You use a POST request to submit a video to Eden AI for processing. When you submit the job, you specify which AI provider you want to use (for example, Google) and provide the video URL. Once the job is submitted, Eden AI processes the video and analyzes the shot changes asynchronously.

Retrieve Job Results

After the processing is complete, you can retrieve the results using a GET request. In this phase, you query the Eden AI API with the public_id provided when the job was submitted. This will give you the results of the shot change detection, such as timestamps where shot changes occurred.

POST Phase (Launch Job)

This part of the code sends a request to Eden AI to start the shot change detection job.


import requests

# Replace with your actual API Key
headers = {"Authorization": "Bearer {API_KEY}"}

# Replace with the actual URL of your video
json_payload = {
    "providers": "google", 
    "file_url": "🔗 URL of your video"
}

# The URL for launching the shot change detection job
url = "https://api.edenai.run/v2/video/shot_change_detection_async"

# Send the POST request to initiate the job
response = requests.post(url, json=json_payload, headers=headers)

# Get the response and display it
result = response.json()
print(result)

Explanation of the POST Phase:

  • URL: We are using the endpoint https://api.edenai.run/v2/video/shot_change_detection_async to launch the shot change detection job.
  • Authorization Header: The Authorization header contains your API key, which is required for authentication with Eden AI’s API.
  • json_payload: Contains the data required to launch the job:
    • "providers": "google" specifies that we want to use Google's AI for this task.
    • "file_url": "🔗 URL of your video" is where you provide the URL of the video you want to process.

Once this request is sent, the API processes the video asynchronously, and you will receive a public_id to track the job.

GET Phase (Retrieve Job Results)

Once the job is complete, you can retrieve the results using the public_id provided in the response.


import requests

# Replace with your actual API Key
headers = {"Authorization": "Bearer {API_KEY}"}

# Replace with the actual public_id from the POST request
public_id = "849cec9a-381a-4ed4-9a75-c76053e9c01c"

# The URL for retrieving job results
url = f"https://api.edenai.run/v2/video/shot_change_detection_async/{public_id}/"

# Send the GET request to retrieve the results
response = requests.get(url, headers=headers)

# Get the response and display it
result = response.json()
print(result)

Explanation of the GET Phase:

  • URL: Here, we are using the endpoint https://api.edenai.run/v2/video/shot_change_detection_async/{public_id}/ where {public_id} is the identifier received from the POST request.
  • Authorization Header: As in the POST request, you need to pass your API key for authentication.
  • GET Request: The GET request retrieves the results of the shot change detection. The results are returned in a JSON format that contains the details of detected shot changes.

Interpreting the Results

When the job is completed, the results from the GET request will contain important information, such as:

  • shots: A list of detected shots, typically represented by timestamps indicating where a shot change occurs.

  • status: The status of the job, indicating whether it is completed or still processing.

  • error: If there was an error during the process, it will provide details on what went wrong.

Example output:


{
  "status": "completed",
  "shots": [
    {"start": 12.5, "end": 15.0},
    {"start": 23.0, "end": 27.0},
    {"start": 45.0, "end": 48.0}
  ]
}

shots: These are the shot change timestamps. For example, the first shot change starts at 12.5s and ends at 15.0s.

Managing Video Shot Change Detection Jobs

For better management of video shot change detection tasks, Eden AI provides additional optional steps:

  1. Video Shot Change Detection - List Jobs
    GET Request:
    https://api.edenai.run/v2/video/shot_change_detection_async/ This step allows you to retrieve a list of all jobs launched for shot change detection. Using the unique ID of each job, you can then check its status and results. You can check the full documentation here.
  1. Video Shot Change Detection - Delete Jobs
    DELETE Request:
    https://api.edenai.run/v2/video/shot_change_detection_async/This endpoint allows you to delete jobs, providing an easy way to manage and clean up completed or unnecessary tasks. You can check the full documentation here.

By leveraging these optional steps, you can more effectively manage and track your video shot change detection jobs.

Why Eden AI is the Best Tool for Shot Change Detection

Eden AI offers a robust shot change detection solution with several advantages.

Multiple Provider Options

Eden AI offers a selection of AI providers, giving you the flexibility to detect deepfakes using various models.

High Accuracy

The shot change detection models used by Eden AI are trained on vast datasets and designed to deliver reliable results.

Ease of Integration

Eden AI simplifies the integration of shot change detection into your applications with clear and straightforward Python code examples.

Security and Trust

The Eden AI platform prioritizes data security and privacy, ensuring that your image data is handled securely.

Conclusion

In this guide, we’ve learned how to detect shot changes in videos using Python and Eden AI's Shot Change Detection API.

By utilizing the two-phase approach of POST and GET requests, we can easily automate the process of shot change detection without the complexity of managing different AI models or platforms.

With Eden AI’s easy-to-integrate API, you can streamline video processing workflows and focus more on creative and impactful tasks.

Start Your AI Journey Today

  • Access 100+ AI APIs in a single platform.
  • Compare and deploy AI models effortlessly.
  • Pay-as-you-go with no upfront fees.
Start building FREE

Related Posts

Try Eden AI for free.

You can directly start building now. If you have any questions, feel free to chat with us!

Get startedContact sales
X

Start Your AI Journey Today

Sign up now with free credits to explore 100+ AI APIs.
Get my FREE credits now