mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-30 07:06:38 +08:00
20 lines
595 B
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)
|
|
}
|