音频转码插件
Transcode 插件提供基于 FFmpeg 的音视频转码功能,支持通过规则自动触发(流发布时按正则匹配)和 HTTP API 手动控制两种方式管理转码任务。
features = ["ffmpeg_transcode"]transcode: enable: true ffmpeg_path: "ffmpeg" # FFmpeg 二进制路径 mode: "auto" # 模式:auto / process local_rtmp: "rtmp://127.0.0.1/live" # 本地 RTMP 地址(作为 FFmpeg 输入源) onpub: # 自动转码规则(发布时触发) "^live/(.+)$": # 正则匹配流路径 outputs: - target: "rtmp://cdn.example.com/live/$1" # 目标地址(支持正则替换) options: video_codec: "libx264" audio_codec: "aac" video_bitrate: 2000 audio_bitrate: 128 resolution: "1280x720" frame_rate: 30| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
enable | bool | false | 是否启用插件 |
mode | string | auto | FFmpeg 后端模式 |
ffmpeg_path | string | ffmpeg | FFmpeg 二进制路径 |
local_rtmp | string | rtmp://127.0.0.1/live | 本地 RTMP 输入地址 |
onpub | map | {} | 自动转码规则 |
转码选项(options)
Section titled “转码选项(options)”| 字段 | 类型 | 说明 |
|---|---|---|
video_codec | string | 视频编码器(如 libx264、libx265、copy) |
audio_codec | string | 音频编码器(如 aac、libopus、copy) |
video_bitrate | int | 视频码率(kbps) |
audio_bitrate | int | 音频码率(kbps) |
resolution | string | 分辨率(如 1280x720) |
frame_rate | float | 帧率 |
自动转码触发
Section titled “自动转码触发”当有新流发布时,Transcode 插件会监听 StreamEvent::Created 事件,将流路径与 onpub 中的正则规则逐一匹配。匹配成功后,自动为每个 outputs 条目启动一个 FFmpeg 转码进程。
当源流销毁时(StreamEvent::Disposed),插件自动终止该流关联的所有转码任务。
WebRTC → RTMP 转推
Section titled “WebRTC → RTMP 转推”WebRTC 推流使用 Opus 音频编码,但 RTMP 通常要求 AAC。通过转码规则自动完成音频格式转换:
onpub: "^webrtc/(.+)$": outputs: - target: "rtmp://127.0.0.1/live/$1" options: video_codec: "copy" # 视频直接复制,不重编码 audio_codec: "aac" # Opus → AAC audio_bitrate: 128RTMP → 多码率输出
Section titled “RTMP → 多码率输出”将单路 RTMP 流转码为多个码率版本:
onpub: "^live/(.+)$": outputs: - target: "rtmp://127.0.0.1/live/$1_720p" options: video_codec: "libx264" resolution: "1280x720" video_bitrate: 2000 audio_codec: "copy" - target: "rtmp://127.0.0.1/live/$1_480p" options: video_codec: "libx264" resolution: "854x480" video_bitrate: 1000 audio_codec: "copy"CDN 转推
Section titled “CDN 转推”将本地流转推到外部 CDN:
onpub: "^live/(.+)$": outputs: - target: "rtmp://cdn-push.example.com/live/$1" options: video_codec: "copy" audio_codec: "copy"API 接口
Section titled “API 接口”所有接口挂载在 /transcode/api/ 路由前缀下。
查询任务列表
Section titled “查询任务列表”GET /transcode/api/list返回当前所有活跃的转码任务。
响应示例:
{ "code": 0, "message": "success", "data": { "count": 2, "jobs": [ { "id": 1, "source_path": "live/camera01", "input_url": "rtmp://127.0.0.1/live/camera01", "target": "rtmp://cdn.example.com/live/camera01" } ] }}手动启动转码
Section titled “手动启动转码”POST /transcode/api/launchContent-Type: application/json
{ "source_path": "live/camera01", "target": "rtmp://cdn.example.com/live/camera01", "options": { "video_codec": "libx264", "audio_codec": "aac", "video_bitrate": 2000, "resolution": "1280x720" }}响应示例:
{ "code": 0, "message": "launched", "data": { "id": 1 }}POST /transcode/api/closeContent-Type: application/json
{ "id": 1}也可以按源流路径停止所有关联任务:
{ "source_path": "live/camera01"}检查目标是否存在
Section titled “检查目标是否存在”GET /transcode/api/exist?target=rtmp://cdn.example.com/live/camera01GET /transcode/api/config- FFmpeg 进程管理:每个转码任务对应一个独立的 FFmpeg 子进程,插件负责进程的创建和清理
- 资源消耗:视频转码(非 copy 模式)消耗大量 CPU,建议合理规划转码任务数量
- local_rtmp 配置:确保引擎的 RTMP 插件已启用,且地址与
local_rtmp配置一致 - 目标去重:对于同一目标 URL,不会重复创建转码任务
联系我们
微信公众号:不卡科技
腾讯频道:流媒体技术
QQ 频道:p0qq0crz08
QQ 群:751639168