Skip to content

Commit

Permalink
[TASK] add cover art and load metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
dpolakovics committed Nov 6, 2024
1 parent 77ea314 commit 6ba5393
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 33 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ It is important that both folders have the same amount of Files and that the fil
- [ ] Make it usable with Mac
- [ ] Generate a package for Ubuntu
- [ ] Better handling of file mapping
- [x] add cover art
- [ ] load metadata from audiobook
- [x] add cover art for mp3
- [x] add cover art for m4b
- [x] load metadata from audiobook

## Support Me
[![Buy Me A Coffee](https://cdn.buymeacoffee.com/buttons/default-orange.png)](https://www.buymeacoffee.com/razormind)
31 changes: 17 additions & 14 deletions internal/logic/sync-atmos.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,36 @@ func combineAtmosFiles(folder1 string, folder2 string, outputFolder string, prog
}

// Get extension of file
ext := filepath.Ext(file)
// ext := filepath.Ext(file)

// Get output file name
newFileName := outputFolder + "/" + filepath.Base(files2[index])
newFileName = newFileName[:len(newFileName)-4] + "_synced" + ext
newFileName = newFileName[:len(newFileName)-4] + "_synced.m4v"

// coverArtString, err := getCoverArtString(file)
// if err != nil {
// return err
// }


// Construct FFmpeg command
ctx, _ := context.WithCancel(context.Background())
// map channel 1 and 2 of file 2 to channel 1 and 2 of file 1 but keep all other channels of file 1
// it will be a 6 channel atmos output file
cmd := exec.CommandContext(ctx, ffmpegPath,
arguments := []string{
"-i", file,
"-i", files2[index],
"-filter_complex", "[1:a]apad[a2];[0:a][a2]amerge=inputs=2,pan=5.1|c0=c0+c6|c1=c1+c7|c2=c2|c3=c3|c4=c4|c5=c5[out]",
"-map", "[out]",
"-c:a", "eac3",
// "-map", "2:0", "-metadata:s:v", "title=\"Album cover\"", "-metadata:s:v", "comment=\"Cover (front)\"",
// "-map", "-map", coverArtString + ":v", "-c:v", "copy", "-disposition:v:1", "attached_pic",
"-metadata:s:a:0", "encoder=\"Dolby Digital Plus + Dolby Atmos\"",
"-progress",
"pipe:1",
newFileName)
// best audio format so far on ios
"-filter_complex", "[1:a]apad[a2];[0:a][a2]amerge=inputs=2[out]",
"-c:a", "aac", "-b:a", "654k",
// mp4 output
// "-filter_complex", "[1:a]apad[a2];[0:a][a2]amerge=inputs=2,pan=5.1|c0=c0+c6|c1=c1+c7|c2=c2|c3=c3|c4=c4|c5=c5[out]",
// "-c:a", "eac3", "-ac", "6",
}
arguments = append(arguments, getBaseArguments()...)
arguments = append(arguments, getCoverArtArguments(file, files2[index])...)
arguments = append(arguments, newFileName)
// map channel 1 and 2 of file 2 to channel 1 and 2 of file 1 but keep all other channels of file 1
// it will be a 6 channel atmos output file
cmd := exec.CommandContext(ctx, ffmpegPath, arguments...)
cmd.SysProcAttr = getSysProcAttr()
stdout, err := cmd.StdoutPipe()
if err != nil {
Expand Down
22 changes: 11 additions & 11 deletions internal/logic/sync-stereo.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,24 @@ func combineStereoFiles(folder1 string, folder2 string, outputFolder string, pro
return err
}

coverArtString, err := getCoverArtString(file)
if err != nil {
return err
}
// coverArtString, err := getCoverArtString(file)
// if err != nil {
// return err
// }

// Construct FFmpeg command
newFileName := outputFolder + "/" + filepath.Base(files2[index])
newFileName = newFileName[:len(newFileName)-4] + "_synced.mp3"
ctx, _ := context.WithCancel(context.Background())
cmd := exec.CommandContext(ctx, ffmpegPath,
arguments := []string{
"-i", file,
"-i", files2[index],
"-filter_complex", "[1:a]apad[a2];[0:a][a2]amerge=inputs=2,pan=stereo|c0<c0+c2|c1<c1+c3[a]",
"-map", coverArtString + ":v", "-c:v", "copy", "-disposition:v", "attached_pic",
"-progress",
"pipe:1",
"-map", "[a]",
outputFolder + "/" + filepath.Base(files2[index]))
"-filter_complex", "[1:a]apad[a2];[0:a][a2]amerge=inputs=2,pan=stereo|c0<c0+c2|c1<c1+c3[out]",
}
arguments = append(arguments, getBaseArguments()...)
arguments = append(arguments, getCoverArtArguments(file, files2[index])...)
arguments = append(arguments, newFileName)
cmd := exec.CommandContext(ctx, ffmpegPath, arguments...)
cmd.SysProcAttr = getSysProcAttr()
stdout, err := cmd.StdoutPipe()
if err != nil {
Expand Down
31 changes: 25 additions & 6 deletions internal/logic/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,43 @@ func parseProgress(index int, total int, progress *widget.ProgressBar, reader io
}
}

func getCoverArtString(filePath string) (string, error) {
func getBaseArguments() []string {
return []string{
"-map", "[out]",
"-progress",
"pipe:1",
"-map_metadata", "1",
}
}

func getCoverArtArguments(file1 string, file2 string) []string {
arguments := []string{}
if testCoverArt(file1) {
arguments = append(arguments, "-map", "0:v", "-c:v", "copy", "-disposition:v", "attached_pic")
}
if testCoverArt(file2) {
arguments = append(arguments, "-map", "1:v", "-c:v", "copy", "-disposition:v", "attached_pic")
}
return arguments
}

func testCoverArt(filePath string) bool {
// Open the audio file
coverArtString := "1"
f, err := os.Open(filePath)
if err != nil {
return coverArtString, err
return false
}
defer f.Close()

// Use taglib to parse the audio file
metadata, err := tag.ReadFrom(f)
if err != nil {
return coverArtString, err
return false
}

// Check if there is any artwork
if metadata.Picture() != nil {
coverArtString = "0"
return true
}
return coverArtString, nil
return false
}

0 comments on commit 6ba5393

Please sign in to comment.