Repository navigation
fix(audio): :bugs: fix webm audio strconv.ParseFloat: parsing "N/A" - #1243
Conversation
WalkthroughThe changes introduce a fallback mechanism in the audio duration calculation logic. The Changes
Poem
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
relay/channel/openai/relay-openai.go (1)
349-357: Minor: normalise & reuse extension, avoid double temp-file work
Consider lower-casing
extto handle.WEBM/.Wavvariants:ext := strings.ToLower(filepath.Ext(reqBody.File.Filename))We already copy the upload into
tmpFp; howeverGetAudioDurationmay copy it again if"N/A"occurs, which doubles I/O for problematic formats.
PassingtmpFp.Name()andextis fine, but you could eliminate one copy by doing the"N/A"fix-up directly here (or by adding askipFix boolflag).Not blocking, just worth considering.
Also applies to: 370-372
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
common/utils.go(1 hunks)relay/channel/openai/relay-openai.go(3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
relay/channel/openai/relay-openai.go (1)
common/utils.go (1)
GetAudioDuration(252-286)
🔇 Additional comments (1)
common/utils.go (1)
252-258: ```shell
#!/bin/bashList any GetAudioDuration calls still using only two arguments
rg -nF 'GetAudioDuration(' | while IFS=: read -r file line content; do
comma_count=$(grep -o ',' <<<"$content" | wc -l)
if [ "$comma_count" -eq 1 ]; then
printf "%s:%s: %s\n" "$file" "$line" "$content"
fi
done</details> </blockquote></details> </details> <!-- This is an auto-generated comment by CodeRabbit for review status -->
| ffmpegCmd := exec.CommandContext(ctx, "ffmpeg", "-y", "-i", filename, "-vcodec", "copy", "-acodec", "copy", tmpName) | ||
| if err := ffmpegCmd.Run(); err != nil { | ||
| return 0, errors.Wrap(err, "failed to run ffmpeg") | ||
| } |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Safer ffmpeg copy flags
-vcodec copy -acodec copy fails when the source has only audio (e.g. pure-audio WebM/OGG).
ffmpeg -c copy copies all streams and is shorter:
-ffmpegCmd := exec.CommandContext(ctx, "ffmpeg", "-y", "-i", filename, "-vcodec", "copy", "-acodec", "copy", tmpName)
+ffmpegCmd := exec.CommandContext(ctx, "ffmpeg", "-y", "-i", filename, "-c", "copy", tmpName)📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| ffmpegCmd := exec.CommandContext(ctx, "ffmpeg", "-y", "-i", filename, "-vcodec", "copy", "-acodec", "copy", tmpName) | |
| if err := ffmpegCmd.Run(); err != nil { | |
| return 0, errors.Wrap(err, "failed to run ffmpeg") | |
| } | |
| ffmpegCmd := exec.CommandContext(ctx, "ffmpeg", "-y", "-i", filename, "-c", "copy", tmpName) | |
| if err := ffmpegCmd.Run(); err != nil { | |
| return 0, errors.Wrap(err, "failed to run ffmpeg") | |
| } |
🤖 Prompt for AI Agents
In common/utils.go around lines 272 to 275, the ffmpeg command uses "-vcodec
copy -acodec copy" which fails for source files containing only audio streams.
Replace these flags with the shorter and safer "-c copy" to copy all streams
regardless of type, ensuring compatibility with audio-only files.
| durationStr := string(bytes.TrimSpace(output)) | ||
| if durationStr == "N/A" { | ||
| // Create a temporary output file name | ||
| tmpFp, err := os.CreateTemp("", "audio-*"+ext) | ||
| if err != nil { | ||
| return 0, errors.Wrap(err, "failed to create temporary file") | ||
| } | ||
| tmpName := tmpFp.Name() | ||
| // Close immediately so ffmpeg can open the file on Windows. | ||
| _ = tmpFp.Close() | ||
| defer os.Remove(tmpName) | ||
|
|
||
| // ffmpeg -y -i filename -vcodec copy -acodec copy <tmpName> | ||
| ffmpegCmd := exec.CommandContext(ctx, "ffmpeg", "-y", "-i", filename, "-vcodec", "copy", "-acodec", "copy", tmpName) | ||
| if err := ffmpegCmd.Run(); err != nil { | ||
| return 0, errors.Wrap(err, "failed to run ffmpeg") | ||
| } | ||
|
|
||
| // Recalculate the duration of the new file | ||
| c = exec.CommandContext(ctx, "ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", tmpName) | ||
| output, err := c.Output() | ||
| if err != nil { | ||
| return 0, errors.Wrap(err, "failed to get audio duration after ffmpeg") | ||
| } | ||
| durationStr = string(bytes.TrimSpace(output)) | ||
| } | ||
| return strconv.ParseFloat(durationStr, 64) |
There was a problem hiding this comment.
"N/A" fallback can still leak through — return explicit error instead of strconv.ParseFloat panic
If the second ffprobe call again yields "N/A" (or any non-numeric string) the final ParseFloat fails with strconv.ParseFloat: parsing "N/A": invalid syntax.
Guard before parsing:
@@
- }
- return strconv.ParseFloat(durationStr, 64)
+ }
+ if durationStr == "N/A" {
+ return 0, errors.Errorf("ffprobe returned N/A duration for %s", filename)
+ }
+ return strconv.ParseFloat(durationStr, 64)📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| durationStr := string(bytes.TrimSpace(output)) | |
| if durationStr == "N/A" { | |
| // Create a temporary output file name | |
| tmpFp, err := os.CreateTemp("", "audio-*"+ext) | |
| if err != nil { | |
| return 0, errors.Wrap(err, "failed to create temporary file") | |
| } | |
| tmpName := tmpFp.Name() | |
| // Close immediately so ffmpeg can open the file on Windows. | |
| _ = tmpFp.Close() | |
| defer os.Remove(tmpName) | |
| // ffmpeg -y -i filename -vcodec copy -acodec copy <tmpName> | |
| ffmpegCmd := exec.CommandContext(ctx, "ffmpeg", "-y", "-i", filename, "-vcodec", "copy", "-acodec", "copy", tmpName) | |
| if err := ffmpegCmd.Run(); err != nil { | |
| return 0, errors.Wrap(err, "failed to run ffmpeg") | |
| } | |
| // Recalculate the duration of the new file | |
| c = exec.CommandContext(ctx, "ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", tmpName) | |
| output, err := c.Output() | |
| if err != nil { | |
| return 0, errors.Wrap(err, "failed to get audio duration after ffmpeg") | |
| } | |
| durationStr = string(bytes.TrimSpace(output)) | |
| } | |
| return strconv.ParseFloat(durationStr, 64) | |
| durationStr := string(bytes.TrimSpace(output)) | |
| if durationStr == "N/A" { | |
| // Create a temporary output file name | |
| tmpFp, err := os.CreateTemp("", "audio-*"+ext) | |
| if err != nil { | |
| return 0, errors.Wrap(err, "failed to create temporary file") | |
| } | |
| tmpName := tmpFp.Name() | |
| // Close immediately so ffmpeg can open the file on Windows. | |
| _ = tmpFp.Close() | |
| defer os.Remove(tmpName) | |
| // ffmpeg -y -i filename -vcodec copy -acodec copy <tmpName> | |
| ffmpegCmd := exec.CommandContext(ctx, "ffmpeg", "-y", "-i", filename, "-vcodec", "copy", "-acodec", "copy", tmpName) | |
| if err := ffmpegCmd.Run(); err != nil { | |
| return 0, errors.Wrap(err, "failed to run ffmpeg") | |
| } | |
| // Recalculate the duration of the new file | |
| c = exec.CommandContext(ctx, "ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", tmpName) | |
| output, err := c.Output() | |
| if err != nil { | |
| return 0, errors.Wrap(err, "failed to get audio duration after ffmpeg") | |
| } | |
| durationStr = string(bytes.TrimSpace(output)) | |
| } | |
| if durationStr == "N/A" { | |
| return 0, errors.Errorf("ffprobe returned N/A duration for %s", filename) | |
| } | |
| return strconv.ParseFloat(durationStr, 64) |
🤖 Prompt for AI Agents
In common/utils.go around lines 259 to 285, the code attempts to parse
durationStr with strconv.ParseFloat without verifying if durationStr is a valid
numeric string, which can cause a panic if it is "N/A" or any non-numeric value.
To fix this, add a validation step before parsing durationStr to check if it is
a valid numeric string; if not, return an explicit error instead of calling
ParseFloat. This prevents panics and handles invalid duration values gracefully.
fix(audio): :bugs: fix webm audio strconv.ParseFloat: parsing "N/A"
fix(audio): :bugs: fix webm audio strconv.ParseFloat: parsing "N/A"
fix(audio): :bugs: fix webm audio strconv.ParseFloat: parsing "N/A"
fix webm audio file, strconv.ParseFloat: parsing "N/A"
#739
Summary by CodeRabbit
Bug Fixes
Chores