123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- package audio
- import (
- "bytes"
- "eta/eta_pub/utils"
- "fmt"
- "math"
- "os"
- "os/exec"
- "path"
- "strconv"
- "strings"
- )
- func ConvAudioDecibel(inputFile string) (outputFile string, isConvDecibel bool, err error) {
-
- convAudioPath := inputFile
-
- ext := path.Ext(convAudioPath)
- inputFileStrList := strings.Split(convAudioPath, ext)
- if ext != ".mp3" {
-
- convAudioPath = inputFileStrList[0] + "_conv.mp3"
- err = ConvertAudioFormat(inputFile, convAudioPath)
- if err != nil {
-
- return
- }
-
- defer func() {
- os.Remove(convAudioPath)
- }()
- }
- baseDecibels := -22.8
-
- var volumeFloat, diffDecibels float64
-
- {
- tmpVolumeFloat, ok, tmpErr := extractVolume(convAudioPath)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if !ok {
- return
- }
- volumeFloat = tmpVolumeFloat
-
- diffDecibels = volumeFloat - baseDecibels
-
- if math.Abs(diffDecibels) <= 5 {
-
- return
- }
- }
-
-
- targetDecibels := ""
-
- if diffDecibels < 0 {
- targetDecibels = fmt.Sprintf("+%sdB", utils.SubFloatToString(math.Abs(diffDecibels), 2))
- } else {
- targetDecibels = fmt.Sprintf("-%sdB", utils.SubFloatToString(math.Abs(diffDecibels), 2))
- }
-
- afCommandStr := "volume=" + targetDecibels
-
-
- outputFile = inputFileStrList[0] + "_decibel.mp3"
-
- cmd := exec.Command("ffmpeg", "-y", "-i", convAudioPath, "-af", afCommandStr, outputFile)
- var out bytes.Buffer
- cmd.Stderr = &out
-
- err = cmd.Run()
- if err != nil {
- utils.FileLog.Info("转换音频分贝格式失败,FFmpeg命令返回:\n" + out.String())
- return
- }
- isConvDecibel = true
-
- return
- }
- func extractVolume(audioPath string) (decibels float64, ok bool, err error) {
- cmd := exec.Command("ffmpeg", "-i", audioPath, "-af", "volumedetect", "-f", "null", "-")
- var out bytes.Buffer
- cmd.Stderr = &out
- err = cmd.Run()
- if err != nil {
- return
- }
- output := out.String()
-
- var volumeLine string
- lines := strings.Split(output, "\n")
- for _, line := range lines {
- if strings.Contains(line, "mean_volume:") {
- volumeLine = line
- break
- }
- }
- if volumeLine == `` {
- return
- }
- volumeArr := strings.Split(volumeLine, " ")
- if len(volumeArr) < 4 {
- return
- }
-
-
-
-
-
-
-
-
- volume := volumeArr[4]
- decibels, err = convertToFloat(volume)
- ok = true
-
- return
- }
- func convertToFloat(str string) (float64, error) {
- str = strings.TrimSpace(str)
- value, err := strconv.ParseFloat(str, 64)
- if err != nil {
- return 0, err
- }
- return value, nil
- }
- func ConvertAudioFormat(inputFilePath, outputFilePath string) (err error) {
- cmd := exec.Command("ffmpeg", "-y", "-i", inputFilePath, "-codec:a", "libmp3lame", outputFilePath)
- err = cmd.Run()
- if err != nil {
- return
- }
- return nil
- }
|