-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscode_api.go
More file actions
352 lines (320 loc) · 10.4 KB
/
transcode_api.go
File metadata and controls
352 lines (320 loc) · 10.4 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// transcode_api.go - HTTP API for media transcoding
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"mime"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
// handleTranscodeAPI handles transcoding requests for media files
func (c *Cluster) handleTranscodeAPI(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, fmt.Sprintf("Method %s not allowed for transcode API (only GET supported)", r.Method), http.StatusMethodNotAllowed)
return
}
// Extract file path from URL
path := strings.TrimPrefix(r.URL.Path, "/api/transcode/")
if path == "" {
http.Error(w, "Missing file path in transcode request - format: /api/transcode/path/to/file", http.StatusBadRequest)
return
}
// URL decode the path
decodedPath, err := url.PathUnescape(path)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid URL-encoded file path: %v", err), http.StatusBadRequest)
return
}
// Ensure path starts with /
if !strings.HasPrefix(decodedPath, "/") {
decodedPath = "/" + decodedPath
}
// Check if transcode parameter is present
transcodeFormat := r.URL.Query().Get("format")
if transcodeFormat == "" {
transcodeFormat = "web" // default
}
if transcodeFormat != "web" {
http.Error(w, fmt.Sprintf("Unsupported transcode format '%s' - only 'web' format is supported", transcodeFormat), http.StatusBadRequest)
return
}
// Get the original file
fileData, contentType, err := c.FileSystem.GetFileWithContentType(decodedPath)
if err != nil {
c.Logger().Printf("[TRANSCODE_API] Failed to get file %s: %v", decodedPath, err)
http.Error(w, fmt.Sprintf("File not found for transcoding: %s", decodedPath), http.StatusNotFound)
return
}
defer fileData.Close()
// Check if file needs transcoding
if !needsTranscoding(contentType, filepath.Base(decodedPath)) {
c.Logger().Printf("[TRANSCODE_API] File %s (%s) doesn't need transcoding", decodedPath, contentType)
// Serve original file
c.serveFile(w, r, fileData, contentType, filepath.Base(decodedPath))
return
}
// Create transcode request
req := TranscodeRequest{
InputPath: decodedPath,
ContentType: contentType,
OutputFormat: transcodeFormat,
}
// If content type is generic, try to infer from filename
if contentType == "application/octet-stream" || contentType == "" {
filename := strings.ToLower(filepath.Base(decodedPath))
if strings.HasSuffix(filename, ".webm") || strings.HasSuffix(filename, ".avi") ||
strings.HasSuffix(filename, ".mkv") || strings.HasSuffix(filename, ".mov") {
req.ContentType = "video/unknown"
} else if strings.HasSuffix(filename, ".wav") || strings.HasSuffix(filename, ".flac") ||
strings.HasSuffix(filename, ".ogg") {
req.ContentType = "audio/unknown"
}
}
// Transcode with timeout
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Minute)
defer cancel()
outputPath, err := c.Transcoder.TranscodeToWeb(ctx, fileData, req)
if err != nil {
c.Logger().Printf("[TRANSCODE_API] Transcoding failed for %s: %v", decodedPath, err)
http.Error(w, fmt.Sprintf("transcoding failed: %v", err), http.StatusInternalServerError)
return
}
// Serve transcoded file
transcodedFile, err := os.Open(outputPath)
if err != nil {
c.Logger().Printf("[TRANSCODE_API] Failed to open transcoded file %s: %v", outputPath, err)
http.Error(w, fmt.Sprintf("Failed to open transcoded file: %v", err), http.StatusInternalServerError)
return
}
defer transcodedFile.Close()
// Determine output content type
var outputContentType string
if strings.HasPrefix(req.ContentType, "video/") {
outputContentType = "video/mp4"
} else if strings.HasPrefix(req.ContentType, "audio/") {
outputContentType = "audio/mp4"
} else {
// Fallback - determine by original content type or filename
filename := strings.ToLower(filepath.Base(decodedPath))
videoExts := []string{".webm", ".avi", ".mkv", ".mov", ".flv", ".wmv", ".m4v"}
for _, ext := range videoExts {
if strings.HasSuffix(filename, ext) {
outputContentType = "video/mp4"
break
}
}
if outputContentType == "" {
audioExts := []string{".wav", ".flac", ".ogg", ".wma", ".m4a"}
for _, ext := range audioExts {
if strings.HasSuffix(filename, ext) {
outputContentType = "audio/mp4"
break
}
}
}
if outputContentType == "" {
outputContentType = "application/octet-stream"
}
}
// Get file info for proper serving
stat, err := transcodedFile.Stat()
if err != nil {
http.Error(w, fmt.Sprintf("Failed to get transcoded file info: %v", err), http.StatusInternalServerError)
return
}
// Serve with proper headers for streaming
filename := filepath.Base(decodedPath)
if ext := filepath.Ext(filename); ext != "" {
filename = strings.TrimSuffix(filename, ext) + ".mp4"
} else {
filename = filename + ".mp4"
}
c.serveFileWithSeeker(w, r, transcodedFile, outputContentType, filename, stat.Size())
}
// needsTranscoding determines if a file needs transcoding for web compatibility
func needsTranscoding(contentType string, filename string) bool {
// Check by content type first
switch contentType {
case "video/mp4":
// MP4 usually works, but might have codec issues
return false
case "video/webm", "video/ogg", "video/avi", "video/mov", "video/mkv":
// These often have codec issues in browsers
return true
case "audio/mpeg", "audio/mp3":
// MP3 usually works
return false
case "audio/mp4", "audio/aac":
// AAC usually works
return false
case "audio/wav", "audio/ogg", "audio/flac":
// These often need transcoding
return true
case "application/octet-stream", "":
// Generic content type - check by file extension
filename = strings.ToLower(filename)
// Video extensions that need transcoding
videoExts := []string{".webm", ".avi", ".mkv", ".mov", ".flv", ".wmv", ".m4v"}
for _, ext := range videoExts {
if strings.HasSuffix(filename, ext) {
return true
}
}
// Audio extensions that need transcoding
audioExts := []string{".wav", ".flac", ".ogg", ".wma", ".m4a"}
for _, ext := range audioExts {
if strings.HasSuffix(filename, ext) {
return true
}
}
// Video extensions that usually work
if strings.HasSuffix(filename, ".mp4") {
return false
}
// Audio extensions that usually work
if strings.HasSuffix(filename, ".mp3") {
return false
}
// Unknown file type with media-like extension
mediaExts := []string{".mp4", ".mp3", ".webm", ".wav", ".avi", ".mkv", ".mov"}
for _, ext := range mediaExts {
if strings.HasSuffix(filename, ext) {
// If we can't determine, err on the side of transcoding
return true
}
}
return false
default:
// Unknown video/audio types likely need transcoding
return strings.HasPrefix(contentType, "video/") || strings.HasPrefix(contentType, "audio/")
}
}
// serveFileWithSeeker serves a file that supports seeking (for HTTP range requests)
func (c *Cluster) serveFileWithSeeker(w http.ResponseWriter, r *http.Request, file *os.File, contentType, filename string, size int64) {
// Set headers for streaming support
w.Header().Set("Content-Type", contentType)
w.Header().Set("Accept-Ranges", "bytes")
w.Header().Set("Content-Length", strconv.FormatInt(size, 10))
// Handle range requests for seeking
rangeHeader := r.Header.Get("Range")
if rangeHeader != "" {
ranges, err := parseRange(rangeHeader, size)
if err != nil || len(ranges) != 1 {
w.Header().Set("Content-Range", fmt.Sprintf("bytes */%d", size))
http.Error(w, fmt.Sprintf("Invalid HTTP Range header (size=%d): %s", size, rangeHeader), http.StatusRequestedRangeNotSatisfiable)
return
}
start := ranges[0].start
end := ranges[0].end
length := end - start + 1
w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", start, end, size))
w.Header().Set("Content-Length", strconv.FormatInt(length, 10))
w.WriteHeader(http.StatusPartialContent)
// Seek to start position
if _, err := file.Seek(start, io.SeekStart); err != nil {
c.Logger().Printf("[TRANSCODE_API] Seek failed: %v", err)
return
}
// Copy the requested range
io.CopyN(w, file, length)
} else {
// Serve entire file
w.WriteHeader(http.StatusOK)
io.Copy(w, file)
}
}
// httpRange represents a single HTTP range request
type httpRange struct {
start, end int64
}
// parseRange parses HTTP Range header
func parseRange(s string, size int64) ([]httpRange, error) {
if s == "" {
return nil, nil
}
const b = "bytes="
if !strings.HasPrefix(s, b) {
return nil, fmt.Errorf("invalid range")
}
var ranges []httpRange
for _, ra := range strings.Split(s[len(b):], ",") {
ra = strings.TrimSpace(ra)
if ra == "" {
continue
}
i := strings.Index(ra, "-")
if i < 0 {
return nil, fmt.Errorf("invalid range")
}
start, end := strings.TrimSpace(ra[:i]), strings.TrimSpace(ra[i+1:])
var r httpRange
if start == "" {
// If no start is specified, end specifies the
// range start relative to the end of the file.
i, err := strconv.ParseInt(end, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid range")
}
if i > size {
i = size
}
r.start = size - i
r.end = size - 1
} else {
i, err := strconv.ParseInt(start, 10, 64)
if err != nil || i < 0 {
return nil, fmt.Errorf("invalid range")
}
r.start = i
if end == "" {
// If no end is specified, range extends to end of the file.
r.end = size - 1
} else {
i, err := strconv.ParseInt(end, 10, 64)
if err != nil || r.start > i {
return nil, fmt.Errorf("invalid range")
}
if i >= size {
i = size - 1
}
r.end = i
}
}
if r.start > size {
return nil, fmt.Errorf("invalid range")
}
ranges = append(ranges, r)
}
return ranges, nil
}
// serveFile serves a file with appropriate headers
func (c *Cluster) serveFile(w http.ResponseWriter, r *http.Request, reader io.ReadCloser, contentType, filename string) {
defer reader.Close()
// Set content type
if contentType == "" {
contentType = mime.TypeByExtension(filepath.Ext(filename))
if contentType == "" {
contentType = "application/octet-stream"
}
}
w.Header().Set("Content-Type", contentType)
// Copy file data
io.Copy(w, reader)
}
// handleTranscodeStats returns transcoder cache statistics
func (c *Cluster) handleTranscodeStats(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, fmt.Sprintf("Method %s not allowed for transcode stats API (only GET supported)", r.Method), http.StatusMethodNotAllowed)
return
}
stats := c.Transcoder.GetCacheStats()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(stats)
}