Skip to content

Commit

Permalink
thumbnails: extract pictures from audio files
Browse files Browse the repository at this point in the history
  • Loading branch information
dschmidt committed Oct 27, 2023
1 parent 5a3e4f5 commit 9f4d41f
Show file tree
Hide file tree
Showing 23 changed files with 3,167 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ require (
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dhowden/tag v0.0.0-20230630033851-978a0926ee25 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,8 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cu
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48 h1:fRzb/w+pyskVMQ+UbP35JkH8yB7MYb4q/qhBarqZE6g=
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA=
github.com/dhowden/tag v0.0.0-20230630033851-978a0926ee25 h1:simG0vMYFvNriGhaaat7QVVkaVkXzvqcohaBoLZl9Hg=
github.com/dhowden/tag v0.0.0-20230630033851-978a0926ee25/go.mod h1:Z3Lomva4pyMWYezjMAU5QWRh0p1VvO4199OHlFnyKkM=
github.com/dimchansky/utfbom v1.1.0/go.mod h1:rO41eb7gLfo8SF1jd9F8HplJm1Fewwi4mQvIirEdv+8=
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
Expand Down
60 changes: 60 additions & 0 deletions services/thumbnails/pkg/preprocessor/lazyReadSeeker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package preprocessor

import (
"io"

"github.com/pkg/errors"
)

type LazyReadSeeker struct {
reader io.Reader
buffer []byte
position int64
}

func NewLazyReadSeeker(r io.Reader) *LazyReadSeeker {
return &LazyReadSeeker{
reader: r,
buffer: make([]byte, 0),
}
}

func (l *LazyReadSeeker) Read(p []byte) (int, error) {
// Fill buffer if necessary
if l.position >= int64(len(l.buffer)) {
temp := make([]byte, len(p))
n, err := l.reader.Read(temp)
if err != nil && err != io.EOF {
return 0, err
}
if n == 0 {
return 0, io.EOF
}
l.buffer = append(l.buffer, temp[:n]...)
}

// Read from buffer
n := copy(p, l.buffer[l.position:])
l.position += int64(n)

return n, nil
}

func (l *LazyReadSeeker) Seek(offset int64, whence int) (int64, error) {
var newPos int64
switch whence {
case io.SeekStart:
newPos = offset
case io.SeekCurrent:
newPos = l.position + offset
default:
return 0, errors.New("seekEnd is not supported")
}

if newPos < 0 {
return 0, errors.New("negative position is invalid")
}

l.position = newPos
return l.position, nil
}
30 changes: 30 additions & 0 deletions services/thumbnails/pkg/preprocessor/preprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package preprocessor

import (
"bufio"
"bytes"
"image"
"image/draw"
"image/gif"
"io"
"log"
"math"
"mime"
"strings"
Expand All @@ -15,6 +17,8 @@ import (
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"golang.org/x/image/math/fixed"

"github.com/dhowden/tag"
)

type FileConverter interface {
Expand All @@ -41,6 +45,28 @@ func (i GifDecoder) Convert(r io.Reader) (interface{}, error) {
return img, nil
}

type AudioDecoder struct{}

func (i AudioDecoder) Convert(r io.Reader) (interface{}, error) {
readSeeker := NewLazyReadSeeker(r)
m, err := tag.ReadFrom(readSeeker)
if err != nil {
log.Fatal(err)
}

picture := m.Picture()
if picture == nil {
return nil, errors.New(`could not extract image from audio file`)
}

converter := ForType(picture.MIMEType, nil)
if converter == nil {
return nil, errors.New(`could not find converter for image extraced from audio file`)
}

return converter.Convert(bytes.NewReader(picture.Data))
}

type TxtToImageConverter struct {
fontLoader *FontLoader
}
Expand Down Expand Up @@ -197,6 +223,10 @@ func ForType(mimeType string, opts map[string]interface{}) FileConverter {
}
case "image/gif":
return GifDecoder{}
case "audio/flac":
case "audio/mpeg":
case "audio/ogg":
return AudioDecoder{}
default:
return ImageDecoder{}
}
Expand Down
3 changes: 3 additions & 0 deletions services/thumbnails/pkg/thumbnail/thumbnail.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ var (
"image/x-ms-bmp": {},
"image/tiff": {},
"text/plain": {},
"audio/flac": {},
"audio/mpeg": {},
"audio/ogg": {},
}
)

Expand Down
19 changes: 19 additions & 0 deletions vendor/github.com/dhowden/tag/.editorconfig

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/github.com/dhowden/tag/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions vendor/github.com/dhowden/tag/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions vendor/github.com/dhowden/tag/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 109 additions & 0 deletions vendor/github.com/dhowden/tag/dsf.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9f4d41f

Please sign in to comment.