adjust: code structure
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"live-streamer/model"
|
||||
"live-streamer/utils"
|
||||
"log"
|
||||
"os"
|
||||
@@ -16,16 +17,9 @@ type OutputConfig struct {
|
||||
StreamKey string `json:"stream_key"`
|
||||
}
|
||||
|
||||
type InputItem struct {
|
||||
Path string `json:"path"`
|
||||
Start string `json:"start"`
|
||||
End string `json:"end"`
|
||||
ItemType string `json:"-"`
|
||||
}
|
||||
|
||||
type LogConfig struct {
|
||||
Level string `json:"level"`
|
||||
PlayState bool `json:"play_state"`
|
||||
Level string `json:"level"`
|
||||
ShowFFmpegOutput bool `json:"show_ffmpeg_output"`
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
@@ -34,23 +28,20 @@ type ServerConfig struct {
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Input []any `json:"input"`
|
||||
InputItems []InputItem `json:"-"` // contains video file or dir
|
||||
VideoList []InputItem `json:"-"` // only contains video file
|
||||
Play map[string]any `json:"play"`
|
||||
Output OutputConfig `json:"output"`
|
||||
Log LogConfig `json:"log"`
|
||||
Server ServerConfig `json:"server"`
|
||||
Input []any `json:"input"`
|
||||
InputItems []model.VideoItem `json:"-"` // contains video file or dir
|
||||
VideoList []model.VideoItem `json:"-"` // only contains video file
|
||||
Play map[string]any `json:"play"`
|
||||
Output OutputConfig `json:"output"`
|
||||
Log LogConfig `json:"log"`
|
||||
Server ServerConfig `json:"server"`
|
||||
}
|
||||
|
||||
var GlobalConfig *Config
|
||||
|
||||
func init() {
|
||||
GlobalConfig = &Config{}
|
||||
err := readConfig("config.json")
|
||||
if len(GlobalConfig.Input) == 0 {
|
||||
log.Fatal("No input video found")
|
||||
}
|
||||
err := readConfig("./config.json")
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
log.Fatal("Config not exists")
|
||||
@@ -58,6 +49,10 @@ func init() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(GlobalConfig.Input) == 0 {
|
||||
log.Fatal("no input video found")
|
||||
}
|
||||
}
|
||||
|
||||
func readConfig(configPath string) error {
|
||||
@@ -70,9 +65,9 @@ func readConfig(configPath string) error {
|
||||
}
|
||||
databytes, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Config read failed: %v", err)
|
||||
return fmt.Errorf("config read failed: %v", err)
|
||||
}
|
||||
if err = json.Unmarshal(databytes, &GlobalConfig); err != nil {
|
||||
if err = json.Unmarshal(databytes, GlobalConfig); err != nil {
|
||||
return fmt.Errorf("config unmarshal failed: %v", err)
|
||||
}
|
||||
err = validateConfig()
|
||||
@@ -107,15 +102,15 @@ func validateInputConfig() error {
|
||||
return errors.New("video_path is nil")
|
||||
}
|
||||
|
||||
GlobalConfig.InputItems = make([]InputItem, 0, len(GlobalConfig.Input))
|
||||
GlobalConfig.VideoList = []InputItem{}
|
||||
GlobalConfig.InputItems = make([]model.VideoItem, 0, len(GlobalConfig.Input))
|
||||
GlobalConfig.VideoList = []model.VideoItem{}
|
||||
|
||||
for i, item := range GlobalConfig.Input {
|
||||
var inputItem InputItem
|
||||
var inputItem model.VideoItem
|
||||
|
||||
switch v := item.(type) {
|
||||
case string:
|
||||
inputItem = InputItem{Path: v}
|
||||
inputItem = model.VideoItem{Path: v}
|
||||
case map[string]any:
|
||||
data, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
@@ -192,14 +187,14 @@ func validateServerConfig() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func getAllVideos(dirPath string) ([]InputItem, error) {
|
||||
res := []InputItem{}
|
||||
func getAllVideos(dirPath string) ([]model.VideoItem, error) {
|
||||
res := []model.VideoItem{}
|
||||
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.IsDir() && utils.IsSupportedVideo(path) {
|
||||
res = append(res, InputItem{Path: path})
|
||||
res = append(res, model.VideoItem{Path: path})
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user