Python video editing library

LOVED BY DEVELOPERS

Used every day by developers at businesses large and small

Edit videos using the Shotstack Python video editing library. The fully managed API makes it quick and easy to build video based applications in days, not months.

Available as an open source Python package on PyPI or from GitHub. The demo repository is also available that shows you how to get started generating video using Python. The SDK and demo projects connect to the Shotstack video editing API - a cloud hosted, fully managed video editing platform.

Using Shotstack and Python you can cut and trim videos, stitch clips, add transitions, filters and effects. Merge videos, images and audio files using Python, the language you are familiar with.

Installation

The Shotstack Python video editing library is available for Python version 3 and can be installed from PyPI.

pip install shotstack_sdk

Usage

1. Import the library

Import the library in to your script.

import shotstack_sdk as shotstack
2. Import features

Import the editing features needed for your video edit.

from shotstack_sdk.api import edit_api
from shotstack_sdk.model.video_asset import VideoAsset
from shotstack_sdk.model.clip import Clip
from shotstack_sdk.model.track import Track
from shotstack_sdk.model.timeline import Timeline
from shotstack_sdk.model.output import Output
from shotstack_sdk.model.edit import Edit
3. Configure the API client

Configure the API client to use either the free developer sandbox or the v1 production API.

configuration = shotstack.Configuration(host = 'https://api.shotstack.io/stage')
4. Authenticate

Copy your API key for the sandbox or v1 environment from the dashboard and set the `DeveloperKey`. You should load the key using environment variables in a production environment.

configuration.api_key['DeveloperKey'] = 'H7jKyj90kd09lbLOF7J900jNbSWS67X87xs9j0cD'
5. Trim a video clip

A simple use case for Shotstack is trim a video. In the example below the first 3 seconds are trimmed from the start of the skater.hd.mp4 video. The video then plays for 8 seconds. The start point of 0 plays the click form the start of the timeline, i.e. immediately.

video_asset = VideoAsset(
src = "https://s3-ap-southeast-2.amazonaws.com/shotstack-assets/footage/skater.hd.mp4",
trim = 3.0
)

video_clip = Clip(
asset = video_asset,
start = 0.0,
length = 8.0
)
6. Add the clip to a track

Videos, images and text can be layered on top of each other using tracks. In this example there is only one video added to a single track.

track = Track(clips=[video_clip])
7. Add the track to the timeline

Tracks are added to the timeline. The timeline accepts an array of tracks as well as a background color.

timeline = Timeline(
background = "#000000",
tracks = [track]
)
8. Set the video output specification

The video output specification is defined including the resolution and format, such as an mp4 SD video.

output = Output(
format = "mp4",
resolution = "sd"
)
9. Create the video edit

Finally everything is put together as an edit, including the timeline and output format.

edit = Edit(
timeline = timeline,
output = output
)
10. POST the edit to the API

The SDK post_render method prepares the edit to the Shotstack JSON specification and posts it to the API. The response includes a render ID of the video which renders in the background.

api_response = api_instance.post_render(edit)
id = api_response['response']['id']
print(f">> python examples/status.py {id}")
10. Get the rendered video URL

Videos render in the background and can take a few seconds to minutes depending on the length. The status can be polled using the id returned in step 9.

api_response = api_instance.get_render(id)
status = api_response['response']['status']

if status == "done":
url = api_response['response']['url']
print(f">> Asset URL: {url}")

Output

After a few seconds the render will complete and the status request will return the URL of a video file.

EXPLORE

Check our Python video editing examples and demos on GitHub

DISCOVER

See what else the Shotstack API can do