Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
a13a6354
authored
Jun 17, 2025
by
Calcium-Ion
Committed by
GitHub
Jun 17, 2025
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1243 from cjm0810151/main
fix(audio): :bugs: fix webm audio strconv.ParseFloat: parsing "N/A"
parents
293decb9
ab5dfd8d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
6 deletions
+33
-6
common/utils.go
+28
-3
relay/channel/openai/relay-openai.go
+5
-3
No files found.
common/utils.go
View file @
a13a6354
...
@@ -249,13 +249,38 @@ func SaveTmpFile(filename string, data io.Reader) (string, error) {
...
@@ -249,13 +249,38 @@ func SaveTmpFile(filename string, data io.Reader) (string, error) {
}
}
// GetAudioDuration returns the duration of an audio file in seconds.
// GetAudioDuration returns the duration of an audio file in seconds.
func
GetAudioDuration
(
ctx
context
.
Context
,
filename
string
)
(
float64
,
error
)
{
func
GetAudioDuration
(
ctx
context
.
Context
,
filename
string
,
ext
string
)
(
float64
,
error
)
{
// ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 {{input}}
// ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 {{input}}
c
:=
exec
.
CommandContext
(
ctx
,
"ffprobe"
,
"-v"
,
"error"
,
"-show_entries"
,
"format=duration"
,
"-of"
,
"default=noprint_wrappers=1:nokey=1"
,
filename
)
c
:=
exec
.
CommandContext
(
ctx
,
"ffprobe"
,
"-v"
,
"error"
,
"-show_entries"
,
"format=duration"
,
"-of"
,
"default=noprint_wrappers=1:nokey=1"
,
filename
)
output
,
err
:=
c
.
Output
()
output
,
err
:=
c
.
Output
()
if
err
!=
nil
{
if
err
!=
nil
{
return
0
,
errors
.
Wrap
(
err
,
"failed to get audio duration"
)
return
0
,
errors
.
Wrap
(
err
,
"failed to get audio duration"
)
}
}
durationStr
:=
string
(
bytes
.
TrimSpace
(
output
))
return
strconv
.
ParseFloat
(
string
(
bytes
.
TrimSpace
(
output
)),
64
)
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
)
}
}
relay/channel/openai/relay-openai.go
View file @
a13a6354
...
@@ -8,6 +8,7 @@ import (
...
@@ -8,6 +8,7 @@ import (
"math"
"math"
"mime/multipart"
"mime/multipart"
"net/http"
"net/http"
"path/filepath"
"one-api/common"
"one-api/common"
"one-api/constant"
"one-api/constant"
"one-api/dto"
"one-api/dto"
...
@@ -345,13 +346,14 @@ func countAudioTokens(c *gin.Context) (int, error) {
...
@@ -345,13 +346,14 @@ func countAudioTokens(c *gin.Context) (int, error) {
if
err
=
c
.
ShouldBind
(
&
reqBody
);
err
!=
nil
{
if
err
=
c
.
ShouldBind
(
&
reqBody
);
err
!=
nil
{
return
0
,
errors
.
WithStack
(
err
)
return
0
,
errors
.
WithStack
(
err
)
}
}
ext
:=
filepath
.
Ext
(
reqBody
.
File
.
Filename
)
// 获取文件扩展名
reqFp
,
err
:=
reqBody
.
File
.
Open
()
reqFp
,
err
:=
reqBody
.
File
.
Open
()
if
err
!=
nil
{
if
err
!=
nil
{
return
0
,
errors
.
WithStack
(
err
)
return
0
,
errors
.
WithStack
(
err
)
}
}
defer
reqFp
.
Close
()
tmpFp
,
err
:=
os
.
CreateTemp
(
""
,
"audio-*"
)
tmpFp
,
err
:=
os
.
CreateTemp
(
""
,
"audio-*"
+
ext
)
if
err
!=
nil
{
if
err
!=
nil
{
return
0
,
errors
.
WithStack
(
err
)
return
0
,
errors
.
WithStack
(
err
)
}
}
...
@@ -365,7 +367,7 @@ func countAudioTokens(c *gin.Context) (int, error) {
...
@@ -365,7 +367,7 @@ func countAudioTokens(c *gin.Context) (int, error) {
return
0
,
errors
.
WithStack
(
err
)
return
0
,
errors
.
WithStack
(
err
)
}
}
duration
,
err
:=
common
.
GetAudioDuration
(
c
.
Request
.
Context
(),
tmpFp
.
Name
())
duration
,
err
:=
common
.
GetAudioDuration
(
c
.
Request
.
Context
(),
tmpFp
.
Name
()
,
ext
)
if
err
!=
nil
{
if
err
!=
nil
{
return
0
,
errors
.
WithStack
(
err
)
return
0
,
errors
.
WithStack
(
err
)
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment