basic loop, mp3 support, default image, resize all images to same size

This commit is contained in:
Octopus Octopus 2024-03-13 22:17:52 -05:00
parent d3cadbcab2
commit 9755424d5e
4 changed files with 130 additions and 41 deletions

BIN
cmus2obs

Binary file not shown.

8
go.mod
View File

@ -3,6 +3,10 @@ module cmus2obs
go 1.22.1 go 1.22.1
require ( require (
github.com/go-flac/flacpicture v0.3.0 // indirect github.com/bogem/id3v2/v2 v2.1.4
github.com/go-flac/go-flac v1.0.0 // indirect github.com/go-flac/flacpicture v0.3.0
github.com/go-flac/go-flac v1.0.0
golang.org/x/image v0.15.0
) )
require golang.org/x/text v0.14.0 // indirect

30
go.sum
View File

@ -1,4 +1,34 @@
github.com/bogem/id3v2/v2 v2.1.4 h1:CEwe+lS2p6dd9UZRlPc1zbFNIha2mb2qzT1cCEoNWoI=
github.com/bogem/id3v2/v2 v2.1.4/go.mod h1:l+gR8MZ6rc9ryPTPkX77smS5Me/36gxkMgDayZ9G1vY=
github.com/go-flac/flacpicture v0.3.0 h1:LkmTxzFLIynwfhHiZsX0s8xcr3/u33MzvV89u+zOT8I= github.com/go-flac/flacpicture v0.3.0 h1:LkmTxzFLIynwfhHiZsX0s8xcr3/u33MzvV89u+zOT8I=
github.com/go-flac/flacpicture v0.3.0/go.mod h1:DPbrzVYQ3fJcvSgLFp9HXIrEQEdfdk/+m0nQCzwodZI= github.com/go-flac/flacpicture v0.3.0/go.mod h1:DPbrzVYQ3fJcvSgLFp9HXIrEQEdfdk/+m0nQCzwodZI=
github.com/go-flac/go-flac v1.0.0 h1:6qI9XOVLcO50xpzm3nXvO31BgDgHhnr/p/rER/K/doY= github.com/go-flac/go-flac v1.0.0 h1:6qI9XOVLcO50xpzm3nXvO31BgDgHhnr/p/rER/K/doY=
github.com/go-flac/go-flac v1.0.0/go.mod h1:WnZhcpmq4u1UdZMNn9LYSoASpWOCMOoxXxcWEHSzkW8= github.com/go-flac/go-flac v1.0.0/go.mod h1:WnZhcpmq4u1UdZMNn9LYSoASpWOCMOoxXxcWEHSzkW8=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/image v0.15.0 h1:kOELfmgrmJlw4Cdb7g/QGuB3CvDrXbqEIww/pNtNBm8=
golang.org/x/image v0.15.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

133
main.go
View File

@ -1,60 +1,92 @@
package main package main
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"image"
"log" "log"
"os" "os"
"strings" "strings"
"time"
"os/exec" "os/exec"
"github.com/bogem/id3v2/v2"
"github.com/go-flac/flacpicture" "github.com/go-flac/flacpicture"
"github.com/go-flac/go-flac" "github.com/go-flac/go-flac"
"golang.org/x/image/draw"
_ "image/gif"
"image/jpeg"
_ "image/jpeg"
_ "image/png"
) )
const TIMER = 2 const (
TIMER = 2
IMAGE_SIZE = 700
)
func main() { func main() {
c := exec.Command("cmus-remote", "-Q") for {
o, _ := c.Output() c := exec.Command("cmus-remote", "-Q")
remoteResp := strings.Split(string(o), "\n") o, _ := c.Output()
//Album remoteResp := strings.Split(string(o), "\n")
album, err := getAttribute(remoteResp, "tag album ") //Album
if err != nil { album, err := getAttribute(remoteResp, "tag album ")
album = "Unknown"
}
//artist
artist, err := getAttribute(remoteResp, "tag artist ")
if err != nil {
artist = "Unknown"
}
// Title
title, err := getAttribute(remoteResp, "tag title ")
if err != nil {
title = "Unknown"
}
filepath, err := getAttribute(remoteResp, "file ")
if err != nil {
log.Fatal(err.Error())
}
img := make([]byte, 0)
if strings.HasSuffix(filepath, ".flac") {
img, err = getFlacArt(filepath)
if err != nil { if err != nil {
album = "Unknown"
}
//artist
artist, err := getAttribute(remoteResp, "tag artist ")
if err != nil {
artist = "Unknown"
}
// Title
title, err := getAttribute(remoteResp, "tag title ")
if err != nil {
title = "Unknown"
}
filepath, err := getAttribute(remoteResp, "file ")
if err != nil {
log.Fatal(err.Error())
}
img := make([]byte, 0)
if strings.HasSuffix(filepath, ".flac") {
img, err = getFlacArt(filepath)
if err != nil {
img = defaultArt()
}
} else if strings.HasSuffix(filepath, ".mp3") {
img, err = getMP3Art(filepath)
if err != nil {
img = defaultArt()
}
} else {
img = defaultArt() img = defaultArt()
} }
} else if strings.HasSuffix(filepath, ".mp3") { imgBuff := bytes.NewBuffer(img)
getMP3Art()
} else {
defaultArt()
}
writeTxt("SongAlbum", album) imgOrig, _, err := image.Decode(imgBuff)
writeTxt("SongArtist", artist) if err != nil {
writeTxt("SongTitle", title) log.Fatal(err.Error())
writeJpg("AlbumArt", img) }
imgOut := image.NewRGBA(image.Rect(0, 0, IMAGE_SIZE, IMAGE_SIZE))
draw.BiLinear.Scale(imgOut, imgOut.Rect, imgOrig, imgOrig.Bounds(), draw.Over, nil)
jpeg.Encode(imgBuff, imgOut, nil)
writeTxt("SongAlbum", album)
writeTxt("SongArtist", artist)
writeTxt("SongTitle", title)
writeJpg("AlbumArt", img)
time.Sleep(TIMER * time.Second)
}
} }
func getFlacArt(s string) ([]byte, error) { func getFlacArt(s string) ([]byte, error) {
@ -71,15 +103,37 @@ func getFlacArt(s string) ([]byte, error) {
return nil, errors.New("no image found") return nil, errors.New("no image found")
} }
func getMP3Art() { func getMP3Art(s string) ([]byte, error) {
tags, err := id3v2.Open(s, id3v2.Options{Parse: true})
if err != nil {
return nil, errors.New("can't open mp3 tags")
}
defer tags.Close()
pic := tags.GetFrames(tags.CommonID("Attached picture"))
if pic != nil {
return pic[0].(id3v2.PictureFrame).Picture, nil //i literally do not care if it has multiple pictures, pick the first one.
}
return nil, errors.New("no image found")
} }
func defaultArt() []byte { func defaultArt() []byte {
return nil file, err := os.Open("default.jpg")
if err != nil {
log.Fatal(err.Error())
}
defer file.Close()
info, err := file.Stat()
if err != nil {
log.Fatal(err.Error())
}
art := make([]byte, info.Size())
file.Read(art)
return art
} }
func writeJpg(filename string, input []byte) { func writeJpg(filename string, input []byte) {
os.Mkdir("output", 0755)
f, err := os.Create("./output/" + filename + ".jpg") f, err := os.Create("./output/" + filename + ".jpg")
if err != nil { if err != nil {
log.Fatal(err.Error()) log.Fatal(err.Error())
@ -90,6 +144,7 @@ func writeJpg(filename string, input []byte) {
} }
func writeTxt(filename, input string) { func writeTxt(filename, input string) {
os.Mkdir("output", 0755)
f, err := os.Create("./output/" + filename + ".txt") f, err := os.Create("./output/" + filename + ".txt")
if err != nil { if err != nil {
log.Fatal(err.Error()) log.Fatal(err.Error())