mirror of
https://github.com/zhaarey/apple-music-downloader.git
synced 2025-10-23 15:11:05 +00:00
add dl-albumcover-for-playlist
This commit is contained in:
54
main.go
54
main.go
@@ -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
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
@@ -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.")
|
||||
@@ -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.")
|
||||
}
|
||||
@@ -1374,7 +1388,15 @@ 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)
|
||||
@@ -1382,6 +1404,12 @@ func rip(albumId string, token string, storefront string, userToken string) erro
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user