-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
233 lines (157 loc) · 3.87 KB
/
main.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"encoding/json"
)
func homePage(w http.ResponseWriter, r *http.Request) {
html := `
<!DOCTYPE html>
<html>
<head>
<title>TuneCraft - Youtube2Mp3</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<style>
body {
font-family: Arial, sans-serif;
margin:0;
padding:0;
}
h1 {
text-align: center;
margin-bottom: 20px;
}
.container {
width:90%;
max-width: 700px;
margin: 100px auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 8px;
text-align: center;
}
form {
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
}
form label {
font-size: 1.2rem;
color: #555;
text-align: left;
}
form input[type="text"] {
width:35%;
max-width: 500px;
margin: 0 auto;
border: 1px solid #ccc;
border-radius: 4px;
padding: 10px;
font-size: 1em;
}
form input[type="submit"] {
width:30%;
max-width: 500px;
padding: 10px;
font-size: 1.1rem;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
form input[type="submit"]:hover {
background-color: #45a049;
}
</style>
<body>
<h1>TuneCraft: YouTube to MP3 Converter</h1>
<div class="container">
<form id="formaction">
<label for="url">Enter YouTube Video ID or Url:</label>
<input type="text" id="url" name="url" required>
<input type="submit" value="Convert to MP3">
</form>
</div>
<script>
document.getElementById("formaction").addEventListener("submit", function(e) {
e.preventDefault();
const id = document.getElementById("url").value;
if (id.includes("youtube.com")) {
newId = id.split("v=")[1];
window.location.href = "/download?id=" + newId;
} else {
window.location.href = "/download?id=" + id;
}
});
</script>
</body>
</html>`
fmt.Fprint(w, html)
}
func main() {
http.HandleFunc("/", homePage)
http.HandleFunc("/download", download)
fmt.Println("Server starting at port 9095")
log.Fatal(http.ListenAndServe(":9095", nil))
}
func downloadAndExtractMp3(id string, output http.ResponseWriter) error {
url := fmt.Sprintf("https://www.youtube.com/watch?v=%s", id)
callUrl := fmt.Sprintf("https://noembed.com/embed?url=%s", url)
callRes, err := http.Get(callUrl)
if err != nil {
return err
}
resBody, err := io.ReadAll(callRes.Body)
if err != nil {
log.Printf("Error reading body: %+v", err)
return err
}
defer callRes.Body.Close()
var out map[string]interface{}
_ = json.Unmarshal(resBody, &out)
// Create pipes for communication
ytdlRead, ytdlWrite := io.Pipe()
ffmpegRead, ffmpegWrite := io.Pipe()
// YouTube-DL Command
ytdl := exec.Command("youtube-dl", url, "-o-")
ytdl.Stdout = ytdlWrite
ytdl.Stderr = os.Stderr
ffmpeg := exec.Command("ffmpeg", "-i", "pipe:0", "-f", "mp3", "-ab", "96000", "-vn", "-")
ffmpeg.Stdin = ytdlRead
ffmpeg.Stdout = ffmpegWrite
ffmpeg.Stderr = os.Stderr
output.Header().Set("Content-Type", "audio/mpeg")
output.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s.mp3", out["title"]))
go func() {
defer ytdlWrite.Close()
ytdl.Run()
}()
go func() {
defer ffmpegWrite.Close()
ffmpeg.Run()
}()
_, err = io.Copy(output, ffmpegRead)
if err != nil {
return err
}
// Wait for FFmpeg to finish
ffmpeg.Wait()
return nil
}
func download(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
fmt.Println(id)
err := downloadAndExtractMp3(id, w)
if err != nil {
http.Error(w, "Failed to download", http.StatusInternalServerError)
return
}
}