1
1
mirror of https://github.com/wader/fq.git synced 2024-12-26 15:02:28 +03:00
fq/format/vorbis/vorbis_comment.go

57 lines
1.7 KiB
Go
Raw Normal View History

2020-06-08 03:29:51 +03:00
package vorbis
import (
"encoding/base64"
"errors"
"io"
2020-06-08 03:29:51 +03:00
"strings"
"github.com/wader/fq/format"
"github.com/wader/fq/format/registry"
"github.com/wader/fq/pkg/decode"
2020-06-08 03:29:51 +03:00
)
var flacPicture decode.Group
2020-06-08 03:29:51 +03:00
func init() {
registry.MustRegister(decode.Format{
2020-06-08 03:29:51 +03:00
Name: format.VORBIS_COMMENT,
Description: "Vorbis comment",
DecodeFn: commentDecode,
Dependencies: []decode.Dependency{
{Names: []string{format.FLAC_PICTURE}, Group: &flacPicture},
2020-06-08 03:29:51 +03:00
},
})
}
func commentDecode(d *decode.D, in interface{}) interface{} {
vendorLen := d.FieldU32LE("vendor_length")
d.FieldUTF8("vendor", int(vendorLen))
userCommentListLength := d.FieldU32LE("user_comment_list_length")
i := uint64(0)
d.FieldStructArrayLoop("user_comments", "user_comment", func() bool { return i < userCommentListLength }, func(d *decode.D) {
2020-06-08 03:29:51 +03:00
userCommentLength := d.FieldU32LE("length")
userCommentStart := d.Pos()
2021-10-06 13:01:07 +03:00
userComment := d.FieldUTF8("comment", int(userCommentLength))
var metadataBlockPicturePreix = "METADATA_BLOCK_PICTURE="
var metadataBlockPicturePrefixLower = "metadata_block_picture="
if strings.HasPrefix(userComment, metadataBlockPicturePreix) ||
strings.HasPrefix(userComment, metadataBlockPicturePrefixLower) {
base64Offset := int64(len(metadataBlockPicturePreix)) * 8
base64Len := int64(len(userComment))*8 - base64Offset
rFn := func(r io.Reader) io.Reader { return base64.NewDecoder(base64.StdEncoding, r) }
_, uncompressedBB, dv, _, err := d.TryFieldReaderRangeFormat("picture", userCommentStart+base64Offset, base64Len, rFn, flacPicture, nil)
if dv == nil && errors.As(err, &decode.FormatsError{}) {
d.FieldRootBitBuf("picture", uncompressedBB)
2020-06-08 03:29:51 +03:00
}
}
i++
})
return nil
}