2023-10-01 03:34:16 +04:00
|
|
|
package oggtag
|
|
|
|
|
|
|
|
/* oggtag is a naive implementation of OGG tag's reader that is just looking
|
|
|
|
for certain tag names ending with an = character, e.g. artist= and title=.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-10-02 01:08:24 +04:00
|
|
|
"io"
|
|
|
|
"os"
|
2023-10-01 06:36:47 +04:00
|
|
|
"strings"
|
2023-10-02 01:08:24 +04:00
|
|
|
"time"
|
2023-10-01 03:34:16 +04:00
|
|
|
)
|
|
|
|
|
2023-10-05 17:26:13 +04:00
|
|
|
const (
|
|
|
|
bufferLength = 6144
|
|
|
|
OggS = "OggS"
|
|
|
|
OggSLen = len(OggS)
|
|
|
|
Vorbis = "vorbis"
|
|
|
|
VorbisLen = len(Vorbis)
|
|
|
|
)
|
|
|
|
|
|
|
|
// OggFile holds a head of a file and a tail part conatining last granule.
|
|
|
|
type OggFile struct {
|
2023-10-09 00:15:21 +04:00
|
|
|
bufHead, bufLast []byte
|
2023-10-05 17:26:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOggFile reads a file and returns a new OggFile.
|
|
|
|
func NewOggFile(path string) (*OggFile, error) {
|
2023-10-02 01:08:24 +04:00
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
2023-10-05 17:26:13 +04:00
|
|
|
return nil, err
|
2023-10-02 01:08:24 +04:00
|
|
|
}
|
2023-10-05 17:26:13 +04:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
of := &OggFile{}
|
2023-10-02 01:08:24 +04:00
|
|
|
|
2023-10-05 17:26:13 +04:00
|
|
|
of.bufHead = make([]byte, bufferLength)
|
2023-10-02 01:08:24 +04:00
|
|
|
|
2023-10-05 17:26:13 +04:00
|
|
|
if _, err := f.Read(of.bufHead); err != nil {
|
|
|
|
return nil, err
|
2023-10-02 01:08:24 +04:00
|
|
|
}
|
|
|
|
|
2023-10-09 00:15:21 +04:00
|
|
|
of.bufLast = make([]byte, bufferLength)
|
2023-10-02 01:08:24 +04:00
|
|
|
|
|
|
|
if _, err := f.Seek(-bufferLength, io.SeekEnd); err != nil {
|
2023-10-05 17:26:13 +04:00
|
|
|
return nil, err
|
2023-10-02 01:08:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
fst, err := f.Stat()
|
|
|
|
if err != nil {
|
2023-10-05 17:26:13 +04:00
|
|
|
return nil, err
|
2023-10-02 01:08:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for offset := int64(bufferLength); offset <= fst.Size(); offset += bufferLength {
|
|
|
|
if _, err := f.Seek(-offset, io.SeekEnd); err != nil {
|
2023-10-05 17:26:13 +04:00
|
|
|
return nil, err
|
2023-10-02 01:08:24 +04:00
|
|
|
}
|
|
|
|
|
2023-10-09 00:15:21 +04:00
|
|
|
if _, err := f.Read(of.bufLast); err != nil {
|
2023-10-05 17:26:13 +04:00
|
|
|
return nil, err
|
2023-10-02 01:08:24 +04:00
|
|
|
}
|
|
|
|
|
2023-10-09 00:15:21 +04:00
|
|
|
if bytes.Contains(of.bufLast, []byte(OggS)) {
|
2023-10-02 01:08:24 +04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-05 17:26:13 +04:00
|
|
|
return of, nil
|
2023-10-02 01:08:24 +04:00
|
|
|
}
|
|
|
|
|
2023-10-05 17:26:13 +04:00
|
|
|
// GetTag is searching for a certain tag and returns its value or an empty string.
|
|
|
|
func (of *OggFile) GetTag(tag string) string {
|
|
|
|
tagIdx := bytes.Index(of.bufHead, append([]byte{0, 0, 0}, (tag+"=")...))
|
2023-10-01 06:36:47 +04:00
|
|
|
if tagIdx == -1 {
|
2023-10-05 17:26:13 +04:00
|
|
|
if tagIdx = bytes.Index(of.bufHead, append([]byte{0, 0, 0}, (strings.ToUpper(tag)+"=")...)); tagIdx == -1 {
|
2023-10-08 01:23:28 +04:00
|
|
|
if tagIdx = bytes.Index(of.bufHead, append([]byte{0, 0, 0}, (strings.Title(tag)+"=")...)); tagIdx == -1 {
|
|
|
|
return ""
|
|
|
|
}
|
2023-10-01 06:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tagIdx += 3
|
2023-10-01 03:34:16 +04:00
|
|
|
tagNameLen := len(tag) + 1
|
|
|
|
valStart := tagIdx + tagNameLen
|
2023-10-05 17:26:13 +04:00
|
|
|
valLen := int(of.bufHead[tagIdx-4]) - tagNameLen
|
|
|
|
return string(of.bufHead[valStart : valStart+valLen])
|
2023-10-01 03:34:16 +04:00
|
|
|
}
|
2023-10-02 01:08:24 +04:00
|
|
|
|
2023-10-05 17:26:13 +04:00
|
|
|
// GetDuration returns song's duration in milliseconds.
|
|
|
|
func (of *OggFile) GetDuration() time.Duration {
|
|
|
|
rateIdx := bytes.Index(of.bufHead, []byte(Vorbis)) +
|
|
|
|
VorbisLen + 5
|
|
|
|
rateBytes := of.bufHead[rateIdx : rateIdx+4]
|
2023-10-02 01:08:24 +04:00
|
|
|
rate := int32(rateBytes[0]) + int32(rateBytes[1])<<8 +
|
|
|
|
int32(rateBytes[2])<<16 + int32(rateBytes[3])<<24
|
|
|
|
|
2023-10-09 00:15:21 +04:00
|
|
|
granuleIdx := bytes.LastIndex(of.bufLast, []byte(OggS)) +
|
2023-10-05 17:26:13 +04:00
|
|
|
OggSLen + 2
|
2023-10-09 00:15:21 +04:00
|
|
|
granuleBytes := of.bufLast[granuleIdx : granuleIdx+8]
|
2023-10-02 01:08:24 +04:00
|
|
|
granule := int64(granuleBytes[0]) + int64(granuleBytes[1])<<8 +
|
|
|
|
int64(granuleBytes[2])<<16 + int64(granuleBytes[3])<<24 +
|
|
|
|
int64(granuleBytes[4])<<32 + int64(granuleBytes[5])<<40 +
|
|
|
|
int64(granuleBytes[6])<<48 + int64(granuleBytes[7])<<56
|
|
|
|
|
|
|
|
return time.Duration(granule*1000/int64(rate)) * time.Millisecond
|
|
|
|
}
|