package scanner import ( "encoding/json" "os" "path" "strconv" "strings" "time" "github.com/pelletier/go-toml/v2" ffmpeg_go "github.com/u2takey/ffmpeg-go" ) type ( Category struct { Name string `toml:"name"` Type string `toml:"type" comment:"Valid types: gaming, other (default: other)"` Url string `toml:"url"` } Metadata struct { Title string `toml:"title"` Part int `toml:"part"` Date string `toml:"date"` Tags []string `toml:"tags"` FootageDir string `toml:"footage_dir"` Uploaded bool `toml:"uploaded"` Category *Category `toml:"category" comment:"(Optional) Category details, for additional credits."` } FFprobeFormat struct { Duration float64 `json:"duration"` Size int64 `json:"size"` } FFprobeOutput struct { Format FFprobeFormat `json:"format"` } ) const METADATA_FILENAME = "metadata.toml" func ScanSegments(directory string, extension string) ([]string, error) { entries, err := os.ReadDir(directory) if err != nil { return nil, err } files := []string{} for _, item := range entries { if item.IsDir() { continue } if strings.HasPrefix(item.Name(), ".") { continue } if !strings.HasSuffix(item.Name(), "." + extension) { continue } if strings.HasSuffix(item.Name(), "-fullvod." + extension) { continue } files = append(files, item.Name()) } return files, nil } func ProbeSegment(filename string) (*FFprobeOutput, error) { out, err := ffmpeg_go.Probe(filename) if err != nil { return nil, err } type ( RawFFprobeFormat struct { // these being strings upsets me immensely Duration string `json:"duration"` Size string `json:"size"` } RawFFprobeOutput struct { Format RawFFprobeFormat `json:"format"` } ) probe := RawFFprobeOutput{} err = json.Unmarshal([]byte(out), &probe) if err != nil { return nil, err } duration, err := strconv.ParseFloat(probe.Format.Duration, 64) if err != nil { return nil, err } size, err := strconv.ParseInt(probe.Format.Size, 10, 0) if err != nil { return nil, err } return &FFprobeOutput{ Format: FFprobeFormat{ Duration: duration, Size: size, }, }, nil } func ReadMetadata(directory string) (*Metadata, error) { metadata := &Metadata{} file, err := os.OpenFile( path.Join(directory, METADATA_FILENAME), os.O_RDONLY, os.ModePerm, ) if err != nil { return nil, err } err = toml.NewDecoder(file).Decode(metadata) if err != nil { return nil, err } return metadata, nil } func WriteMetadata(directory string, metadata *Metadata) (error) { file, err := os.OpenFile( path.Join(directory, METADATA_FILENAME), os.O_CREATE | os.O_RDWR, 0644, ) if err != nil { return err } err = toml.NewEncoder(file).Encode(metadata) return err } func DefaultMetadata() *Metadata { return &Metadata{ Title: "Untitled Stream", Date: time.Now().Format("2006-01-02"), Part: 0, Category: &Category{ Name: "Something", Type: "", Url: "", }, } }