add config.yaml support

pull/14/head
itouakirai 7 months ago committed by GitHub
parent b726af587e
commit e384d06c09
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 45
      main_atmos.go

@ -4,6 +4,7 @@ import (
"bytes"
"encoding/binary"
"encoding/json"
"gopkg.in/yaml.v2"
"errors"
"fmt"
"github.com/beevik/etree"
@ -34,6 +35,18 @@ const (
var (
forbiddenNames = regexp.MustCompile(`[/\\<>:"|?*]`)
)
type Config struct {
MediaUserToken string `yaml:"media-user-token"`
SaveLrcFile bool `yaml:"save-lrc-file"`
EmbedLrc bool `yaml:"embed-lrc"`
EmbedCover bool `yaml:"embed-cover"`
CoverSize string `yaml:"cover-size"`
CoverFormat string `yaml:"cover-format"`
AlacSaveFolder string `yaml:"alac-save-folder"`
AtmosSaveFolder string `yaml:"atmos-save-folder"`
}
var config Config
var oktrackNum int = 0
var trackTotalnum int = 0
@ -49,6 +62,20 @@ type SongInfo struct {
samples []SampleInfo
}
func loadConfig() error {
// 读取config.yaml文件内容
data, err := ioutil.ReadFile("config.yaml")
if err != nil {
return err
}
// 将yaml解析到config变量中
err = yaml.Unmarshal(data, &config)
if err != nil {
return err
}
return nil
}
func (s *SongInfo) Duration() (ret uint64) {
for i := range s.samples {
ret += uint64(s.samples[i].duration)
@ -1027,7 +1054,7 @@ func getSongLyrics(songId string, storefront string, token string, userToken str
}
func writeCover(sanAlbumFolder, url string) error {
covPath := filepath.Join(sanAlbumFolder, "cover.jpg")
covPath := filepath.Join(sanAlbumFolder, "cover." + config.CoverFormat)
exists, err := fileExists(covPath)
if err != nil {
fmt.Println("Failed to check if cover exists.")
@ -1036,7 +1063,7 @@ func writeCover(sanAlbumFolder, url string) error {
if exists {
return nil
}
url = strings.Replace(url, "{w}x{h}", "5000x5000", 1)
url = strings.Replace(url, "{w}x{h}", config.CoverSize, 1)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
@ -1087,7 +1114,7 @@ func rip(albumId string, token string, storefront string, userToken string) erro
singerFoldername = strings.ReplaceAll(singerFoldername, ".", "")
}
singerFoldername = strings.TrimSpace(singerFoldername)
singerFolder := filepath.Join("AM-DL-Atmos downloads", forbiddenNames.ReplaceAllString(singerFoldername, "_"))
singerFolder := filepath.Join(config.AtmosSaveFolder, forbiddenNames.ReplaceAllString(singerFoldername, "_"))
albumFolder := fmt.Sprintf("%s [Atmos]", meta.Data[0].Attributes.Name)
if strings.HasSuffix(albumFolder, ".") {
albumFolder = strings.ReplaceAll(albumFolder, ".", "")
@ -1134,7 +1161,7 @@ func rip(albumId string, token string, storefront string, userToken string) erro
continue
}
var lrc string = ""
if userToken != "" {
if userToken != "" && (config.EmbedLrc || config.SaveLrcFile) {
ttml, err := getSongLyrics(track.ID, storefront, token, userToken)
if err != nil {
fmt.Println("Failed to get lyrics")
@ -1143,10 +1170,15 @@ func rip(albumId string, token string, storefront string, userToken string) erro
if err != nil {
fmt.Printf("Failed to parse lyrics: %s \n", err)
} else {
if config.SaveLrcFile {
err := writeLyrics(sanAlbumFolder, lrcFilename, lrc)
if err != nil {
fmt.Printf("Failed to write lyrics")
}
if !config.EmbedLrc {
lrc = ""
}
}
}
}
}
@ -1182,7 +1214,6 @@ func rip(albumId string, token string, storefront string, userToken string) erro
tags := []string{
"tool=",
fmt.Sprintf("lyrics=%s", lrc),
fmt.Sprintf("cover=%s/cover.jpg", sanAlbumFolder),
fmt.Sprintf("album=%s", meta.Data[0].Attributes.Name),
fmt.Sprintf("title=%s", meta.Data[0].Relationships.Tracks.Data[index].Attributes.Name),
fmt.Sprintf("artist=%s", meta.Data[0].Relationships.Tracks.Data[index].Attributes.ArtistName),
@ -1195,7 +1226,9 @@ func rip(albumId string, token string, storefront string, userToken string) erro
fmt.Sprintf("UPC=%s", meta.Data[0].Attributes.Upc),
fmt.Sprintf("track=%d/%d", trackNum, trackTotal),
}
if config.EmbedCover {
tags = append(tags, fmt.Sprintf("cover=%s/cover.%s", sanAlbumFolder, config.CoverFormat))
}
tagsString := strings.Join(tags, ":")
cmd := exec.Command("MP4Box", "-add", trackPath, "-name", fmt.Sprintf("1=%s", meta.Data[0].Relationships.Tracks.Data[index].Attributes.Name), "-itags", tagsString, "-brand", "mp42", "-ab", "dby1", m4atrackPath)
fmt.Printf("Encapsulating %s into %s\n", filepath.Base(trackPath), filepath.Base(m4atrackPath))

Loading…
Cancel
Save