网站通话使用webrtc,新增ffmpeg

This commit is contained in:
2025-12-16 15:12:03 +08:00
parent 811764fd8a
commit c8ab9b74ab
3 changed files with 103 additions and 30 deletions

View File

@@ -19,6 +19,9 @@ import (
// TranscoderConfig 转码器配置
type TranscoderConfig struct {
// 模式配置
CopyMode bool // 是否使用 copy 模式H.264 输入时使用,只重封装不重新编码)
// 视频配置
VideoCodec string // 输出视频编解码器,默认 libx264
VideoPreset string // x264 预设,默认 ultrafast
@@ -96,20 +99,41 @@ func (t *FFmpegTranscoder) Start() error {
}
// 构建 FFmpeg 命令
// ffmpeg -f webm -i pipe:0 -c:v libx264 -preset ultrafast -tune zerolatency -c:a aac -f flv pipe:1
args := []string{
"-f", "webm", // 输入格式
"-i", "pipe:0", // 从 stdin 读取
"-c:v", t.config.VideoCodec,
"-preset", t.config.VideoPreset,
"-tune", "zerolatency", // 零延迟模
"-b:v", t.config.VideoBitrate,
"-c:a", t.config.AudioCodec,
"-b:a", t.config.AudioBitrate,
"-ar", fmt.Sprintf("%d", t.config.AudioSampleRate),
"-ac", fmt.Sprintf("%d", t.config.AudioChannels),
"-f", "flv", // 输出格式
"pipe:1", // 输出到 stdout
var args []string
if t.config.CopyMode {
// H.264 copy 模式:只重封装,不重新编码(低延迟)
// ffmpeg -f webm -i pipe:0 -c:v copy -c:a aac -f flv pipe:1
args = []string{
"-f", "webm", // 输入格
"-i", "pipe:0", // 从 stdin 读取
"-c:v", "copy", // 视频直接复制,不重新编码
"-c:a", t.config.AudioCodec,
"-b:a", t.config.AudioBitrate,
"-ar", fmt.Sprintf("%d", t.config.AudioSampleRate),
"-ac", fmt.Sprintf("%d", t.config.AudioChannels),
"-f", "flv", // 输出格式
"pipe:1", // 输出到 stdout
}
log.Printf("🎬 [FFmpegTranscoder] 使用 copy 模式H.264 重封装)")
} else {
// VP8/VP9 转码模式:重新编码为 H.264
// ffmpeg -f webm -i pipe:0 -c:v libx264 -preset ultrafast -tune zerolatency -c:a aac -f flv pipe:1
args = []string{
"-f", "webm", // 输入格式
"-i", "pipe:0", // 从 stdin 读取
"-c:v", t.config.VideoCodec,
"-preset", t.config.VideoPreset,
"-tune", "zerolatency", // 零延迟模式
"-b:v", t.config.VideoBitrate,
"-c:a", t.config.AudioCodec,
"-b:a", t.config.AudioBitrate,
"-ar", fmt.Sprintf("%d", t.config.AudioSampleRate),
"-ac", fmt.Sprintf("%d", t.config.AudioChannels),
"-f", "flv", // 输出格式
"pipe:1", // 输出到 stdout
}
log.Printf("🎬 [FFmpegTranscoder] 使用转码模式VP8/VP9 → H.264")
}
t.cmd = exec.Command(t.ffmpegPath, args...)