Skip to content

Instantly share code, notes, and snippets.

@mapes911
Last active June 19, 2018 17:38
Show Gist options
  • Save mapes911/31dc8543d731c4952d6ececce580a4fd to your computer and use it in GitHub Desktop.
Save mapes911/31dc8543d731c4952d6ececce580a4fd to your computer and use it in GitHub Desktop.
Simple method to submit a transcoding job to the Coconut.co video transcoding service.
import requests
from requests.auth import HTTPBasicAuth
COCONUT_API_KEY = 'asdfasdf'
AWS_ACCESS_KEY = 'AWSKEY'
AWS_SECRET_KEY = 'AWSSECRET'
AWS_OUTPUT_BUCKET = 'transcoded-videos'
def create_transcoding_job(source, webhook, outputs):
"""
source - original url of media to be transcoded
webhook - url for post-processing webhook
outputs - dictionary of requested outputs.
eg.
base_s3 = f's3://{AWS_ACCESS_KEY}:{AWS_SECRET_KEY}@{AWS_OUTPUT_BUCKET}'
{
'mp4:720p': f'{base_s3}/transcoded_video.mp4',
'jpg:600x': f'{base_s3}/screenshot.jpg, number=1'
}
"""
headers = {'user-agent': 'Coconut (Python 3.6)'}
config = []
config.append(f'set source = {source}')
config.append(f'set webhook = {webhook}')
unsorted_outputs = []
for format, cdn in outputs.items():
unsorted_outputs.append(f'-> {format} = {cdn}')
config += sorted(unsorted_outputs)
post_config = '\n'.join(config)
r = requests.post(
'https://api.coconut.co/v1/job',
data=post_config,
headers=headers,
auth=HTTPBasicAuth(COCONUT_API_KEY, '')
)
print(r.status_code)
print(r.headers)
print(r.text)
return r.json()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment