Break: change ffmpeg_configs to ffmpeg_config_list structure

This commit is contained in:
2021-11-14 23:18:09 +08:00
parent b1fb8b0866
commit d556bbe0c8
3 changed files with 46 additions and 30 deletions

View File

@@ -4,16 +4,16 @@
"addr": ":8080", "addr": ":8080",
"token": "!! config your very strong token here !!", "token": "!! config your very strong token here !!",
"ffmpeg_threads": 1, "ffmpeg_threads": 1,
"ffmpeg_configs": { "ffmpeg_config_list": [
"0. OPUS 128k": {"args": "-c:a libopus -ab 128k"}, {"name": "OPUS 128k", "args": "-c:a libopus -ab 128k"},
"1. OPUS 96k": {"args": "-c:a libopus -ab 96k"}, {"name": "OPUS 96k", "args": "-c:a libopus -ab 96k"},
"2. OPUS 256k": {"args": "-c:a libopus -ab 256k"}, {"name": "OPUS 256k", "args": "-c:a libopus -ab 256k"},
"3. OPUS 320k": {"args": "-c:a libopus -ab 320k"}, {"name": "OPUS 320k", "args": "-c:a libopus -ab 320k"},
"4. OPUS 512k": {"args": "-c:a libopus -ab 512k"}, {"name": "OPUS 512k", "args": "-c:a libopus -ab 512k"},
"5. AAC 128k": {"args": "-c:a aac -ab 128k"}, {"name": "AAC 128k", "args": "-c:a aac -ab 128k"},
"6. AAC 256k": {"args": "-c:a aac -ab 256k"}, {"name": "AAC 256k", "args": "-c:a aac -ab 256k"},
"7. 全损音质 32k": {"args": "-c:a libopus -ab 32k"} {"name": "全损音质 32k", "args": "-c:a libopus -ab 32k"}
} ]
}, },
"tmpfs": { "tmpfs": {
"file_life_time": 600, "file_life_time": 600,

View File

@@ -24,8 +24,8 @@ type API struct {
Tmpfs *tmpfs.Tmpfs Tmpfs *tmpfs.Tmpfs
} }
type FfmpegConfigs struct { type FfmpegConfigList struct {
FfmpegConfigs map[string]*FfmpegConfig `json:"ffmpeg_configs"` FfmpegConfigList []FfmpegConfig `json:"ffmpeg_config_list"`
} }
type AddFfmpegConfigRequest struct { type AddFfmpegConfigRequest struct {
@@ -35,6 +35,7 @@ type AddFfmpegConfigRequest struct {
} }
type FfmpegConfig struct { type FfmpegConfig struct {
Name string `json:"name"`
Args string `json:"args"` Args string `json:"args"`
} }
@@ -371,6 +372,19 @@ func (api *API) CheckGetFileStream(w http.ResponseWriter, r *http.Request) (erro
return nil return nil
} }
func (api *API) GetFfmpegConfig(configName string) (FfmpegConfig, bool) {
ffmpegConfig := FfmpegConfig{}
for _, f := range api.APIConfig.FfmpegConfigList {
if f.Name == configName {
ffmpegConfig = f
}
}
if ffmpegConfig.Name == "" {
return ffmpegConfig, false
}
return ffmpegConfig, true
}
func (api *API) HandleGetFileStream(w http.ResponseWriter, r *http.Request) { func (api *API) HandleGetFileStream(w http.ResponseWriter, r *http.Request) {
err := api.CheckGetFileStream(w, r) err := api.CheckGetFileStream(w, r)
if err != nil { if err != nil {
@@ -395,7 +409,7 @@ func (api *API) HandleGetFileStream(w http.ResponseWriter, r *http.Request) {
log.Println("[api] Stream file", path, configName) log.Println("[api] Stream file", path, configName)
ffmpegConfig, ok := api.APIConfig.FfmpegConfigs[configName] ffmpegConfig, ok := api.GetFfmpegConfig(configName)
if !ok { if !ok {
api.HandleErrorStringCode(w, r, `ffmpeg config not found`, 404) api.HandleErrorStringCode(w, r, `ffmpeg config not found`, 404)
return return
@@ -455,7 +469,7 @@ func (api *API) HandlePrepareFileStreamDirect(w http.ResponseWriter, r *http.Req
} }
log.Println("[api] Prepare stream direct file", srcPath, prepareFileStreamDirectRequst.ConfigName) log.Println("[api] Prepare stream direct file", srcPath, prepareFileStreamDirectRequst.ConfigName)
ffmpegConfig, ok := api.APIConfig.FfmpegConfigs[prepareFileStreamDirectRequst.ConfigName] ffmpegConfig, ok := api.GetFfmpegConfig(prepareFileStreamDirectRequst.ConfigName)
if !ok { if !ok {
api.HandleErrorStringCode(w, r, `ffmpeg config not found`, 404) api.HandleErrorStringCode(w, r, `ffmpeg config not found`, 404)
return return
@@ -593,10 +607,10 @@ func (api *API) HandleGetFile(w http.ResponseWriter, r *http.Request) {
func (api *API) HandleGetFfmpegConfigs(w http.ResponseWriter, r *http.Request) { func (api *API) HandleGetFfmpegConfigs(w http.ResponseWriter, r *http.Request) {
log.Println("[api] Get ffmpeg config list") log.Println("[api] Get ffmpeg config list")
ffmpegConfigs:= &FfmpegConfigs{ ffmpegConfigList:= &FfmpegConfigList{
FfmpegConfigs: api.APIConfig.FfmpegConfigs, FfmpegConfigList: api.APIConfig.FfmpegConfigList,
} }
json.NewEncoder(w).Encode(&ffmpegConfigs) json.NewEncoder(w).Encode(&ffmpegConfigList)
} }
func (api *API) HandleAddFfmpegConfig(w http.ResponseWriter, r *http.Request) { func (api *API) HandleAddFfmpegConfig(w http.ResponseWriter, r *http.Request) {
@@ -625,7 +639,8 @@ func (api *API) HandleAddFfmpegConfig(w http.ResponseWriter, r *http.Request) {
log.Println("[api] Add ffmpeg config") log.Println("[api] Add ffmpeg config")
api.APIConfig.FfmpegConfigs[addFfmpegConfigRequest.Name] = &addFfmpegConfigRequest.FfmpegConfig api.APIConfig.FfmpegConfigList = append(api.APIConfig.FfmpegConfigList, addFfmpegConfigRequest.FfmpegConfig)
api.HandleOK(w, r) api.HandleOK(w, r)
} }
@@ -666,9 +681,7 @@ func (api *API) HandleFeedback(w http.ResponseWriter, r *http.Request) {
} }
func NewAPIConfig() (APIConfig) { func NewAPIConfig() (APIConfig) {
apiConfig := APIConfig{ apiConfig := APIConfig{}
FfmpegConfigs: make(map[string]*FfmpegConfig),
}
return apiConfig return apiConfig
} }
@@ -677,12 +690,20 @@ type APIConfig struct {
Addr string `json:"addr"` Addr string `json:"addr"`
Token string `json:"token"` Token string `json:"token"`
FfmpegThreads int64 `json:"ffmpeg_threads"` FfmpegThreads int64 `json:"ffmpeg_threads"`
FfmpegConfigs map[string]*FfmpegConfig `json:"ffmpeg_configs"` FfmpegConfigList []FfmpegConfig `json:"ffmpeg_config_list"`
} }
func NewAPI(apiConfig APIConfig, tmpfsConfig tmpfs.TmpfsConfig) (*API, error) { type Config struct {
APIConfig APIConfig `json:"api"`
TmpfsConfig tmpfs.TmpfsConfig `json:"tmpfs"`
}
func NewAPI(config Config) (*API, error) {
var err error var err error
apiConfig := config.APIConfig
tmpfsConfig := config.TmpfsConfig
db, err := database.NewDatabase(apiConfig.DatabaseName) db, err := database.NewDatabase(apiConfig.DatabaseName)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -5,7 +5,6 @@ import (
"flag" "flag"
"log" "log"
"msw-open-music/internal/pkg/api" "msw-open-music/internal/pkg/api"
"msw-open-music/internal/pkg/tmpfs"
"os" "os"
) )
@@ -15,16 +14,12 @@ func init() {
flag.StringVar(&ConfigFilePath, "config", "config.json", "backend config file path") flag.StringVar(&ConfigFilePath, "config", "config.json", "backend config file path")
} }
type Config struct {
APIConfig api.APIConfig `json:"api"`
TmpfsConfig tmpfs.TmpfsConfig `json:"tmpfs"`
}
func main() { func main() {
var err error var err error
flag.Parse() flag.Parse()
config := Config{} config := api.Config{}
configFile, err := os.Open(ConfigFilePath) configFile, err := os.Open(ConfigFilePath)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@@ -35,7 +30,7 @@ func main() {
} }
configFile.Close() configFile.Close()
api, err := api.NewAPI(config.APIConfig, config.TmpfsConfig) api, err := api.NewAPI(config)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }