跳转到内容

Python 客户端

使用 video-to-text-python 上传媒体、创建转录任务并轮询结果。

Python 客户端封装了公开上传接口和任务接口,并提供同步与异步客户端。

Terminal window
pip install video-to-text-python
import os
from video_to_text import VideoToTextClient
client = VideoToTextClient(
api_key=os.environ["VTT_API_KEY"],
)
from pathlib import Path
task = client.transcriptions.transcribe_file(
Path("meeting.mp4"),
language="Auto",
timestamp_mode="CHUNK",
transcription_mode="balanced",
timeout=600,
)
print(task.full_text)

当准确性比学分成本更重要时,可以使用 precision

upload = client.uploads.create({
"filename": "meeting.mp4",
"mimetype": "video/mp4",
})
content = Path("meeting.mp4").read_bytes()
client.uploads.put(upload.upload_url, content, mimetype="video/mp4")
asset = client.uploads.complete(upload.upload_id, {
"fileKey": upload.file_key,
"fileUrl": upload.file_url,
"filename": "meeting.mp4",
"mimetype": "video/mp4",
"fileSize": len(content),
})
created = client.tasks.create({
"assetId": asset.asset_id,
"language": "Auto",
"timestampMode": "CHUNK",
"transcriptionMode": "balanced",
})
result = client.tasks.wait(created.transcript_id)
print(result.full_text)
from video_to_text import AsyncVideoToTextClient
async with AsyncVideoToTextClient(
api_key=os.environ["VTT_API_KEY"],
) as client:
task = await client.transcriptions.transcribe_file(
Path("meeting.mp4"),
transcription_mode="balanced",
)
print(task.full_text)

tasks.wait() 会在任务进入 SUCCEEDED 时返回。如果任务进入 FAILEDCANCELED,客户端会抛出 APIError,并优先使用 show_error 作为错误详情。

任务详情包含 full_textchunkswordssource_duration_msresult_languagetranscription_modebilled_credits

from video_to_text import APIError, RateLimitError, UploadError
try:
task = client.transcriptions.transcribe_file(
Path("meeting.mp4"),
transcription_mode="balanced",
)
except RateLimitError:
print("Please retry later.")
except UploadError:
print("The media upload did not complete.")
except APIError as error:
print(error.error_code, error.problem.detail)

源代码、问题反馈和贡献请查看 video-to-text-python GitHub 仓库。最新版本可从 PyPI 上的 video-to-text-python 安装。