基于 OpenAI Whisper 和 FFmpeg 的多媒体处理 API 服务,支持语音转文本和音视频转码功能。
# 1. 拉取最新镜像
docker pull docker.cnb.cool/l8ai/document/mediaconvert:latest
# 2. 创建必要的目录(数据持久化)
mkdir -p data logs models task_workspace
# 3. 创建环境配置文件
cat > .env << 'EOF'
# 数据库配置
DB_TYPE=sqlite
SQLITE_DB_NAME=data/MediaConvert.db
# S3/MinIO配置(下载服务)
AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
S3_ENDPOINT_URL=http://your-minio:9000
AWS_REGION=us-east-1
# S3/MinIO配置(上传服务)
UPLOAD_S3_ACCESS_KEY_ID=your_access_key
UPLOAD_S3_SECRET_ACCESS_KEY=your_secret_key
UPLOAD_S3_ENDPOINT_URL=http://your-minio:9000
UPLOAD_S3_REGION=us-east-1
UPLOAD_S3_BUCKET=ai-file
# 日志配置
LOG_LEVEL=20
LOG_FILES_DIR=/workspace/log_files
# GPU配置(如果有GPU)
WHISPER_DEVICE=cuda
FORCE_CPU=false
EOF
# 4. 启动服务(生产环境)
docker run -d \
--name mediaconvert-api \
--restart unless-stopped \
-p 33080:80 \
--env-file .env \
-v $(pwd)/data:/workspace/data \
-v $(pwd)/logs:/workspace/log_files \
-v $(pwd)/models:/workspace/models \
-v $(pwd)/task_workspace:/workspace/task_workspace \
docker.cnb.cool/l8ai/document/mediaconvert:latest
# 启动服务(GPU加速)
docker run -d \
--name mediaconvert-api \
--restart unless-stopped \
--gpus all \
-p 33080:80 \
--env-file .env \
-v $(pwd)/data:/workspace/data \
-v $(pwd)/logs:/workspace/log_files \
-v $(pwd)/models:/workspace/models \
-v $(pwd)/task_workspace:/workspace/task_workspace \
docker.cnb.cool/l8ai/document/mediaconvert:latest
# 1. 下载 docker-compose.yml
curl -O https://raw.githubusercontent.com/your-repo/mediaconvert-api/main/docker-compose.yml
# 2. 启动服务
docker-compose up -d
# 3. 查看服务状态
docker-compose ps
# 4. 查看日志
docker-compose logs -f
./ ├── data/ # 数据库文件(持久化) │ └── MediaConvert.db # SQLite数据库 ├── logs/ # 日志文件(持久化) │ ├── app.log # 应用日志 │ └── error.log # 错误日志 ├── models/ # 模型文件(持久化,可选) │ ├── whisper/ # Whisper模型 │ └── huggingface/ # HuggingFace模型 ├── task_workspace/ # 任务工作空间(持久化) │ └── task_*/ # 各任务的临时文件 ├── .env # 环境配置文件 └── docker-compose.yml # Docker Compose配置
# 使用国内镜像加速
pip install -r requirements.txt -i https://mirrors.cloud.tencent.com/pypi/simple
python start.py
服务将在 http://localhost:80 启动。
# 健康检查
curl "http://localhost:33080/api/tasks/health"
# 预期返回
{"status":"healthy","message":"Unified Task API is running"}
# Whisper服务检查
curl "http://localhost:33080/api/whisper/health"
# 预期返回
{"status":"healthy","message":"Whisper API is running"}
# 访问API文档
open http://localhost:33080/docs
POST /api/tasks/create
# S3存储方式(推荐)
curl -X POST "http://localhost:33080/api/tasks/create" \
-F "bucket=gaojiaqi" \
-F "file_path=audio.mp3" \
-F "task_type=transcribe" \
-F "language=zh" \
-F "word_timestamps=true" \
-F "priority=high"
# 本地文件上传
curl -X POST "http://localhost:33080/api/tasks/create" \
-F "file_upload=@audio.mp3" \
-F "task_type=transcribe" \
-F "language=zh" \
-F "priority=high"
# 高质量音频转码
curl -X POST "http://localhost:33080/api/tasks/create" \
-F "bucket=gaojiaqi" \
-F "file_path=video.mp4" \
-F "task_type=transcode" \
-F "transcode_type=audio_high" \
-F "output_format=mp3"
# H264视频转码
curl -X POST "http://localhost:33080/api/tasks/create" \
-F "bucket=gaojiaqi" \
-F "file_path=video.mp4" \
-F "task_type=transcode" \
-F "transcode_type=video_h264" \
-F "output_format=mp4" \
-F "resolution=1280x720"
# 根据任务ID查询
curl "http://localhost:33080/api/tasks/query/{task_id}"
# 根据bucket和文件路径查询
curl "http://localhost:33080/api/tasks/query?bucket=gaojiaqi&file_url=s3://gaojiaqi/audio.mp3"
POST /api/tasks/whisper/create
curl -X POST "http://localhost:33080/api/tasks/whisper/create" \
-F "bucket=gaojiaqi" \
-F "file_path=audio.mp3" \
-F "language=zh" \
-F "word_timestamps=true" \
-F "priority=high"
POST /api/tasks/transcode/create
curl -X POST "http://localhost:33080/api/tasks/transcode/create" \
-F "bucket=gaojiaqi" \
-F "file_path=video.mp4" \
-F "transcode_type=audio_high" \
-F "output_format=mp3"
{
"code": 200,
"message": "统一任务创建成功: 类型=transcribe, 文件来源=s3",
"data": {
"task_id": 19,
"status": "queued",
"task_type": "transcribe",
"file_name": "audio.mp3",
"file_url": "s3://gaojiaqi/audio.mp3",
"created_at": "2025-08-08T14:50:00.123456",
"priority": "high",
"result_url": "http://localhost:33080/api/tasks/query/19"
}
}
| 状态 | 说明 | 下一步 |
|---|---|---|
queued | 任务已排队,等待处理 | 继续等待 |
processing | 任务正在处理中 | 继续等待 |
completed | ✅ 任务已完成 | 获取结果 |
failed | ❌ 任务处理失败 | 检查错误信息 |
{
"code": 200,
"message": "查询成功",
"data": {
"task_id": 18,
"status": "completed",
"task_type": "transcribe",
"file_name": "audio.mp3",
"file_url": "s3://gaojiaqi/audio.mp3",
"ai_file_path": [
"gaojiaqi/audio/whisper/transcription_result.json",
"gaojiaqi/audio/whisper/transcription_text.txt",
"gaojiaqi/audio/whisper/transcription_timeline.txt",
"gaojiaqi/audio/whisper/transcription_subtitles.srt"
],
"s3_urls": [
"http://shenben.club:9000/ai-file/gaojiaqi/audio/whisper/transcription_result.json",
"http://shenben.club:9000/ai-file/gaojiaqi/audio/whisper/transcription_text.txt",
"http://shenben.club:9000/ai-file/gaojiaqi/audio/whisper/transcription_timeline.txt",
"http://shenben.club:9000/ai-file/gaojiaqi/audio/whisper/transcription_subtitles.srt"
],
"result": {
"text": "这是转录的完整文本内容...",
"language": "zh",
"segments": [
{
"id": 0,
"start": 0.0,
"end": 5.0,
"text": "这是第一段文本"
}
]
},
"task_processing_time": 100.98
}
}
主要配置通过环境变量或.env文件设置:
DB_TYPE=sqlite # 数据库类型
SQLITE_DB_NAME=data/MediaConvert.db # SQLite数据库文件路径
# 下载服务配置
AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
S3_ENDPOINT_URL=http://your-minio:9000
AWS_REGION=us-east-1
# 上传服务配置
UPLOAD_S3_ACCESS_KEY_ID=your_access_key
UPLOAD_S3_SECRET_ACCESS_KEY=your_secret_key
UPLOAD_S3_ENDPOINT_URL=http://your-minio:9000
UPLOAD_S3_REGION=us-east-1
UPLOAD_S3_BUCKET=ai-file
WHISPER_DEVICE=cuda # 设备类型:cuda/cpu
FORCE_CPU=false # 强制使用CPU
LOG_LEVEL=20 # 日志级别
MAX_FILE_SIZE=2147483648 # 最大文件大小(2GB)
详细配置选项请参考:config/settings.py
# 检查 CUDA 可用性
docker run --rm --gpus all docker.cnb.cool/l8ai/document/mediaconvert:latest \
python -c "import torch; print('CUDA available:', torch.cuda.is_available())"
# 检查GPU状态
nvidia-smi
| 模型 | 大小 | 速度 | 准确性 | 推荐用途 |
|---|---|---|---|---|
tiny | 39MB | 最快 | 较低 | 实时转录 |
base | 74MB | 快 | 中等 | 快速处理 |
small | 244MB | 中等 | 良好 | 平衡选择 |
medium | 769MB | 较慢 | 很好 | 高质量转录 |
large-v3 | 1550MB | 慢 | 最佳 | 最高质量(默认) |
# 调整并发任务数(通过环境变量)
MAX_CONCURRENT_TASKS=3
# 监控系统资源
docker stats mediaconvert-api
CUDA 内存不足
# 修改环境变量使用 CPU
echo "FORCE_CPU=true" >> .env
docker restart mediaconvert-api
容器启动失败
# 查看容器日志
docker logs mediaconvert-api
# 检查端口占用
netstat -tlnp | grep :80
# 检查磁盘空间
df -h
任务处理失败
# 查看任务详情
curl "http://localhost:33080/api/tasks/query/{task_id}"
# 查看应用日志
tail -f logs/app.log
S3连接问题
# 测试S3连接
curl "http://your-minio:9000"
# 验证S3配置
docker exec mediaconvert-api env | grep S3
# 查看容器状态
docker ps
docker stats mediaconvert-api
# 查看日志
docker logs -f mediaconvert-api
tail -f logs/app.log
# 重启服务
docker restart mediaconvert-api
# 更新镜像
docker pull docker.cnb.cool/l8ai/document/mediaconvert:latest
docker-compose down && docker-compose up -d
启动服务后,访问以下地址查看交互式API文档:
http://localhost:33080/docshttp://localhost:33080/redocai-file bucket/ ├── {原始bucket}/ │ └── {文件路径(去掉后缀)}/ │ ├── whisper/ # 语音转文本结果 │ │ ├── transcription_result.json │ │ ├── transcription_text.txt │ │ ├── transcription_timeline.txt │ │ └── transcription_subtitles.srt │ └── ffmpeg/ # 转码结果 │ └── output_file.mp3
python main.py
# 移动端转码
curl -X POST "http://localhost:33080/api/tasks/create" \
-H "Content-Type: application/json" \
-d '{
"file_url": "s3://gaojiaqi/20250710133938-象山教科研中心智能体培训-视频-1-说话人.mp4",
"task_type": "transcode",
"transcode_type": "mobile_high"
}'
# PC端转码
curl -X POST "http://localhost:33080/api/tasks/create" \
-H "Content-Type: application/json" \
-d '{
"file_url": "s3://gaojiaqi/20250710133938-象山教科研中心智能体培训-视频-1-说话人.mp4",
"task_type": "transcode",
"transcode_type": "pc_high"
}'
# 4K转码
curl -X POST "http://localhost:33080/api/tasks/create" \
-H "Content-Type: application/json" \
-d '{
"file_url": "s3://gaojiaqi/20250710133938-象山教科研中心智能体培训-视频-1-说话人.mp4",
"task_type": "transcode",
"transcode_type": "4k_standard"
}'
curl -X POST "http://localhost:33080/api/tasks/create" \
-H "Content-Type: application/json" \
-d '{
"file_url": "s3://gaojiaqi/20250710133938-象山教科研中心智能体培训-视频-1-说话人.mp4",
"task_type": "transcribe",
"language": "zh"
}'
curl "http://localhost:33080/api/tasks/query/{task_id}"
测试环境:
测试结果:
移动端视频转码 ✅ 成功
PC端视频转码 ✅ 成功
4K高清视频转码 ✅ 成功
音频文本转录 ✅ 成功
验证要点:
docker.cnb.cool/l8ai/document/mediaconvert:latestApache License 2.0
欢迎提交 Issue 和 Pull Request!
# 克隆仓库
git clone https://github.com/your-repo/mediaconvert-api.git
cd mediaconvert-api
# 安装依赖
pip install -r requirements.txt -i https://mirrors.cloud.tencent.com/pypi/simple
# 启动开发服务
python start.py
feat: 添加新功能fix: 修复问题docs: 更新文档perf: 性能优化