2023-06-13 00:07:14 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-02-24 21:47:18 +02:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2024-02-23 23:35:12 +02:00
|
|
|
"encoding/json"
|
2023-06-13 00:07:14 +03:00
|
|
|
"io/fs"
|
2024-02-23 23:35:12 +02:00
|
|
|
"os"
|
2023-06-13 00:07:14 +03:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2024-02-23 23:35:12 +02:00
|
|
|
|
|
|
|
"github.com/BatteredBunny/rjson"
|
|
|
|
ffmpeg "github.com/u2takey/ffmpeg-go"
|
2023-06-13 00:07:14 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func listFilesRecursively(findPath string, hide_hidden bool) (filelist []string, err error) {
|
|
|
|
err = filepath.Walk(findPath, func(currentPath string, info fs.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !info.IsDir() {
|
|
|
|
if hide_hidden && strings.HasPrefix(path.Base(currentPath), ".") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
filelist = append(filelist, currentPath)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2024-02-23 23:35:12 +02:00
|
|
|
|
2024-02-24 16:54:27 +02:00
|
|
|
func RecreateDir(dir string) (err error) {
|
|
|
|
if err = os.RemoveAll(dir); err != nil {
|
2024-02-23 23:35:12 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-24 16:54:27 +02:00
|
|
|
os.MkdirAll(dir, 0700)
|
2024-02-23 23:35:12 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-25 17:26:48 +02:00
|
|
|
type StreamInfo struct {
|
|
|
|
Index int `json:"index"`
|
|
|
|
CodecName string `json:"codec_name"`
|
|
|
|
CodecType string `json:"codec_type"`
|
|
|
|
Disposition StreamInfoDisposition `json:"disposition"`
|
|
|
|
Tags StreamInfoTags `json:"tags"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type StreamInfoDisposition struct {
|
|
|
|
HearingImpaired int `json:"hearing_impaired"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type StreamInfoTags struct {
|
|
|
|
Language string `json:"language"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func SelectPreferredSubtitle(filename string) (*int, error) {
|
|
|
|
rawMetadata, err := ffmpeg.Probe(filename)
|
2024-02-23 23:35:12 +02:00
|
|
|
if err != nil {
|
2024-02-25 17:26:48 +02:00
|
|
|
return nil, err
|
2024-02-23 23:35:12 +02:00
|
|
|
}
|
|
|
|
|
2024-02-25 17:26:48 +02:00
|
|
|
metadata, err := rjson.QueryJson([]byte(rawMetadata), `.streams.[]`)
|
2024-02-23 23:35:12 +02:00
|
|
|
if err != nil {
|
2024-02-25 17:26:48 +02:00
|
|
|
return nil, err
|
2024-02-23 23:35:12 +02:00
|
|
|
}
|
|
|
|
|
2024-02-25 17:26:48 +02:00
|
|
|
var streams []StreamInfo
|
|
|
|
if err = json.Unmarshal(metadata, &streams); err != nil {
|
|
|
|
return nil, err
|
2024-02-23 23:35:12 +02:00
|
|
|
}
|
|
|
|
|
2024-02-25 17:26:48 +02:00
|
|
|
var numberOfSubStreams int
|
|
|
|
for _, stream := range streams {
|
|
|
|
if stream.CodecType == "subtitle" {
|
|
|
|
numberOfSubStreams += 1
|
2024-02-23 23:35:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-25 21:08:04 +02:00
|
|
|
streamNumberModifier := len(streams) - numberOfSubStreams
|
2024-02-25 17:26:48 +02:00
|
|
|
|
|
|
|
var bestSubtitle StreamInfo
|
|
|
|
var foundSubtitle bool
|
|
|
|
for _, stream := range streams {
|
|
|
|
// Skips dvd_subtitle or picture based subtitles as i dont know how to hardcode them
|
|
|
|
if stream.CodecType != "subtitle" || stream.CodecName == "dvd_subtitle" || stream.Tags.Language != "eng" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if bestSubtitle.Disposition.HearingImpaired == 0 && stream.Disposition.HearingImpaired == 1 {
|
|
|
|
bestSubtitle = stream
|
|
|
|
foundSubtitle = true
|
|
|
|
} else if bestSubtitle.Disposition.HearingImpaired == 1 && stream.Disposition.HearingImpaired == 0 {
|
|
|
|
// Skips if bestSubtitle is HearingImpaired and current one isnt
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
bestSubtitle = stream
|
|
|
|
foundSubtitle = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if foundSubtitle {
|
2024-02-25 21:08:04 +02:00
|
|
|
actualStreamIndex := bestSubtitle.Index - streamNumberModifier
|
|
|
|
|
|
|
|
return &actualStreamIndex, nil
|
2024-02-25 17:26:48 +02:00
|
|
|
} else {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2024-02-23 23:35:12 +02:00
|
|
|
}
|
2024-02-24 21:47:18 +02:00
|
|
|
|
|
|
|
func ToColor(s string) (color string) {
|
|
|
|
hasher := sha256.New()
|
|
|
|
hasher.Write([]byte(s))
|
|
|
|
|
|
|
|
color = hex.EncodeToString(hasher.Sum(nil))
|
|
|
|
color = color[0:6]
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|