generated from songquanpeng/gin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: audio transcription only charge for the length of audio duration
- Loading branch information
Showing
9 changed files
with
238 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package helper | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"strconv" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// SaveTmpFile saves data to a temporary file. The filename would be apppended with a random string. | ||
func SaveTmpFile(filename string, data io.Reader) (string, error) { | ||
f, err := os.CreateTemp(os.TempDir(), filename) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "failed to create temporary file %s", filename) | ||
} | ||
defer f.Close() | ||
|
||
_, err = io.Copy(f, data) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "failed to copy data to temporary file %s", filename) | ||
} | ||
|
||
return f.Name(), nil | ||
} | ||
|
||
// GetAudioDuration returns the duration of an audio file in seconds. | ||
func GetAudioDuration(ctx context.Context, filename string) (float64, error) { | ||
// 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) | ||
output, err := c.Output() | ||
if err != nil { | ||
return 0, errors.Wrap(err, "failed to get audio duration") | ||
} | ||
|
||
return strconv.ParseFloat(string(bytes.TrimSpace(output)), 64) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package helper | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net/http" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetAudioDuration(t *testing.T) { | ||
t.Run("should return correct duration for a valid audio file", func(t *testing.T) { | ||
tmpFile, err := os.CreateTemp("", "test_audio*.mp3") | ||
require.NoError(t, err) | ||
defer os.Remove(tmpFile.Name()) | ||
|
||
// download test audio file | ||
resp, err := http.Get("https://s3.laisky.com/uploads/2025/01/audio-sample.m4a") | ||
require.NoError(t, err) | ||
defer resp.Body.Close() | ||
|
||
_, err = io.Copy(tmpFile, resp.Body) | ||
require.NoError(t, err) | ||
require.NoError(t, tmpFile.Close()) | ||
|
||
duration, err := GetAudioDuration(context.Background(), tmpFile.Name()) | ||
require.NoError(t, err) | ||
require.Equal(t, duration, 3.904) | ||
}) | ||
|
||
t.Run("should return an error for a non-existent file", func(t *testing.T) { | ||
_, err := GetAudioDuration(context.Background(), "non_existent_file.mp3") | ||
require.Error(t, err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.