-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractor.go
54 lines (48 loc) · 1.47 KB
/
extractor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
)
func ExtractAudio(url, output string, ytdlpPath string) (string, error) {
wavFile := output + ".wav"
cmd := exec.Command(ytdlpPath,
"-x",
"--audio-format", "wav",
"--audio-quality", "0",
"-o", wavFile,
url)
out, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("failed to extract audio: %w, output: %s", err, out)
}
// Проверяем, существует ли файл
if _, err := os.Stat(wavFile); os.IsNotExist(err) {
// Если файл не существует, ищем файл с расширением .wav
matches, err := filepath.Glob(output + "*.wav")
if err != nil {
return "", fmt.Errorf("failed to find output file: %w", err)
}
if len(matches) == 0 {
return "", fmt.Errorf("output file not found")
}
wavFile = matches[0]
}
// Конвертируем WAV в PCM
pcmFile := output + ".pcm"
cmd = exec.Command("ffmpeg",
"-i", wavFile,
"-f", "s16le",
"-acodec", "pcm_s16le",
"-ar", "48000",
"-ac", "2",
pcmFile)
out, err = cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("failed to convert to PCM: %w, output: %s", err, out)
}
// Удаляем временный WAV файл
os.Remove(wavFile)
return pcmFile, nil
}