Skip to content

Instantly share code, notes, and snippets.

@derkzomer
Created October 26, 2023 07:40
Show Gist options
  • Save derkzomer/72997163df3865f4d181f42fb66bc66d to your computer and use it in GitHub Desktop.
Save derkzomer/72997163df3865f4d181f42fb66bc66d to your computer and use it in GitHub Desktop.
import time
import requests
import json
# Define the API key and the headers for POST and GET requests
api_key = 'xxxxxx'
post_headers = {'Content-Type': 'application/json', 'x-api-key': api_key}
get_headers = {'Accept': 'application/json', 'x-api-key': api_key}
# Array of URLs to process
urls = ["https://github.com/shotstack/test-media/raw/main/captioning/scott-ko.mp4", ...]
# Function to POST the request and get the ID
def post_request(url):
post_data = {
"url": url,
"outputs": {
"renditions": [
{
"fps": 25
}
]
}
}
response = requests.post('https://api.shotstack.io/ingest/v1/sources', headers=post_headers, data=json.dumps(post_data))
return response.json()['data']['id']
# Function to GET the status of the request
def get_request(id):
while True:
response = requests.get(f'https://api.shotstack.io/ingest/stage/sources/{id}', headers=get_headers)
data = response.json()
if data['data']['attributes']['status'] == 'ready':
return data
time.sleep(5) # Wait for 5 seconds before polling again
ids = []
# Loop over each URL
for url in urls:
id = post_request(url)
ids.append(id)
time.sleep(0.1) # Wait for 100ms before sending the next request
results = {}
# Loop over each ID
for id in ids:
result = get_request(id)
# Store the original URL and the URL of the rendition in the results dictionary
rendition_url = result['data']['attributes']['outputs']['renditions'][0]['url']
original_url = result['data']['attributes']['input']
results[original_url] = rendition_url
print(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment