(ctx context.Context, segments [][]byte, targetFormat string)
| 118 | ) |
| 119 | |
| 120 | func (t *ffmpegRecordingTranscoder) Transcode(ctx context.Context, segments [][]byte, targetFormat string) ([]byte, error) { |
| 121 | if len(segments) == 0 { |
| 122 | return nil, errors.New("录音分段为空") |
| 123 | } |
| 124 | if len(segments) != 1 { |
| 125 | return nil, fmt.Errorf("录音转码仅支持单个分段: %d", len(segments)) |
| 126 | } |
| 127 | |
| 128 | var totalSize int64 |
| 129 | for _, seg := range segments { |
| 130 | totalSize += int64(len(seg)) |
| 131 | } |
| 132 | if totalSize > maxTotalSegmentsSize { |
| 133 | return nil, fmt.Errorf("录音总大小超限: %d > %d bytes", totalSize, maxTotalSegmentsSize) |
| 134 | } |
| 135 | |
| 136 | normalizedFormat := normalizeRecordingTargetFormat(targetFormat) |
| 137 | encoder, err := encoderForRecordingTargetFormat(normalizedFormat) |
| 138 | if err != nil { |
| 139 | return nil, err |
| 140 | } |
| 141 | ffmpegPath, err := ResolveFFmpegToolchainPath() |
| 142 | if err != nil { |
| 143 | return nil, err |
| 144 | } |
| 145 | |
| 146 | workDir, err := os.MkdirTemp("", "recording-transcode-*") |
| 147 | if err != nil { |
| 148 | return nil, fmt.Errorf("创建临时目录失败: %w", err) |
| 149 | } |
| 150 | defer os.RemoveAll(workDir) |
| 151 | |
| 152 | inputPath := filepath.Join(workDir, "input.webm") |
| 153 | if err := writeSegmentsToFile(inputPath, segments); err != nil { |
| 154 | return nil, err |
| 155 | } |
| 156 | outputPath := filepath.Join(workDir, "output."+normalizedFormat) |
| 157 | if err := ensureRecordingWritableFile(outputPath, recordingLocalArtifactMode); err != nil { |
| 158 | return nil, err |
| 159 | } |
| 160 | args := []string{ |
| 161 | "-y", |
| 162 | "-i", inputPath, |
| 163 | "-vn", |
| 164 | "-c:a", encoder, |
| 165 | outputPath, |
| 166 | } |
| 167 | if err := t.runner.Run(ctx, ffmpegPath, args...); err != nil { |
| 168 | return nil, fmt.Errorf("转码录音失败: %w", err) |
| 169 | } |
| 170 | if err := ensureRecordingFileMode(outputPath, recordingLocalArtifactMode); err != nil { |
| 171 | return nil, err |
| 172 | } |
| 173 | result, err := os.ReadFile(outputPath) |
| 174 | if err != nil { |
| 175 | return nil, fmt.Errorf("读取最终输出失败: %w", err) |
| 176 | } |
| 177 | return result, nil |
nothing calls this directly
no test coverage detected