Rewriting image Rendering
Now individual parsing of mp3, flac files is done. and then they are rendered
This commit is contained in:
parent
5e4e6fabbe
commit
cad01293c8
@ -3,61 +3,102 @@ package main
|
|||||||
import (
|
import (
|
||||||
"image"
|
"image"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/dhowden/tag"
|
"github.com/bogem/id3v2"
|
||||||
|
"github.com/mewkiz/flac"
|
||||||
|
"github.com/mewkiz/flac/meta"
|
||||||
"github.com/nfnt/resize"
|
"github.com/nfnt/resize"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
func GetMp3Image(songPath, imagePath string) string {
|
||||||
Gets the Image Path from the uri to the string passed
|
tag, err := id3v2.Open(songPath, id3v2.Options{Parse: true})
|
||||||
if embedded image is found the path to that Image is returned else
|
|
||||||
path to default image is sent.
|
|
||||||
*/
|
|
||||||
func getAlbumArt(uri string) string {
|
|
||||||
var path string = viper.GetString("DEFAULT_IMAGE_PATH")
|
|
||||||
f, err := os.Open(uri)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return ""
|
||||||
}
|
}
|
||||||
m, err := tag.ReadFrom(f)
|
defer tag.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return ""
|
||||||
}
|
}
|
||||||
albumCover := m.Picture()
|
// Read tags.
|
||||||
if albumCover != nil {
|
Frames := tag.GetFrames(tag.CommonID("Attached picture"))
|
||||||
b, err := os.Create(viper.GetString("COVER_IMAGE_PATH"))
|
var ImageData []byte
|
||||||
if err != nil {
|
for _, er := range Frames {
|
||||||
panic(err)
|
pic, ok := er.(id3v2.PictureFrame)
|
||||||
|
if ok {
|
||||||
|
for _, i := range pic.Picture {
|
||||||
|
ImageData = append(ImageData, byte(i))
|
||||||
|
}
|
||||||
|
imageHandler, err := os.Create(imagePath)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
} else {
|
||||||
|
imageHandler.Write(ImageData)
|
||||||
|
return imagePath
|
||||||
|
}
|
||||||
}
|
}
|
||||||
defer b.Close()
|
|
||||||
b.Write(albumCover.Data)
|
|
||||||
path = viper.GetString("COVER_IMAGE_PATH")
|
|
||||||
b.Close()
|
|
||||||
}
|
}
|
||||||
f.Close()
|
return ""
|
||||||
return path
|
}
|
||||||
|
|
||||||
|
func GetFlacImage(songPath, imagePath string) string {
|
||||||
|
stream, err := flac.ParseFile(songPath)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
defer stream.Close()
|
||||||
|
for _, block := range stream.Blocks {
|
||||||
|
if block.Type == meta.TypePicture {
|
||||||
|
pic := block.Body.(*meta.Picture)
|
||||||
|
if pic.Type == 3 {
|
||||||
|
imageHandler, err := os.Create(imagePath)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
imageHandler.Write(pic.Data)
|
||||||
|
return imagePath
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func extractImageFromFile(uri string) string {
|
||||||
|
if strings.HasSuffix(uri, ".mp3") {
|
||||||
|
imagePath := GetMp3Image(uri, viper.GetString("COVER_IMAGE_PATH"))
|
||||||
|
if imagePath == "" {
|
||||||
|
return viper.GetString("DEFAULT_IMAGE_PATH")
|
||||||
|
} else {
|
||||||
|
return imagePath
|
||||||
|
}
|
||||||
|
} else if strings.HasSuffix(uri, ".flac") {
|
||||||
|
imagePath := GetFlacImage(uri, viper.GetString("COVER_IMAGE_PATH"))
|
||||||
|
if imagePath == "" {
|
||||||
|
return viper.GetString("DEFAULT_IMAGE_PATH")
|
||||||
|
} else {
|
||||||
|
return imagePath
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return viper.GetString("DEFAULT_IMAGE_PATH")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getImg(uri string) (image.Image, error) {
|
func getImg(uri string) (image.Image, error) {
|
||||||
|
|
||||||
f, err := os.Open(uri)
|
f, err := os.Open(uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
img, _, err := image.Decode(f)
|
img, _, err := image.Decode(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
fw, fh := getFontWidth()
|
fw, fh := getFontWidth()
|
||||||
img = resize.Resize(
|
img = resize.Resize(
|
||||||
uint(float32(IMG_W)*(fw+float32(viper.GetFloat64("IMAGE_WIDTH_EXTRA_X")))), uint(float32(IMG_H)*(fh+float32(viper.GetFloat64("IMAGE_WIDTH_EXTRA_Y")))),
|
uint(float32(IMG_W)*(fw+float32(viper.GetFloat64("IMAGE_WIDTH_EXTRA_X")))), uint(float32(IMG_H)*(fh+float32(viper.GetFloat64("IMAGE_WIDTH_EXTRA_Y")))),
|
||||||
img,
|
img,
|
||||||
resize.Bilinear,
|
resize.Bilinear,
|
||||||
)
|
)
|
||||||
|
|
||||||
return img, nil
|
return img, nil
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ func openImage(path string, c chan string) {
|
|||||||
fw, fh := getFontWidth()
|
fw, fh := getFontWidth()
|
||||||
var im *ueberzug.Image
|
var im *ueberzug.Image
|
||||||
if path != "stop" {
|
if path != "stop" {
|
||||||
img2, _ := getImg(getAlbumArt(path))
|
img2, _ := getImg(extractImageFromFile(path))
|
||||||
im, _ = ueberzug.NewImage(img2, int(float32(IMG_X)*fw)+viper.GetInt("ADDITIONAL_PADDING_X"), int(float32(IMG_Y)*fh)+viper.GetInt("ADDITIONAL_PADDING_Y"))
|
im, _ = ueberzug.NewImage(img2, int(float32(IMG_X)*fw)+viper.GetInt("ADDITIONAL_PADDING_X"), int(float32(IMG_Y)*fh)+viper.GetInt("ADDITIONAL_PADDING_Y"))
|
||||||
}
|
}
|
||||||
d := <-c
|
d := <-c
|
||||||
|
Loading…
Reference in New Issue
Block a user