FFHub SDK 示例

各语言调用 FFHub API 的代码示例。

FFHub 是 REST API。下面是常见语言的示例。

Node.js / JavaScript

const response = await fetch('https://api.ffhub.io/v1/tasks', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer YOUR_API_KEY',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    command: 'ffmpeg -i input.mp4 -c:v libx264 -crf 28 output.mp4'
  })
});
const { task_id } = await response.json();

// 轮询状态
const checkStatus = async (taskId) => {
  const res = await fetch(`https://api.ffhub.io/v1/tasks/${taskId}`, {
    headers: { 'Authorization': 'Bearer YOUR_API_KEY' }
  });
  return res.json();
};

Python

import requests

response = requests.post(
    'https://api.ffhub.io/v1/tasks',
    headers={'Authorization': 'Bearer YOUR_API_KEY'},
    json={'command': 'ffmpeg -i input.mp4 -c:v libx264 -crf 28 output.mp4'}
)
task_id = response.json()['task_id']

# 轮询状态
def check_status(task_id):
    res = requests.get(
        f'https://api.ffhub.io/v1/tasks/{task_id}',
        headers={'Authorization': 'Bearer YOUR_API_KEY'}
    )
    return res.json()

cURL

curl -X POST https://api.ffhub.io/v1/tasks \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"command": "ffmpeg -i input.mp4 -c:v libx264 output.mp4"}'

查询状态:

curl https://api.ffhub.io/v1/tasks/TASK_ID \
  -H "Authorization: Bearer YOUR_API_KEY"

Webhook 回调

不想轮询,可以传一个 webhook URL,任务完成后会收到通知:

const response = await fetch('https://api.ffhub.io/v1/tasks', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer YOUR_API_KEY',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    command: 'ffmpeg -i input.mp4 -c:v libx264 -crf 28 output.mp4',
    webhook: 'https://your-server.com/api/ffhub-callback'
  })
});

任务完成后,FFHub 会 POST 到你的 webhook,body 是任务结果:

{
  "task_id": "task_abc123",
  "status": "completed",
  "outputs": [
    {
      "filename": "output.mp4",
      "url": "https://storage.ffhub.io/outputs/task_abc123/output.mp4",
      "size": 10485760
    }
  ]
}

上传本地文件

源文件不在公网时,先上传,再把返回的 URL 用到 FFmpeg 命令里。

Step 1:上传

const formData = new FormData();
formData.append('file', fileBlob, 'input.mp4');

const uploadRes = await fetch('https://api.ffhub.io/v1/files', {
  method: 'POST',
  headers: { 'Authorization': 'Bearer YOUR_API_KEY' },
  body: formData
});
const { url } = await uploadRes.json();

Step 2:在命令里用这个 URL

const taskRes = await fetch('https://api.ffhub.io/v1/tasks', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer YOUR_API_KEY',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    command: `ffmpeg -i ${url} -c:v libx264 -crf 28 output.mp4`
  })
});
FFHub SDK 示例 — FFHub Docs