grab track info from cmus, begin work for parsing images

This commit is contained in:
Octopus Octopus 2025-02-23 16:11:54 -06:00
parent b5adb5a747
commit b8714becca
6 changed files with 271 additions and 4 deletions

36
cmus.go Normal file
View File

@ -0,0 +1,36 @@
package main
import (
"strings"
"os/exec"
"fmt"
)
func CmusRemoteOutput() ([]string, error) {
c := exec.Command("cmus-remote", "-Q")
o, err := c.Output()
if err != nil {
return []string{}, err
}
resp := strings.Split(string(o), "\n")
return resp, err
}
func getAttribute(input []string, prefix string) (string, error) {
var attr string
for i := range input {
has := strings.HasPrefix(input[i], prefix)
if has {
attr = input[i]
}
}
attr, b := strings.CutPrefix(attr, prefix)
if !b {
return "", fmt.Errorf("did not find prefix \"%v\"", prefix)
}
return attr, nil
}

Binary file not shown.

9
go.mod
View File

@ -2,7 +2,13 @@ module git.jade.moe/electricboogaloo
go 1.24.0 go 1.24.0
require github.com/andreykaipov/goobs v1.5.3 require (
github.com/andreykaipov/goobs v1.5.3
github.com/bogem/id3v2/v2 v2.1.4
github.com/go-flac/flacpicture v0.3.0
github.com/go-flac/go-flac v1.0.0
golang.org/x/image v0.24.0
)
require ( require (
github.com/buger/jsonparser v1.1.1 // indirect github.com/buger/jsonparser v1.1.1 // indirect
@ -11,4 +17,5 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mmcloughlin/profile v0.1.1 // indirect github.com/mmcloughlin/profile v0.1.1 // indirect
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
golang.org/x/text v0.22.0 // indirect
) )

34
go.sum
View File

@ -1,9 +1,15 @@
github.com/andreykaipov/goobs v1.5.3 h1:2kp99IUm0VA5cB67WrMp3nfDAM8lo3fTBoA1EfDdZ64= github.com/andreykaipov/goobs v1.5.3 h1:2kp99IUm0VA5cB67WrMp3nfDAM8lo3fTBoA1EfDdZ64=
github.com/andreykaipov/goobs v1.5.3/go.mod h1:yE6JutVAl1vLcABwfX7OcPcShTG9eABUUfoUj/y5Xc0= github.com/andreykaipov/goobs v1.5.3/go.mod h1:yE6JutVAl1vLcABwfX7OcPcShTG9eABUUfoUj/y5Xc0=
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/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/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/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y= github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
@ -18,5 +24,33 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
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.24.0 h1:AN7zRgVsbvmTfNyqIbbOraYL8mSwcKncEj8ofjgzcMQ=
golang.org/x/image v0.24.0/go.mod h1:4b/ITuLfqYq1hqZcjofwctIhi7sZh2WaCjvsBNjjya8=
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.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
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=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

34
main.go
View File

@ -22,19 +22,47 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
out, err := CmusRemoteOutput()
if err != nil {
panic(err)
}
artist, err := getAttribute(out, "tag artist ")
if err != nil {
panic(err)
}
title, err := getAttribute(out, "tag title ")
if err != nil {
panic(err)
}
album, err := getAttribute(out, "tag album ")
if err != nil {
panic(err)
}
for i := range scil.SceneItems { for i := range scil.SceneItems {
if scil.SceneItems[i].SourceName == "Artist" { if scil.SceneItems[i].SourceName == "Artist" {
updateTextItem(client, scil.SceneItems[i]) updateTextItem(client, scil.SceneItems[i], artist)
}
if scil.SceneItems[i].SourceName == "Song" {
updateTextItem(client, scil.SceneItems[i], title)
}
if scil.SceneItems[i].SourceName == "Album" {
updateTextItem(client, scil.SceneItems[i], album)
} }
} }
} }
func updateTextItem(client *goobs.Client, sI *typedefs.SceneItem) {
func updateTextItem(client *goobs.Client, sI *typedefs.SceneItem, text string) {
params := inputs.NewSetInputSettingsParams().WithInputName(sI.SourceName).WithInputUuid(sI.SourceUuid) params := inputs.NewSetInputSettingsParams().WithInputName(sI.SourceName).WithInputUuid(sI.SourceUuid)
params.InputSettings = make(map[string]any) params.InputSettings = make(map[string]any)
params.InputSettings["text"] = "pens" params.InputSettings["text"] = text
_, err := client.Inputs.SetInputSettings(params) _, err := client.Inputs.SetInputSettings(params)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

162
parse.go Normal file
View File

@ -0,0 +1,162 @@
package main
import (
"strings"
"github.com/bogem/id3v2/v2"
"github.com/go-flac/flacpicture"
"github.com/go-flac/go-flac"
"fmt"
"os"
"io"
"log"
"path/filepath"
)
// has
func hasFlacTrackArt(s string) bool {
// dumb check
if !strings.HasSuffix(s, ".flac") {
return false
}
// is parsable
f, err := flac.ParseFile(s)
if err != nil {
return false
}
// has any frames
if len(f.Meta) == 0 {
return false
}
// has any pictures
for _, metadata := range f.Meta {
if metadata.Type == flac.Picture {
return true
}
}
// no pictures
return false
}
func hasMp3TrackArt(s string) bool {
// dumb check
if !strings.HasSuffix(s, ".mp3") {
return false
}
// is parsable
m, err := id3v2.Open(s, id3v2.Options{Parse: true})
if err != nil {
return false
}
defer m.Close()
// has a picture
pic := m.GetFrames(m.CommonID("Attached picture"))
if pic != nil {
return true
}
// no pictures
return false
}
func hasCoverJpg(s string) bool {
exts := []string{".jpg", ".jpeg", ".png", ".gif"} // 'hasCoverJpg' is a misnomer, check all sane image types
dir := filepath.Dir(s)
file, err := os.Open(dir)
if err != nil {
log.Printf("can't open %v; %v\n", dir, err.Error())
return false
}
defer file.Close()
indexes, _ := file.ReadDir(0)
for i := range indexes {
for _, key := range exts {
if "cover"+key == indexes[i].Name() {
return true
}
}
}
return false
}
// get
func getFlacArt(s string) ([]byte, error) {
f, err := flac.ParseFile(s)
if err != nil {
return nil, fmt.Errorf("can't open file")
}
for _, metadata := range f.Meta {
if metadata.Type == flac.Picture {
// do not care if it has multiple pictures, pick the first one.
pic, err := flacpicture.ParseFromMetaDataBlock(*metadata)
return pic.ImageData, err
}
}
return nil, fmt.Errorf("no image found")
}
func getMP3Art(s string) ([]byte, error) {
tags, err := id3v2.Open(s, id3v2.Options{Parse: true})
if err != nil {
return nil, fmt.Errorf("can't open mp3 tags")
}
defer tags.Close()
pic := tags.GetFrames(tags.CommonID("Attached picture"))
if pic != nil {
// do not care if it has multiple pictures, pick the first one.
return pic[0].(id3v2.PictureFrame).Picture, nil
}
return nil, fmt.Errorf("no image found")
}
func getCoverJpg(s string) ([]byte, error) {
exts := []string{".jpg", ".jpeg", ".png", ".gif"}
dir := filepath.Dir(s)
file, err := os.Open(dir)
if err != nil {
return nil, fmt.Errorf("can't open %v; %v", dir, err.Error())
}
defer file.Close()
indexes, _ := file.ReadDir(0)
for i := range indexes {
for _, key := range exts {
if "cover"+key == indexes[i].Name() {
cover, err := os.Open(dir + "/" + indexes[i].Name())
if err != nil {
return nil, fmt.Errorf("can't open %v", indexes[i].Name())
}
defer cover.Close()
if err != nil {
return nil, fmt.Errorf("cant stat %v; %v", indexes[i].Name(), err.Error())
}
return io.ReadAll(cover)
}
}
}
return nil, fmt.Errorf("cover not found")
}
func getDefaultArt() ([]byte, error) {
file, err := os.Open("default.jpg")
if err != nil {
return []byte{}, err
}
defer file.Close()
info, err := file.Stat()
if err != nil {
return []byte{}, err
}
art := make([]byte, info.Size())
file.Read(art)
return art, err
}