Skip to content

fix(audio): :bugs: fix webm audio strconv.ParseFloat: parsing "N/A" - #1243

Merged
Calcium-Ion merged 2 commits into
QuantumNous:mainfrom
cjm0810151:main
Jun 17, 2025
Merged

Calcium-Ion merged 2 commits into
QuantumNous:mainfrom
cjm0810151:main

Conversation

@cjm0810151

@cjm0810151 cjm0810151 commented Jun 17, 2025 •

Copy link
Copy Markdown
Contributor

fix webm audio file, strconv.ParseFloat: parsing "N/A"
#739

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of audio files where the duration could not be determined initially, adding a fallback mechanism to ensure accurate duration retrieval.
  • Chores

    • Enhanced resource management and file extension handling for uploaded audio files, leading to more robust processing and compatibility with various audio formats.

@coderabbitai

coderabbitai Bot commented Jun 17, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

The changes introduce a fallback mechanism in the audio duration calculation logic. The GetAudioDuration function now accepts a file extension parameter and, if the initial duration check fails, creates a temporary file with the correct extension to retry the duration extraction. The calling code in the OpenAI relay handler is updated to pass the extension and ensure proper temporary file handling.

Changes

File(s) Change Summary
common/utils.go GetAudioDuration now accepts an ext parameter; adds fallback logic using a temp file with extension if duration is "N/A".
relay/channel/openai/relay-openai.go Updates countAudioTokens to extract file extension, create temp file with extension, and call updated GetAudioDuration.

Poem

In the warren, clever code does dwell,
Now with temp files, all is well.
If duration’s “N/A,” don’t despair—
A fallback hop is waiting there.
Extensions checked, the files are neat,
This bunny’s work is quite a treat!
🐇🎶

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need 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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. Consider lower-casing ext to handle .WEBM / .Wav variants:

    ext := strings.ToLower(filepath.Ext(reqBody.File.Filename))
  2. We already copy the upload into tmpFp; however GetAudioDuration may copy it again if "N/A" occurs, which doubles I/O for problematic formats.
    Passing tmpFp.Name() and ext is fine, but you could eliminate one copy by doing the "N/A" fix-up directly here (or by adding a skipFix bool flag).

Not blocking, just worth considering.

Also applies to: 370-372

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 59a6fa7 and b33d808.

📒 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/bash

List 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 -->

Comment thread common/utils.go
Comment on lines +272 to +275
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")
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ 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.

Suggested change
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.

Comment thread common/utils.go
Comment on lines +259 to +285
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

"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.

Suggested change
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.

@Calcium-Ion
Calcium-Ion merged commit 87c2600 into QuantumNous:main Jun 17, 2025
x22x22 pushed a commit to x22x22/new-api that referenced this pull request Apr 24, 2026
fix(audio): :bugs: fix webm audio strconv.ParseFloat: parsing "N/A"
salem-2007 added a commit to salem-2007/new-api that referenced this pull request Sep 10, 2026
fix(audio): :bugs: fix webm audio strconv.ParseFloat: parsing "N/A"
junjundesk pushed a commit to junjundesk/new-api that referenced this pull request Oct 3, 2026
fix(audio): :bugs: fix webm audio strconv.ParseFloat: parsing "N/A"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants