live-streamer/main.go

99 lines
2.1 KiB
Go
Raw Permalink Normal View History

2024-10-22 16:39:10 -04:00
package main
import (
2024-10-23 17:06:19 -04:00
"bufio"
"fmt"
2024-10-24 04:43:06 -04:00
2024-10-22 16:39:10 -04:00
"live-streamer/config"
2024-10-24 04:43:06 -04:00
"live-streamer/constant"
2024-10-23 10:47:37 -04:00
"live-streamer/server"
2024-10-22 16:39:10 -04:00
"live-streamer/streamer"
"live-streamer/utils"
2024-10-24 04:23:50 -04:00
"live-streamer/websocket"
2024-10-22 16:39:10 -04:00
"log"
2024-10-23 17:06:19 -04:00
"os"
2024-10-22 16:39:10 -04:00
"github.com/fsnotify/fsnotify"
)
var GlobalStreamer *streamer.Streamer
func main() {
2024-10-24 04:43:06 -04:00
fmt.Println("Version: " + constant.Version)
server.NewServer(config.GlobalConfig.Server.Addr, websocket.RequestHandler)
2024-10-23 10:47:37 -04:00
server.GlobalServer.Run()
2024-10-22 16:39:10 -04:00
if !utils.HasFFMPEG() {
log.Fatal("ffmpeg not found")
}
2024-10-23 17:06:19 -04:00
GlobalStreamer = streamer.NewStreamer(config.GlobalConfig.VideoList)
2024-10-22 16:39:10 -04:00
go startWatcher()
2024-10-23 17:06:19 -04:00
go input()
2024-10-22 16:39:10 -04:00
GlobalStreamer.Stream()
GlobalStreamer.Close()
}
2024-10-23 17:06:19 -04:00
func input() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
2024-10-24 04:43:06 -04:00
line := scanner.Text()
2024-10-23 17:06:19 -04:00
switch line {
case "list":
fmt.Println(GlobalStreamer.GetVideoListPath())
case "index":
fmt.Println(GlobalStreamer.GetCurrentIndex())
case "next":
GlobalStreamer.Next()
case "prev":
GlobalStreamer.Prev()
case "quit":
GlobalStreamer.Close()
case "current":
fmt.Println(GlobalStreamer.GetCurrentVideoPath())
}
}
}
2024-10-22 16:39:10 -04:00
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)
}
2024-10-23 14:35:37 -04:00
log.Println("watching dir:", item.Path)
2024-10-22 16:39:10 -04:00
}
}
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 14:35:37 -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 14:35:37 -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
}
2024-10-23 14:35:37 -04:00
log.Println("watcher error:", err)
2024-10-22 16:39:10 -04:00
}
}
}