-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
730 lines (645 loc) · 20.4 KB
/
main.go
File metadata and controls
730 lines (645 loc) · 20.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
)
var rootDir string
var (
transcodeMutex sync.Mutex
activeCmd *exec.Cmd
)
type FileInfo struct {
Name string `json:"name"`
Path string `json:"path"`
IsDir bool `json:"isDir"`
IsVideo bool `json:"isVideo"`
CanPlay bool `json:"canPlay"`
NeedsTranscode bool `json:"needsTranscode"`
}
// Video formats that browsers can typically play natively
var nativeFormats = map[string]bool{
".mp4": true,
".webm": true,
".ogg": true,
}
// All video formats we'll recognize
var videoFormats = map[string]bool{
".mp4": true,
".webm": true,
".ogg": true,
".mkv": true,
".avi": true,
".mov": true,
".wmv": true,
".flv": true,
".m4v": true,
".mpg": true,
".mpeg": true,
".3gp": true,
}
func main() {
dir := flag.String("d", ".", "Directory to serve")
port := flag.String("p", "8080", "Port to listen on")
flag.Parse()
var err error
rootDir, err = filepath.Abs(*dir)
if err != nil {
log.Fatal("Invalid directory:", err)
}
if _, err := os.Stat(rootDir); os.IsNotExist(err) {
log.Fatal("Directory does not exist:", rootDir)
}
log.Printf("Serving directory: %s", rootDir)
log.Printf("Server starting on http://localhost:%s", *port)
http.HandleFunc("/", handleIndex)
http.HandleFunc("/api/browse", handleBrowse)
http.HandleFunc("/api/video/", handleVideo)
http.HandleFunc("/api/stream/", handleStream)
log.Fatal(http.ListenAndServe(":"+*port, nil))
}
func handleIndex(w http.ResponseWriter, r *http.Request) {
tmpl := `<!DOCTYPE html>
<html>
<head>
<title>Stromboli</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { width: 100%; height: 100%; overflow: hidden; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: #1a1a1a;
color: #e0e0e0;
min-height: 100svh;
display: flex;
flex-direction: column;
}
header {
background: #2d2d2d;
padding: 1rem 2rem;
border-bottom: 2px solid #3d3d3d;
}
h1 { font-size: 1.5rem; color: #fff; }
.container {
display: flex;
flex: 1 1 auto;
min-height: 0;
overflow: hidden;
}
.browser {
width: clamp(240px, 30vw, 350px);
background: #242424;
border-right: 1px solid #3d3d3d;
display: flex;
flex-direction: column;
overflow: hidden;
min-height: 0;
}
.breadcrumb {
padding: 1rem;
background: #2d2d2d;
border-bottom: 1px solid #3d3d3d;
font-size: 0.9rem;
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.5rem;
}
.breadcrumb-path {
flex: 1;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
min-width: 0;
}
.breadcrumb span {
color: #4a9eff;
cursor: pointer;
padding: 0.2rem 0.4rem;
border-radius: 3px;
text-transform: capitalize;
}
.breadcrumb span:hover { background: #3d3d3d; }
.filter-toggle {
background: #3d3d3d;
border: none;
color: #e0e0e0;
padding: 0.5rem 0.75rem;
border-radius: 4px;
cursor: pointer;
font-size: 0.9rem;
margin-left: 0.5rem;
flex-shrink: 0;
}
.filter-toggle:hover { background: #4d4d4d; }
.filter-toggle.active { background: #4a9eff; color: #000; }
.filter-bar {
padding: 0.75rem 1rem;
background: #2d2d2d;
border-bottom: 1px solid #3d3d3d;
display: none;
}
.filter-bar.visible { display: block; }
.filter-input {
width: 100%;
padding: 0.5rem;
background: #1a1a1a;
border: 1px solid #3d3d3d;
border-radius: 4px;
color: #e0e0e0;
font-size: 0.9rem;
}
.filter-input:focus {
outline: none;
border-color: #4a9eff;
}
.filter-input::placeholder { color: #666; }
.file-list {
flex: 1 1 auto;
overflow-y: auto;
padding: 0.5rem;
min-height: 0;
overscroll-behavior: contain;
-webkit-overflow-scrolling: touch;
}
.file-item {
padding: 0.75rem 1rem;
cursor: pointer;
border-radius: 4px;
margin-bottom: 0.25rem;
display: flex;
align-items: center;
gap: 0.5rem;
}
.file-item:hover { background: #2d2d2d; }
.file-item.active { background: #3d3d3d; }
.icon {
font-size: 1.2rem;
width: 24px;
text-align: center;
}
.player {
flex: 1 1 auto;
display: flex;
align-items: center;
justify-content: center;
padding: 2rem;
min-height: 0;
overflow: hidden;
}
video {
max-width: 100%;
max-height: 100%;
background: #000;
border-radius: 8px;
}
.empty-state {
text-align: center;
color: #666;
}
.empty-state h2 { font-size: 1.5rem; margin-bottom: 0.5rem; }
.loading {
text-align: center;
padding: 2rem;
color: #666;
}
.transcoding-notice {
position: absolute;
top: 1rem;
right: 1rem;
background: #ff9800;
color: #000;
padding: 0.5rem 1rem;
border-radius: 4px;
font-size: 0.9rem;
font-weight: 500;
}
@media (max-width: 768px) {
.container {
flex-direction: column;
}
.browser {
width: 100%;
max-height: 40svh;
border-right: none;
border-bottom: 1px solid #3d3d3d;
}
.player {
padding: 1rem;
}
header {
padding: 0.75rem 1rem;
}
h1 {
font-size: 1.25rem;
}
.file-item {
padding: 1rem;
font-size: 1rem;
}
.breadcrumb span {
padding: 0.4rem 0.6rem;
}
.transcoding-notice {
top: auto;
bottom: 1rem;
right: 50%;
transform: translateX(50%);
font-size: 0.8rem;
}
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<header>
<h1>Stromboli</h1>
</header>
<div class="container">
<div class="browser">
<div class="breadcrumb" id="breadcrumb">
<div class="breadcrumb-path" id="breadcrumbPath"></div>
<button class="filter-toggle" id="filterToggle" onclick="toggleFilter()">🔍</button>
</div>
<div class="filter-bar" id="filterBar">
<input type="text" class="filter-input" id="filterInput" placeholder="Filter files and folders..." oninput="applyFilter()">
</div>
<div class="file-list" id="fileList">
<div class="loading">Loading...</div>
</div>
</div>
<div class="player" id="player">
<div class="empty-state">
<h2>Select a video to play</h2>
<p>Browse the directory tree on the left</p>
</div>
</div>
</div>
<script>
let currentPath = '';
let currentVideo = null;
let allFiles = [];
let filterVisible = false;
function toggleFilter() {
filterVisible = !filterVisible;
const filterBar = document.getElementById('filterBar');
const filterToggle = document.getElementById('filterToggle');
const filterInput = document.getElementById('filterInput');
if (filterVisible) {
filterBar.classList.add('visible');
filterToggle.classList.add('active');
filterInput.focus();
} else {
filterBar.classList.remove('visible');
filterToggle.classList.remove('active');
filterInput.value = '';
renderFileList(allFiles);
}
}
function applyFilter() {
const filterText = document.getElementById('filterInput').value.toLowerCase();
if (!filterText) {
renderFileList(allFiles);
return;
}
const filtered = allFiles.filter(file =>
file.name.toLowerCase().includes(filterText)
);
renderFileList(filtered);
}
function browse(path = '') {
currentPath = path;
fetch('/api/browse?path=' + encodeURIComponent(path))
.then(r => r.json())
.then(files => {
allFiles = files;
updateBreadcrumb(path);
// Clear filter when changing directories
document.getElementById('filterInput').value = '';
renderFileList(files);
})
.catch(err => {
document.getElementById('fileList').innerHTML =
'<div class="loading">Error loading directory</div>';
});
}
function updateBreadcrumb(path) {
const parts = path ? path.split('/').filter(p => p) : [];
const breadcrumbPath = document.getElementById('breadcrumbPath');
let html = '<span onclick="browse(\'\')">Home</span>';
let accumulated = '';
parts.forEach(part => {
accumulated += (accumulated ? '/' : '') + part;
const thisPath = accumulated;
html += ' / <span onclick="browse(\'' + thisPath + '\')">' + part + '</span>';
});
breadcrumbPath.innerHTML = html;
}
function renderFileList(files) {
const list = document.getElementById('fileList');
if (files.length === 0) {
list.innerHTML = '<div class="loading">No matches found</div>';
return;
}
// Sort: directories first, then files
files.sort((a, b) => {
if (a.isDir !== b.isDir) return b.isDir - a.isDir;
return a.name.localeCompare(b.name);
});
list.innerHTML = files.map(file => {
const icon = file.isDir ? '📁' : (file.isVideo ? '🎬' : '📄');
let onclick = '';
let clickHandler = '';
if (file.isDir) {
onclick = 'onclick="browse(\'' + file.path + '\')"';
} else if (file.isVideo) {
onclick = 'onclick="playVideo(\'' + file.path + '\', ' + file.canPlay + ')"';
}
return '<div class="file-item" ' + onclick + ' data-path="' + file.path + '">' +
'<span class="icon">' + icon + '</span>' +
'<span>' + file.name + '</span>' +
'</div>';
}).join('');
}
function playVideo(path, canPlayNatively) {
const player = document.getElementById('player');
let videoElement = document.getElementById('activeVideo');
// Highlight selected file
document.querySelectorAll('.file-item').forEach(el => {
el.classList.toggle('active', el.dataset.path === path);
});
const videoUrl = canPlayNatively
? '/api/video/' + encodeURIComponent(path)
: '/api/stream/' + encodeURIComponent(path);
const transcodeNotice = canPlayNatively ? '' :
'<div class="transcoding-notice">Transcoding...</div>';
// If video element already exists, just swap the source
if (videoElement) {
// Update transcode notice
const existingNotice = player.querySelector('.transcoding-notice');
if (transcodeNotice && !existingNotice) {
const noticeDiv = document.createElement('div');
noticeDiv.className = 'transcoding-notice';
noticeDiv.textContent = 'Transcoding...';
player.insertBefore(noticeDiv, videoElement);
} else if (!transcodeNotice && existingNotice) {
existingNotice.remove();
}
// Swap the source
videoElement.src = videoUrl;
videoElement.load();
videoElement.play();
} else {
// First time playing - create the video element
player.innerHTML = transcodeNotice +
'<video controls autoplay id="activeVideo">' +
'<source src="' + videoUrl + '" type="video/mp4">' +
'Your browser does not support the video tag.' +
'</video>';
videoElement = document.getElementById('activeVideo');
// Add event listener for when video ends (only needs to be added once)
videoElement.addEventListener('ended', function() {
playNextVideo();
});
}
currentVideo = path;
}
function playNextVideo() {
// Find the current video in the file list
const currentIndex = allFiles.findIndex(f => f.path === currentVideo);
if (currentIndex === -1) return;
// Find the next video file after the current one
for (let i = currentIndex + 1; i < allFiles.length; i++) {
if (allFiles[i].isVideo && !allFiles[i].isDir) {
// Found next video, play it
playVideo(allFiles[i].path, allFiles[i].canPlay);
// Scroll the file list to show the now-playing video
const fileItems = document.querySelectorAll('.file-item');
const nextItem = Array.from(fileItems).find(
item => item.dataset.path === allFiles[i].path
);
if (nextItem) {
nextItem.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
return;
}
}
// No next video found
console.log('No more videos to play');
}
// Initial load
browse();
</script>
</body>
</html>`
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, tmpl)
}
func needsTranscoding(filePath string) bool {
// Use ffprobe to check audio codec
cmd := exec.Command("ffprobe",
"-v", "error",
"-select_streams", "a:0",
"-show_entries", "stream=codec_name",
"-of", "default=noprint_wrappers=1:nokey=1",
filePath,
)
output, err := cmd.Output()
if err != nil {
// If we can't determine, assume it needs transcoding
return true
}
audioCodec := strings.TrimSpace(string(output))
// Browser-compatible audio codecs
compatibleAudio := map[string]bool{
"aac": true,
"mp3": true,
"opus": true,
"vorbis": true,
}
return !compatibleAudio[audioCodec]
}
func handleBrowse(w http.ResponseWriter, r *http.Request) {
path := r.URL.Query().Get("path")
fullPath := filepath.Join(rootDir, path)
// Security check: ensure we're not escaping the root directory
if !strings.HasPrefix(filepath.Clean(fullPath), filepath.Clean(rootDir)) {
http.Error(w, "Invalid path", http.StatusBadRequest)
return
}
entries, err := os.ReadDir(fullPath)
if err != nil {
http.Error(w, "Cannot read directory", http.StatusInternalServerError)
return
}
var files []FileInfo
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
continue
}
// Skip hidden files
if strings.HasPrefix(entry.Name(), ".") {
continue
}
ext := strings.ToLower(filepath.Ext(entry.Name()))
isVideo := videoFormats[ext]
canPlay := nativeFormats[ext]
needsTranscode := false
relativePath := filepath.Join(path, entry.Name())
fullFilePath := filepath.Join(rootDir, relativePath)
if canPlay && isVideo && !info.IsDir() {
needsTranscode = needsTranscoding(fullFilePath)
if needsTranscode {
canPlay = false // Mark as needing transcode route
}
}
files = append(files, FileInfo{
Name: entry.Name(),
Path: relativePath,
IsDir: info.IsDir(),
IsVideo: isVideo,
CanPlay: canPlay,
NeedsTranscode: needsTranscode,
})
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(files)
}
func handleVideo(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/api/video/")
fullPath := filepath.Join(rootDir, path)
// Security check
if !strings.HasPrefix(filepath.Clean(fullPath), filepath.Clean(rootDir)) {
http.Error(w, "Invalid path", http.StatusBadRequest)
return
}
// Serve the file directly
http.ServeFile(w, r, fullPath)
}
func handleStream(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/api/stream/")
fullPath := filepath.Join(rootDir, path)
// Security check
if !strings.HasPrefix(filepath.Clean(fullPath), filepath.Clean(rootDir)) {
http.Error(w, "Invalid path", http.StatusBadRequest)
return
}
// Check if file exists
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
http.Error(w, "File not found", http.StatusNotFound)
return
}
// Kill any existing transcoding process before starting a new one
transcodeMutex.Lock()
if activeCmd != nil && activeCmd.Process != nil {
log.Printf("Killing existing ffmpeg process to start new transcode")
activeCmd.Process.Kill()
activeCmd.Wait() // Wait for it to fully exit
activeCmd = nil
}
transcodeMutex.Unlock()
// Set headers for streaming
w.Header().Set("Content-Type", "video/mp4")
w.Header().Set("Cache-Control", "no-cache")
// FFmpeg command to transcode to H.264/AAC MP4
cmd := exec.Command("ffmpeg",
"-re", // Read input at native frame rate
"-i", fullPath,
"-map", "0:v:0", // First video stream only
"-map", "0:a:0", // First audio stream only
"-c:v", "libx264",
"-preset", "ultrafast",
"-tune", "zerolatency",
"-crf", "23",
"-maxrate", "3M",
"-bufsize", "6M",
"-pix_fmt", "yuv420p",
"-c:a", "aac",
"-b:a", "128k",
"-ac", "2", // Stereo audio
"-movflags", "frag_keyframe+empty_moov+faststart",
"-f", "mp4",
"-loglevel", "warning",
"pipe:1",
)
// Track this as the active command
transcodeMutex.Lock()
activeCmd = cmd
transcodeMutex.Unlock()
// Capture stderr for debugging
stderr, err := cmd.StderrPipe()
if err != nil {
log.Printf("Error creating stderr pipe: %v", err)
http.Error(w, "Transcoding error", http.StatusInternalServerError)
return
}
// Get stdout pipe
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Printf("Error creating stdout pipe: %v", err)
http.Error(w, "Transcoding error", http.StatusInternalServerError)
return
}
// Start the command
if err := cmd.Start(); err != nil {
log.Printf("Error starting ffmpeg: %v", err)
http.Error(w, "Transcoding error", http.StatusInternalServerError)
return
}
// Log stderr in background
go func() {
buf := make([]byte, 4096)
for {
n, err := stderr.Read(buf)
if n > 0 {
log.Printf("FFmpeg: %s", string(buf[:n]))
}
if err != nil {
break
}
}
}()
// Monitor for client disconnect and kill ffmpeg if needed
done := make(chan bool)
go func() {
// Copy output to response
_, err = io.Copy(w, stdout)
if err != nil {
log.Printf("Error streaming video: %v", err)
}
done <- true
}()
// Wait for either completion or context cancellation
select {
case <-done:
// Streaming finished normally
case <-r.Context().Done():
// Client disconnected
log.Printf("Client disconnected, killing ffmpeg process for: %s", path)
if err := cmd.Process.Kill(); err != nil {
log.Printf("Error killing ffmpeg: %v", err)
}
}
// Clean up active command reference
transcodeMutex.Lock()
if activeCmd == cmd {
activeCmd = nil
}
transcodeMutex.Unlock()
// Wait for command to finish
if err := cmd.Wait(); err != nil {
// Don't log error if we killed the process intentionally
if r.Context().Err() == nil {
log.Printf("FFmpeg error: %v", err)
}
}
}