Skip to content

Commit

Permalink
add dl-albumcover-for-playlist
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaarey committed Jun 6, 2024
1 parent 1f20fa5 commit 0979ae0
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 68 deletions.
4 changes: 4 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ artist-folder-format: "{ArtistName}"
explicit-choice : "[E]"
clean-choice : "[C]"
apple-master-choice : "[M]"
#if set true,for playlst,will use songinfo for meta #albumname track disk
use-songinfo-for-playlist: false
#if set true,will download album cover for playlist
dl-albumcover-for-playlist: false
54 changes: 41 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type Config struct {
Check string `yaml:"check"`
GetM3u8FromDevice bool `yaml:"get-m3u8-from-device"`
AlacMax int `yaml:"alac-max"`
UseSongInfoForPlaylist bool `yaml:"use-songinfo-for-playlist"`
DlAlbumcoverForPlaylist bool `yaml:"dl-albumcover-for-playlist"`
}

var config Config
Expand Down Expand Up @@ -688,13 +690,18 @@ func writeM4a(w *mp4.Writer, info *SongInfo, meta *AutoGenerated, data []byte, t
if err != nil {
return err
}

err = addMeta(mp4.BoxType{'\251', 'a', 'l', 'b'}, meta.Data[0].Relationships.Tracks.Data[index].Attributes.AlbumName)
AlbumName:=meta.Data[0].Relationships.Tracks.Data[index].Attributes.AlbumName
if strings.Contains(meta.Data[0].ID, "pl."){
if !config.UseSongInfoForPlaylist {
AlbumName=meta.Data[0].Attributes.Name
}
}
err = addMeta(mp4.BoxType{'\251', 'a', 'l', 'b'}, AlbumName)
if err != nil {
return err
}

err = addMeta(mp4.BoxType{'s', 'o', 'a', 'l'}, meta.Data[0].Relationships.Tracks.Data[index].Attributes.AlbumName)
err = addMeta(mp4.BoxType{'s', 'o', 'a', 'l'}, AlbumName)
if err != nil {
return err
}
Expand Down Expand Up @@ -759,12 +766,12 @@ func writeM4a(w *mp4.Writer, info *SongInfo, meta *AutoGenerated, data []byte, t
if len(meta.Data) > 0 {
album := meta.Data[0]

err = addMeta(mp4.BoxType{'a', 'A', 'R', 'T'}, album.Attributes.ArtistName)
err = addMeta(mp4.BoxType{'a', 'A', 'R', 'T'}, meta.Data[0].Relationships.Tracks.Data[index].Attributes.ArtistName)
if err != nil {
return err
}

err = addMeta(mp4.BoxType{'s', 'o', 'a', 'a'}, album.Attributes.ArtistName)
err = addMeta(mp4.BoxType{'s', 'o', 'a', 'a'}, meta.Data[0].Relationships.Tracks.Data[index].Attributes.ArtistName)
if err != nil {
return err
}
Expand Down Expand Up @@ -822,17 +829,24 @@ func writeM4a(w *mp4.Writer, info *SongInfo, meta *AutoGenerated, data []byte, t
return err
}
}

trkn := make([]byte, 8)
disk := make([]byte, 8)
binary.BigEndian.PutUint32(trkn, uint32(meta.Data[0].Relationships.Tracks.Data[index].Attributes.TrackNumber))
binary.BigEndian.PutUint16(trkn[4:], uint16(trackTotal))
binary.BigEndian.PutUint32(disk, uint32(meta.Data[0].Relationships.Tracks.Data[index].Attributes.DiscNumber))
binary.BigEndian.PutUint16(disk[4:], uint16(meta.Data[0].Relationships.Tracks.Data[trackTotal - 1].Attributes.DiscNumber))
if strings.Contains(meta.Data[0].ID, "pl."){
if !config.UseSongInfoForPlaylist {
binary.BigEndian.PutUint32(trkn, uint32(trackNum))
binary.BigEndian.PutUint16(trkn[4:], uint16(trackTotal))
binary.BigEndian.PutUint32(disk, uint32(1))
binary.BigEndian.PutUint16(disk[4:], uint16(1))
}
}
err = addMeta(mp4.BoxType{'t', 'r', 'k', 'n'}, trkn)
if err != nil {
return err
}
disk := make([]byte, 8)
binary.BigEndian.PutUint32(disk, uint32(meta.Data[0].Relationships.Tracks.Data[index].Attributes.DiscNumber))
binary.BigEndian.PutUint16(disk[4:], uint16(meta.Data[0].Relationships.Tracks.Data[trackTotal - 1].Attributes.DiscNumber))
err = addMeta(mp4.BoxType{'d', 'i', 's', 'k'}, disk)
if err != nil {
return err
Expand Down Expand Up @@ -1101,8 +1115,8 @@ func getSongLyrics(songId string, storefront string, token string, userToken str
}
}

func writeCover(sanAlbumFolder, url string) error {
covPath := filepath.Join(sanAlbumFolder, "cover." + config.CoverFormat)
func writeCover(sanAlbumFolder,name string, url string) error {
covPath := filepath.Join(sanAlbumFolder, name+"." + config.CoverFormat)
exists, err := fileExists(covPath)
if err != nil {
fmt.Println("Failed to check if cover exists.")
Expand Down Expand Up @@ -1248,7 +1262,7 @@ func rip(albumId string, token string, storefront string, userToken string) erro
sanAlbumFolder := filepath.Join(singerFolder, forbiddenNames.ReplaceAllString(albumFolder, "_"))
os.MkdirAll(sanAlbumFolder, os.ModePerm)
fmt.Println(albumFolder)
err = writeCover(sanAlbumFolder, meta.Data[0].Attributes.Artwork.URL)
err = writeCover(sanAlbumFolder,"cover", meta.Data[0].Attributes.Artwork.URL)
if err != nil {
fmt.Println("Failed to write cover.")
}
Expand Down Expand Up @@ -1374,14 +1388,28 @@ func rip(albumId string, token string, storefront string, userToken string) erro
fmt.Sprintf("lyrics=%s", lrc),
}
if config.EmbedCover {
tags = append(tags, fmt.Sprintf("cover=%s/cover.%s", sanAlbumFolder, config.CoverFormat))
if strings.Contains(albumId, "pl.") && config.DlAlbumcoverForPlaylist {
err = writeCover(sanAlbumFolder,track.ID, track.Attributes.Artwork.URL)
if err != nil {
fmt.Println("Failed to write cover.")
}
tags = append(tags, fmt.Sprintf("cover=%s/%s.%s", sanAlbumFolder,track.ID, config.CoverFormat))
}else{
tags = append(tags, fmt.Sprintf("cover=%s/%s.%s", sanAlbumFolder,"cover", config.CoverFormat))
}
}
tagsString := strings.Join(tags, ":")
cmd := exec.Command("MP4Box", "-itags", tagsString, trackPath)
if err := cmd.Run(); err != nil {
fmt.Printf("Embed failed: %v\n", err)
continue
}
if strings.Contains(albumId, "pl.") && config.DlAlbumcoverForPlaylist {
if err := os.Remove(fmt.Sprintf("%s/%s.%s", sanAlbumFolder,track.ID, config.CoverFormat)); err != nil {
fmt.Printf("Error deleting file: %s/%s.%s\n", sanAlbumFolder,track.ID, config.CoverFormat)
continue
}
}
oktrackNum += 1
}
return err
Expand Down
39 changes: 31 additions & 8 deletions main_atmos.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ type Config struct {
CleanChoice string `yaml:"clean-choice"`
AppleMasterChoice string `yaml:"apple-master-choice"`
AtmosMax int `yaml:"atmos-max"`
UseSongInfoForPlaylist bool `yaml:"use-songinfo-for-playlist"`
DlAlbumcoverForPlaylist bool `yaml:"dl-albumcover-for-playlist"`
}

var config Config
Expand Down Expand Up @@ -1061,8 +1063,8 @@ func getSongLyrics(songId string, storefront string, token string, userToken str
}
}

func writeCover(sanAlbumFolder, url string) error {
covPath := filepath.Join(sanAlbumFolder, "cover." + config.CoverFormat)
func writeCover(sanAlbumFolder,name string, url string) error {
covPath := filepath.Join(sanAlbumFolder, name+"." + config.CoverFormat)
exists, err := fileExists(covPath)
if err != nil {
fmt.Println("Failed to check if cover exists.")
Expand Down Expand Up @@ -1191,7 +1193,7 @@ func rip(albumId string, token string, storefront string, userToken string) erro
sanAlbumFolder := filepath.Join(singerFolder, forbiddenNames.ReplaceAllString(albumFolder, "_"))
os.MkdirAll(sanAlbumFolder, os.ModePerm)
fmt.Println(albumFolder)
err = writeCover(sanAlbumFolder, meta.Data[0].Attributes.Artwork.URL)
err = writeCover(sanAlbumFolder,"cover", meta.Data[0].Attributes.Artwork.URL)
if err != nil {
fmt.Println("Failed to write cover.")
}
Expand Down Expand Up @@ -1310,7 +1312,6 @@ func rip(albumId string, token string, storefront string, userToken string) erro
tags := []string{
"tool=",
fmt.Sprintf("lyrics=%s", lrc),
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),
fmt.Sprintf("genre=%s", meta.Data[0].Relationships.Tracks.Data[index].Attributes.GenreNames[0]),
Expand All @@ -1322,12 +1323,28 @@ func rip(albumId string, token string, storefront string, userToken string) erro
fmt.Sprintf("copyright=%s", meta.Data[0].Attributes.Copyright),
fmt.Sprintf("ISRC=%s", meta.Data[0].Relationships.Tracks.Data[index].Attributes.Isrc),
fmt.Sprintf("UPC=%s", meta.Data[0].Attributes.Upc),
fmt.Sprintf("track=%s", trackNum),
fmt.Sprintf("tracknum=%d/%d", trackNum, trackTotal),
fmt.Sprintf("disk=%d/%d", meta.Data[0].Relationships.Tracks.Data[index].Attributes.DiscNumber, meta.Data[0].Relationships.Tracks.Data[trackTotal - 1].Attributes.DiscNumber),
}
if config.EmbedCover {
tags = append(tags, fmt.Sprintf("cover=%s/cover.%s", sanAlbumFolder, config.CoverFormat))
if strings.Contains(albumId, "pl.") && config.DlAlbumcoverForPlaylist {
err = writeCover(sanAlbumFolder,track.ID, track.Attributes.Artwork.URL)
if err != nil {
fmt.Println("Failed to write cover.")
}
tags = append(tags, fmt.Sprintf("cover=%s/%s.%s", sanAlbumFolder,track.ID, config.CoverFormat))
}else{
tags = append(tags, fmt.Sprintf("cover=%s/%s.%s", sanAlbumFolder,"cover", config.CoverFormat))
}
}
if strings.Contains(albumId, "pl.") && !config.UseSongInfoForPlaylist {
tags = append(tags, "disk=1/1")
tags = append(tags, fmt.Sprintf("track=%s", trackNum))
tags = append(tags, fmt.Sprintf("tracknum=%d/%d", trackNum, trackTotal))
tags = append(tags, fmt.Sprintf("album=%s", meta.Data[0].Attributes.Name))
}else{
tags = append(tags, fmt.Sprintf("disk=%d/%d", meta.Data[0].Relationships.Tracks.Data[index].Attributes.DiscNumber, meta.Data[0].Relationships.Tracks.Data[trackTotal - 1].Attributes.DiscNumber))
tags = append(tags, fmt.Sprintf("track=%s", meta.Data[0].Relationships.Tracks.Data[index].Attributes.TrackNumber))
tags = append(tags, fmt.Sprintf("tracknum=%d/%d", meta.Data[0].Relationships.Tracks.Data[index].Attributes.TrackNumber, trackTotal))
tags = append(tags, fmt.Sprintf("album=%s", meta.Data[0].Relationships.Tracks.Data[index].Attributes.AlbumName))
}
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)
Expand All @@ -1341,6 +1358,12 @@ func rip(albumId string, token string, storefront string, userToken string) erro
fmt.Printf("Error deleting file: %v\n", err)
continue
}
if strings.Contains(albumId, "pl.") && config.DlAlbumcoverForPlaylist {
if err := os.Remove(fmt.Sprintf("%s/%s.%s", sanAlbumFolder,track.ID, config.CoverFormat)); err != nil {
fmt.Printf("Error deleting file: %s/%s.%s\n", sanAlbumFolder,track.ID, config.CoverFormat)
continue
}
}
fmt.Printf("Successfully processed and deleted %s\n", filepath.Base(trackPath))
oktrackNum += 1

Expand Down
Loading

0 comments on commit 0979ae0

Please sign in to comment.