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

View File

@@ -24,8 +24,8 @@ type API struct {
Tmpfs *tmpfs.Tmpfs
}
type FfmpegConfigs struct {
FfmpegConfigs map[string]*FfmpegConfig `json:"ffmpeg_configs"`
type FfmpegConfigList struct {
FfmpegConfigList []FfmpegConfig `json:"ffmpeg_config_list"`
}
type AddFfmpegConfigRequest struct {
@@ -35,6 +35,7 @@ type AddFfmpegConfigRequest struct {
}
type FfmpegConfig struct {
Name string `json:"name"`
Args string `json:"args"`
}
@@ -371,6 +372,19 @@ func (api *API) CheckGetFileStream(w http.ResponseWriter, r *http.Request) (erro
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) {
err := api.CheckGetFileStream(w, r)
if err != nil {
@@ -395,7 +409,7 @@ func (api *API) HandleGetFileStream(w http.ResponseWriter, r *http.Request) {
log.Println("[api] Stream file", path, configName)
ffmpegConfig, ok := api.APIConfig.FfmpegConfigs[configName]
ffmpegConfig, ok := api.GetFfmpegConfig(configName)
if !ok {
api.HandleErrorStringCode(w, r, `ffmpeg config not found`, 404)
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)
ffmpegConfig, ok := api.APIConfig.FfmpegConfigs[prepareFileStreamDirectRequst.ConfigName]
ffmpegConfig, ok := api.GetFfmpegConfig(prepareFileStreamDirectRequst.ConfigName)
if !ok {
api.HandleErrorStringCode(w, r, `ffmpeg config not found`, 404)
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) {
log.Println("[api] Get ffmpeg config list")
ffmpegConfigs:= &FfmpegConfigs{
FfmpegConfigs: api.APIConfig.FfmpegConfigs,
ffmpegConfigList:= &FfmpegConfigList{
FfmpegConfigList: api.APIConfig.FfmpegConfigList,
}
json.NewEncoder(w).Encode(&ffmpegConfigs)
json.NewEncoder(w).Encode(&ffmpegConfigList)
}
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")
api.APIConfig.FfmpegConfigs[addFfmpegConfigRequest.Name] = &addFfmpegConfigRequest.FfmpegConfig
api.APIConfig.FfmpegConfigList = append(api.APIConfig.FfmpegConfigList, addFfmpegConfigRequest.FfmpegConfig)
api.HandleOK(w, r)
}
@@ -666,9 +681,7 @@ func (api *API) HandleFeedback(w http.ResponseWriter, r *http.Request) {
}
func NewAPIConfig() (APIConfig) {
apiConfig := APIConfig{
FfmpegConfigs: make(map[string]*FfmpegConfig),
}
apiConfig := APIConfig{}
return apiConfig
}
@@ -677,12 +690,20 @@ type APIConfig struct {
Addr string `json:"addr"`
Token string `json:"token"`
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
apiConfig := config.APIConfig
tmpfsConfig := config.TmpfsConfig
db, err := database.NewDatabase(apiConfig.DatabaseName)
if err != nil {
return nil, err

View File

@@ -5,7 +5,6 @@ import (
"flag"
"log"
"msw-open-music/internal/pkg/api"
"msw-open-music/internal/pkg/tmpfs"
"os"
)
@@ -15,16 +14,12 @@ func init() {
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() {
var err error
flag.Parse()
config := Config{}
config := api.Config{}
configFile, err := os.Open(ConfigFilePath)
if err != nil {
log.Fatal(err)
@@ -35,7 +30,7 @@ func main() {
}
configFile.Close()
api, err := api.NewAPI(config.APIConfig, config.TmpfsConfig)
api, err := api.NewAPI(config)
if err != nil {
log.Fatal(err)
}