API DocsExamples

Examples

End-to-end client flows for multiple HTTP clients, not just curl.

JavaScript (Node)

import fs from 'node:fs'
import fetch from 'node-fetch'
 
const API = 'https://api.ffmpeg-api.com'
const KEY = process.env.FFMPEG_API_KEY
 
const headers = { Authorization: `Basic ${KEY}`, 'Content-Type': 'application/json' }
 
// 1) Get an upload URL
const fileRes = await fetch(`${API}/file`, { method: 'POST', headers, body: JSON.stringify({ file_name: 'input.mp4' }) })
const fileJson = await fileRes.json()
 
// 2) Upload
await fetch(fileJson.upload.url, { method: 'PUT', body: fs.readFileSync('./input.mp4') })
 
// 3) Process
const task = { task: { inputs: [{ file_path: fileJson.file.file_path }], outputs: [{ file: 'output.mp4', options: ['-crf', '23'] }] } }
const procRes = await fetch(`${API}/ffmpeg/process`, { method: 'POST', headers, body: JSON.stringify(task) })
const procJson = await procRes.json()
 
// 4) Download
const dl = await fetch(procJson.result[0].download_url)
fs.writeFileSync('output.mp4', Buffer.from(await dl.arrayBuffer()))

Python

import os, requests, json
 
API = 'https://api.ffmpeg-api.com'
KEY = os.environ['FFMPEG_API_KEY']
headers = { 'Authorization': f'Basic {KEY}', 'Content-Type': 'application/json' }
 
# 1) Get an upload URL
fres = requests.post(f'{API}/file', headers=headers, json={'file_name':'input.mp4'})
fjson = fres.json()
 
# 2) Upload
with open('input.mp4','rb') as f:
    requests.put(fjson['upload']['url'], data=f)
 
# 3) Process
task = { 'task': { 'inputs': [{ 'file_path': fjson['file']['file_path'] }], 'outputs': [{ 'file': 'output.mp4', 'options': ['-crf','23'] }] } }
pres = requests.post(f'{API}/ffmpeg/process', headers=headers, data=json.dumps(task))
pjson = pres.json()
 
# 4) Download
data = requests.get(pjson['result'][0]['download_url']).content
open('output.mp4','wb').write(data)

HTTPie

# Get an upload URL
http POST https://api.ffmpeg-api.com/file \
  Authorization:"Basic $KEY" file_name=input.mp4
 
# Upload
http PUT "$UPLOAD_URL" < input.mp4
 
# Process
http POST https://api.ffmpeg-api.com/ffmpeg/process \
  Authorization:"Basic $KEY" task:='{"inputs":[{"file_path":"<dir>/input.mp4"}],"outputs":[{"file":"output.mp4"}]}'

See also Quick Start and Processing.