Shotstack Python SDK
Python SDK for the Shotstack Python video editor and cloud video editing API.
Shotstack is a cloud based video editing platform that enables the editing of videos using code.
The platform uses an API and a JSON format for specifying how videos should be edited and what assets and titles should be used.
A server based render farm takes care of rendering the videos allowing multiple videos to be created simultaneously.
For examples of how to use the SDK to create videos using code checkout the Python demo repo: https://github.com/shotstack/python-demos
Contents
Using the Python SDK
Installation
pip install shotstack-sdk
Video Editing
The Shotstack SDK enables programmatic video editing via the Edit API render endpoint. Prepare JSON edits using the
provided schema classes and POST to the API for rendering.
Video Editing Example
The example below trims the start of a video clip and plays it for 8 seconds. The edit is prepared using the SDK models
and then sent to the API for rendering.
import shotstack_sdk as shotstack
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
host = 'https://api.shotstack.io/stage'
configuration = shotstack.Configuration(host = host)
configuration.api_key['DeveloperKey'] = 'H7jKyj90kd09lbLOF7J900jNbSWS67X87xs9j0cD'
with shotstack.ApiClient(configuration) as api_client:
api_instance = edit_api.EditApi(api_client)
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
)
track = Track(clips=[video_clip])
timeline = Timeline(tracks=[track])
timeline['tracks'] = [another_track]
output = Output(
format = 'mp4',
resolution = 'sd'
)
edit = Edit(
timeline = timeline,
output = output
)
try:
api_response = api_instance.post_render(edit)
print(f"{api_response['response']['id']}")
except Exception as e:
print(f"Unable to resolve API call: {e}")
Status Check Example
The example request below can be called a few seconds after the render above is posted. It will return the status of
the render, which can take several seconds to process.
import shotstack_sdk as shotstack
from shotstack_sdk.api import edit_api
host = 'https://api.shotstack.io/stage'
configuration = shotstack.Configuration(host = host)
configuration.api_key['DeveloperKey'] = 'H7jKyj90kd09lbLOF7J900jNbSWS67X87xs9j0cD'
with shotstack.ApiClient(configuration) as api_client:
api_instance = edit_api.EditApi(api_client)
id = '75143ec6-4b72-46f8-a67a-fd7284546935'
try:
api_response = api_instance.get_render(id, data=False, merged=True)
status = api_response['response']['status']
if status == 'done':
print(f"{api_response['response']['url']}")
except Exception as e:
print(f"Unable to resolve API call: {e}")
Save a Template Example
The example below uses the Edit we create in the Video Editing Example and saves it as a
template. The template can be rendered at a later date and can include placeholders. Placeholders can be replaced
when rendered using merge fields.
This example uses a placeholder for the video src (URL), trim (TRIM), and length (LENGTH) to allow you to trim any video
using a template.
import shotstack_sdk as shotstack
import os
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
from shotstack_sdk.model.template import Template
host = "https://api.shotstack.io/stage"
configuration = shotstack.Configuration(host = host)
configuration.api_key['DeveloperKey'] = os.getenv("SHOTSTACK_KEY")
with shotstack.ApiClient(configuration) as api_client:
api_instance = edit_api.EditApi(api_client)
video_asset = VideoAsset(
src = '{{ URL }}',
trim = '{{ TRIM }}'
)
video_clip = Clip(
asset = video_asset,
start = 0.0,
length = '{{ LENGTH }}'
)
track = Track(clips=[video_clip])
timeline = Timeline(
background = '#000000',
tracks = [track]
)
output = Output(
format = 'mp4',
resolution = 'sd'
)
edit = Edit(
timeline = timeline,
output = output
)
template = Template(
name = 'Trim Template',
template = edit
)
try:
api_response = api_instance.post_template(template)
print(f"{api_response['response']['id']}")
except Exception as e:
print(f"Unable to resolve API call: {e}")
Render a Template Example
The example below renders the template we created in the previous example and includes merge fields that will replace
the placeholders. Once submitted use the returned render ID and call the Status Check Example
to get the render progress.