one-api/common/audio/audio.go

20 lines
595 B
Go

package audio
import (
"bytes"
"context"
"os/exec"
"strconv"
)
// 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, err
}
return strconv.ParseFloat(string(bytes.TrimSpace(output)), 64)
}