2024-10-22 16:39:10 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"live-streamer/config"
|
|
|
|
"live-streamer/streamer"
|
|
|
|
"live-streamer/utils"
|
|
|
|
"log"
|
|
|
|
"os"
|
2024-10-23 04:37:43 -04:00
|
|
|
"strings"
|
2024-10-22 16:39:10 -04:00
|
|
|
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
)
|
|
|
|
|
|
|
|
var GlobalStreamer *streamer.Streamer
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if !utils.HasFFMPEG() {
|
|
|
|
log.Fatal("ffmpeg not found")
|
|
|
|
}
|
2024-10-23 05:11:52 -04:00
|
|
|
GlobalStreamer = streamer.NewStreamer(config.GlobalConfig.VideoList)
|
2024-10-22 16:39:10 -04:00
|
|
|
go input()
|
|
|
|
go startWatcher()
|
|
|
|
GlobalStreamer.Stream()
|
|
|
|
GlobalStreamer.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func input() {
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
for scanner.Scan() {
|
|
|
|
switch scanner.Text() {
|
|
|
|
case "prev":
|
|
|
|
GlobalStreamer.Prev()
|
|
|
|
case "next":
|
|
|
|
GlobalStreamer.Next()
|
2024-10-23 04:37:43 -04:00
|
|
|
case "quit":
|
|
|
|
GlobalStreamer.Close()
|
|
|
|
os.Exit(0)
|
|
|
|
case "list":
|
|
|
|
list := GlobalStreamer.GetVideoListPath()
|
2024-10-23 05:08:05 -04:00
|
|
|
log.Println("\nvideo list:\n", strings.Join(list, "\n"))
|
|
|
|
case "current":
|
|
|
|
log.Println("current video: ", GlobalStreamer.GetCurrentVideo())
|
2024-10-22 16:39:10 -04:00
|
|
|
default:
|
|
|
|
log.Println("unknown command")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startWatcher() {
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to create watcher: %v", err)
|
|
|
|
}
|
|
|
|
defer watcher.Close()
|
|
|
|
|
2024-10-23 05:11:52 -04:00
|
|
|
for _, item := range config.GlobalConfig.InputItems {
|
2024-10-22 16:39:10 -04:00
|
|
|
if item.ItemType == "dir" {
|
|
|
|
err = watcher.Add(item.Path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to add dir to watcher: %v", err)
|
|
|
|
}
|
|
|
|
log.Println("watching dir:", item.Path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to start watcher: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event, ok := <-watcher.Events:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if event.Op&fsnotify.Create == fsnotify.Create {
|
|
|
|
if utils.IsSupportedVideo(event.Name) {
|
2024-10-23 05:08:05 -04:00
|
|
|
log.Println("new video added:", event.Name)
|
2024-10-22 16:39:10 -04:00
|
|
|
GlobalStreamer.Add(event.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if event.Op&fsnotify.Remove == fsnotify.Remove {
|
2024-10-23 05:08:05 -04:00
|
|
|
log.Println("video removed:", event.Name)
|
2024-10-22 16:39:10 -04:00
|
|
|
GlobalStreamer.Remove(event.Name)
|
|
|
|
}
|
|
|
|
case err, ok := <-watcher.Errors:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Println("watcher error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|