Seamlessly Split Videos for Fine-Tuning with lucataco/mochi1-video-split Actions

In today's digital landscape, video content is more prevalent than ever, especially in fields like machine learning and AI. The lucataco/mochi1-video-split API offers a powerful Cognitive Action that allows developers to segment videos efficiently, providing customizable options for length, resolution, and frame rate. This pre-built action simplifies the process of preparing video data for tasks such as LoRA fine-tuning, making it an invaluable tool for developers.
Prerequisites
Before diving into the implementation, there are a few general requirements to use the Cognitive Actions:
- API Key: Ensure you have an API key for accessing the Cognitive Actions platform.
- Setup: Familiarize yourself with how to authenticate requests. Typically, this involves passing your API key in the request headers.
Cognitive Actions Overview
Split Video into Segments
The Split Video into Segments action enables you to segment videos for various applications, particularly for training AI models. This action allows for customization in segment duration, resolution, and frame rate, ensuring that your video data is tailored to your project's specific needs.
- Category: Video Processing
Input
The required and optional fields for this action are defined in the input schema. Below is a breakdown:
- inputVideo (required): URI of the input video file in MP4 or MOV format.
- Example:
"https://replicate.delivery/pbxt/MHgmDF1AYMqghD5jN7ijBDHs2fYmjzUJrDPID2H2mIQOhegv/heygen-demo.mp4"
- Example:
- targetWidth (optional): Width in pixels for each video segment (default: 848, range: 320-1920).
- Example:
960
- Example:
- targetHeight (optional): Height in pixels for each video segment (default: 480, range: 240-1080).
- Example:
544
- Example:
- createCaptions (optional): Boolean flag to indicate if empty caption files should be generated for each segment (default: false).
- Example:
false
- Example:
- targetDuration (optional): Duration in seconds for each segment (default: 2, range: 1-10).
- Example:
2
- Example:
- targetFramesPerSecond (optional): Frames per second for each video segment (default: 30, range: 1-60).
- Example:
30
- Example:
Here’s an example of the JSON payload you would use to invoke the action:
{
"inputVideo": "https://replicate.delivery/pbxt/MHgmDF1AYMqghD5jN7ijBDHs2fYmjzUJrDPID2H2mIQOhegv/heygen-demo.mp4",
"targetWidth": 960,
"targetHeight": 544,
"createCaptions": false,
"targetDuration": 2,
"targetFramesPerSecond": 30
}
Output
When the action is executed successfully, it returns a URL pointing to a zipped file containing the segmented video segments. Here’s an example output:
"https://assets.cognitiveactions.com/invocations/f7a87944-89ac-41f2-850a-8defc3fd0166/2acb8d88-ccae-421f-a91e-69c79697a295.zip"
The output may vary depending on the specifics of the input and the processing results.
Conceptual Usage Example (Python)
Below is a conceptual Python code snippet demonstrating how a developer might call the Cognitive Actions endpoint to split a video into segments:
import requests
import json
# Replace with your Cognitive Actions API key and endpoint
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute" # Hypothetical endpoint
action_id = "ffbdb801-b41b-4f24-835e-9a3f069a0e6d" # Action ID for Split Video into Segments
# Construct the input payload based on the action's requirements
payload = {
"inputVideo": "https://replicate.delivery/pbxt/MHgmDF1AYMqghD5jN7ijBDHs2fYmjzUJrDPID2H2mIQOhegv/heygen-demo.mp4",
"targetWidth": 960,
"targetHeight": 544,
"createCaptions": false,
"targetDuration": 2,
"targetFramesPerSecond": 30
}
headers = {
"Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
"Content-Type": "application/json"
}
try:
response = requests.post(
COGNITIVE_ACTIONS_EXECUTE_URL,
headers=headers,
json={"action_id": action_id, "inputs": payload} # Hypothetical structure
)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
result = response.json()
print("Action executed successfully:")
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: {e.response.text}")
In this code snippet:
- Replace
YOUR_COGNITIVE_ACTIONS_API_KEYwith your actual API key. - The
action_idis set to the ID for the "Split Video into Segments" action. - The
payloadis constructed using the example input, and sent in a POST request to the hypothetical Cognitive Actions execution endpoint.
Conclusion
The Split Video into Segments action from the lucataco/mochi1-video-split API provides developers with a robust tool for video processing, particularly useful for training machine learning models. By customizing aspects like segment duration and resolution, developers can prepare high-quality video datasets with ease.
Consider exploring other potential use cases, such as automating video content creation or enhancing video analysis workflows. With these powerful Cognitive Actions at your disposal, the possibilities are endless!