-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtranscribe.go
89 lines (75 loc) · 2.29 KB
/
transcribe.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"os/exec"
"path/filepath"
)
func downloadAudio(youtubeUrl string, outputPath string) error {
cmd := exec.Command("yt-dlp",
"--extract-audio",
"--audio-format", "mp3",
"--audio-quality", "0",
"-o", outputPath,
youtubeUrl)
if err := cmd.Run(); err != nil {
return fmt.Errorf("error downloading audio: %w", err)
}
fmt.Println("Audio downloaded successfully.")
return nil
}
func sendAudioToLemonfox(filePath string, apiKey string) (string, error) {
fmt.Println("Starting transcription process...")
// Read file
fileData, err := os.ReadFile(filePath)
if err != nil {
return "", fmt.Errorf("error reading file: %w", err)
}
// Create multipart form
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
// Add file to form
part, err := writer.CreateFormFile("file", filepath.Base(filePath))
if err != nil {
return "", fmt.Errorf("error creating form file: %w", err)
}
if _, err := io.Copy(part, bytes.NewReader(fileData)); err != nil {
return "", fmt.Errorf("error copying file data: %w", err)
}
// Add other form fields
writer.WriteField("language", "english")
writer.WriteField("response_format", "vtt")
writer.Close()
// Create request
req, err := http.NewRequest("POST", "https://api.lemonfox.ai/v1/audio/transcriptions", body)
if err != nil {
return "", fmt.Errorf("error creating request: %w", err)
}
// Set headers
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", writer.FormDataContentType())
fmt.Println("Sending file to Lemonfox API...")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("error sending request: %w", err)
}
defer resp.Body.Close()
fmt.Println("Request sent, waiting for transcription...")
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading response: %w", err)
}
// Add status code check and response logging
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(respBody))
}
fmt.Printf("Status Code: %d\n", resp.StatusCode)
fmt.Printf("Response Headers: %v\n", resp.Header)
fmt.Printf("Transcription result: %s\n", string(respBody))
return string(respBody), nil
}