api.video
SearchCtrl K

Features

Developers

How to create a video clip with api.video

January 20, 2022 - Erikka Innes in video create, Python

Sometimes you just want to upload a smaller piece of a video you have, but you don't want to take the time to edit it with FFMPEG or FinalCut Pro. If you just need a shorter clip and you know the start and end timecode, you can use api.video's clip feature. Today we'll walk through how to upload a clip from a video you have on your computer and how to take a clip from an online, publicly available mp4 link or a video you have stored with api.video.

# Pre-requisites

For this tutorial, you will need:

Installation

Install api.video's Python client with pip install api.video

Code sample for uploading a clip from a video on your computer

Our code sample today is this:

import apivideo
from apivideo.apis import VideosApi
from apivideo.model.bad_request import BadRequest
from pprint import pprint

client = apivideo.AuthenticatedApiClient("your API key here")

client.connect()

videos_api = VideosApi(client)

video_create_payload = {
    "title": "My sample clip from a new source",
    "description": "test",
    "clip": {
        "start_timecode": "1",
        "end_timecode": "2",
    },
}

# Create the container for your video and print the response
response = videos_api.create(video_create_payload)
print("Video Container", response)

# Retrieve the video ID, you can upload once to a video ID
video_id = response["video_id"]

# Prepare the file you want to upload. Place the file in the same folder as your code.
file = open("test_vid45.mp4", "rb")

# Upload your video. 
video_response = videos_api.upload(video_id, file)

print("Uploaded Video", video_response)

Video clip code walkthrough

What we'll be doing is a normal video upload. If you're new to uploads, with api.video it's a two step process. First you create a video container with all your metadata, and in this case, you'll also include the information needed to clip your video. Then you retrieve the video ID and use that to upload your video into the container. In this case, you will upload just the video clip into the container. When you're done, you'll have a new, shortened clip to display.

  1. First initialize the client and VideoApi and connect.

`client = apivideo.AuthenticatedApiClient("your API key here")

client.connect()

videos_api = VideosApi(client)`

  1. Prepare the video payload with metadata that you'll use as the container.

video_create_payload = { "title": "My sample clip from a new source", "description": "test", "clip": { "start_timecode": "1", "end_timecode": "2", }, }

  1. Send a request with your payload to create the new video container, and retrieve the video ID.

` response = videos_api.create(video_create_payload) print("Video Container", response)

video_id = response"video_id" `

  1. Now it's time to upload your video. Open your video file in binary to get it ready to send, then send a request to upload it.

` file = open("test_vid45.mp4", "rb") video_response = videos_api.upload(video_id, file)

print("Uploaded Video", video_response)`

You can check your dashboard to see that you've successfully uploaded a short video clip!

Upload a clip from a link or video source

To take a clip from an online .mp4 link or a video source you have with api.video is easier than the process we just walked through. All you need to do is create a video and the clip will be created for you. If you use a link, make sure it's a .mp4 link. You can't, for example, grab a YouTube link and get a clip from that.

If you're uploading a clip from an online .mp4 source, you specify the complete link, as a string for the source parameter. And if you're uploading a clip from a video you have stored with api.video, you can specify the video ID for the video you want to grab a clip from.

In the example, I've put an .mp4 link in for the source. You don't need to do the second step that we did in the earlier walkthrough. Everything is handled with one call to the api.video API.

import apivideo
from apivideo.apis import VideosApi
from apivideo.model.bad_request import BadRequest
from pprint import pprint

client = apivideo.AuthenticatedApiClient("your api key here")

client.connect()

videos_api = VideosApi(client)

video_create_payload = {
    "title": "source_download_with_clip",
    "description": "source_download_with_clip",
    "source": "http://techslides.com/demos/sample-videos/small.mp4",
    "public": True,
    "panoramic": False,
    "clip": {
        "start_timecode": "00:00:02",
        "end_timecode": "00:00:05"
    }
}

# Create the container for your video and print the response
response = videos_api.create(video_create_payload)
print("Video Container", response)

Conclusion

Video clipping is great in situations where you wanted to grab one or more clips from a longer video, or you just wanted to edit a bit of content off the front and end of your video clip. Now, it's possible to do with the api.video API. Find all the information in our Docs (in the "Body params" section) or ask questions in our Community. Happy building!

Erikka Innes

Developer Evangelist

Create your free account

Start building with video now