Skip to content

Commit d6570bd

Browse files
committed
Adding support for embedded pictures in music file metadata
1 parent 8d01d37 commit d6570bd

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.23.5
44

55
require (
66
github.com/davidbyttow/govips/v2 v2.16.0
7+
github.com/dhowden/tag v0.0.0-20240417053706-3d75831295e8
78
github.com/gabriel-vasile/mimetype v1.4.8
89
github.com/kenshaw/colors v0.2.1
910
github.com/kenshaw/fontimg v0.1.1

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
2727
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2828
github.com/davidbyttow/govips/v2 v2.16.0 h1:1nH/Rbx8qZP1hd+oYL9fYQjAnm1+KorX9s07ZGseQmo=
2929
github.com/davidbyttow/govips/v2 v2.16.0/go.mod h1:clH5/IDVmG5eVyc23qYpyi7kmOT0B/1QNTKtci4RkyM=
30+
github.com/dhowden/tag v0.0.0-20240417053706-3d75831295e8 h1:OtSeLS5y0Uy01jaKK4mA/WVIYtpzVm63vLVAPzJXigg=
31+
github.com/dhowden/tag v0.0.0-20240417053706-3d75831295e8/go.mod h1:apkPC/CR3s48O2D7Y++n1XWEpgPNNCjXYga3PPbJe2E=
3032
github.com/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yAo=
3133
github.com/dlclark/regexp2 v1.11.4/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
3234
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=

main.go

+33-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"unicode"
3131

3232
"github.com/davidbyttow/govips/v2/vips"
33+
"github.com/dhowden/tag"
3334
"github.com/gabriel-vasile/mimetype"
3435
"github.com/kenshaw/colors"
3536
"github.com/kenshaw/fontimg"
@@ -115,14 +116,15 @@ func run(w io.Writer, args *Args) func(context.Context, []string) error {
115116
for i := 0; i < len(cliargs); i++ {
116117
v, err := open(cliargs[i])
117118
if err != nil {
118-
fmt.Fprintf(w, "error: unable to open arg %d: %v\n", i, err)
119+
fmt.Fprintf(w, "error: unable to open arg %d: %v\n\n", i, err)
120+
} else {
121+
files = append(files, v...)
119122
}
120-
files = append(files, v...)
121123
}
122124
// render
123125
for i := 0; i < len(files); i++ {
124126
if err := args.render(w, files[i]); err != nil {
125-
fmt.Fprintf(w, "error: unable to render arg %d: %v\n", i, err)
127+
fmt.Fprintf(w, "error: unable to render arg %d: %v\n\n", i, err)
126128
}
127129
}
128130
return nil
@@ -202,6 +204,8 @@ func (args *Args) renderFile(name string) (image.Image, string, error) {
202204
g = args.renderFont
203205
case strings.HasPrefix(typ, "video/"):
204206
g, notStream = args.renderFfmpeg, true
207+
case strings.HasPrefix(typ, "audio/"):
208+
g = args.renderTag
205209
default:
206210
return nil, "", fmt.Errorf("mime type %q not supported", typ)
207211
}
@@ -493,6 +497,24 @@ func (args *Args) vipsExport(v *vips.ImageRef) (image.Image, error) {
493497
return img, nil
494498
}
495499

500+
// renderTag renders the embedded picture from music metadata (ie, album art).
501+
func (args *Args) renderTag(r io.Reader, _ string) (image.Image, error) {
502+
f, ok := r.(*os.File)
503+
if !ok {
504+
return nil, fmt.Errorf("must take *os.File, got: %T", r)
505+
}
506+
md, err := tag.ReadFrom(f)
507+
if err != nil {
508+
return nil, err
509+
}
510+
pic := md.Picture()
511+
if pic == nil {
512+
return nil, errors.New("no embedded picture")
513+
}
514+
img, _, err := image.Decode(bytes.NewReader(pic.Data))
515+
return img, err
516+
}
517+
496518
// addBackground adds a background to a image.
497519
func (args *Args) addBackground(fg image.Image, typ string) image.Image {
498520
if args.bgc == nil || typ == "image/svg" {
@@ -763,6 +785,7 @@ var (
763785
var extensions = map[string]bool{
764786
"3g2": true,
765787
"3gp": true,
788+
"aac": true,
766789
"asf": true,
767790
"avif": true,
768791
"avi": true,
@@ -774,6 +797,7 @@ var extensions = map[string]bool{
774797
"dvb": true,
775798
"dwg": true,
776799
"eot": true,
800+
"flac": true,
777801
"flv": true,
778802
"gif": true,
779803
"heic": true,
@@ -785,13 +809,16 @@ var extensions = map[string]bool{
785809
"jpg": true,
786810
"jxl": true,
787811
"jxs": true,
812+
"m4a": true,
788813
"m4v": true,
789814
"markdown": true,
790815
"md": true,
791816
"mj2": true,
792817
"mkv": true,
793818
"mov": true,
819+
"mp3": true,
794820
"mp4": true,
821+
"mpeg3": true,
795822
"mpeg": true,
796823
"mpg": true,
797824
"odc": true,
@@ -800,6 +827,9 @@ var extensions = map[string]bool{
800827
"odp": true,
801828
"ods": true,
802829
"odt": true,
830+
"oga": true,
831+
"ogg": true,
832+
"ogv": true,
803833
"otf": true,
804834
"otg": true,
805835
"otp": true,

0 commit comments

Comments
 (0)