2015-03-19 13:21:53 +01:00
|
|
|
// Copyright 2015, David Howden
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package tag
|
|
|
|
|
|
|
|
import (
|
2016-03-06 12:24:05 +01:00
|
|
|
"bytes"
|
2015-03-19 13:21:53 +01:00
|
|
|
"encoding/binary"
|
2015-05-25 08:45:31 +02:00
|
|
|
"errors"
|
2015-03-19 13:21:53 +01:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strconv"
|
2019-02-01 02:22:21 +01:00
|
|
|
"strings"
|
2015-03-19 13:21:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var atomTypes = map[int]string{
|
2016-03-06 12:24:05 +01:00
|
|
|
0: "implicit", // automatic based on atom name
|
2015-03-19 13:21:53 +01:00
|
|
|
1: "text",
|
|
|
|
13: "jpeg",
|
|
|
|
14: "png",
|
|
|
|
21: "uint8",
|
|
|
|
}
|
|
|
|
|
2015-05-27 00:43:47 +02:00
|
|
|
// NB: atoms does not include "----", this is handled separately
|
2015-03-19 13:21:53 +01:00
|
|
|
var atoms = atomNames(map[string]string{
|
|
|
|
"\xa9alb": "album",
|
|
|
|
"\xa9art": "artist",
|
|
|
|
"\xa9ART": "artist",
|
|
|
|
"aART": "album_artist",
|
|
|
|
"\xa9day": "year",
|
|
|
|
"\xa9nam": "title",
|
|
|
|
"\xa9gen": "genre",
|
|
|
|
"trkn": "track",
|
|
|
|
"\xa9wrt": "composer",
|
|
|
|
"\xa9too": "encoder",
|
|
|
|
"cprt": "copyright",
|
|
|
|
"covr": "picture",
|
|
|
|
"\xa9grp": "grouping",
|
|
|
|
"keyw": "keyword",
|
|
|
|
"\xa9lyr": "lyrics",
|
|
|
|
"\xa9cmt": "comment",
|
|
|
|
"tmpo": "tempo",
|
|
|
|
"cpil": "compilation",
|
|
|
|
"disk": "disc",
|
|
|
|
})
|
|
|
|
|
2016-03-06 12:24:05 +01:00
|
|
|
// Detect PNG image if "implicit" class is used
|
|
|
|
var pngHeader = []byte{137, 80, 78, 71, 13, 10, 26, 10}
|
|
|
|
|
2015-03-19 13:21:53 +01:00
|
|
|
type atomNames map[string]string
|
|
|
|
|
|
|
|
func (f atomNames) Name(n string) []string {
|
|
|
|
res := make([]string, 1)
|
|
|
|
for k, v := range f {
|
|
|
|
if v == n {
|
|
|
|
res = append(res, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// metadataMP4 is the implementation of Metadata for MP4 tag (atom) data.
|
2016-02-22 11:01:12 +01:00
|
|
|
type metadataMP4 struct {
|
|
|
|
fileType FileType
|
|
|
|
data map[string]interface{}
|
|
|
|
}
|
2015-03-19 13:21:53 +01:00
|
|
|
|
2015-04-14 16:06:32 +02:00
|
|
|
// ReadAtoms reads MP4 metadata atoms from the io.ReadSeeker into a Metadata, returning
|
|
|
|
// non-nil error if there was a problem.
|
|
|
|
func ReadAtoms(r io.ReadSeeker) (Metadata, error) {
|
2016-02-22 11:01:12 +01:00
|
|
|
m := metadataMP4{
|
2016-03-06 12:24:05 +01:00
|
|
|
data: make(map[string]interface{}),
|
2016-02-22 11:01:12 +01:00
|
|
|
fileType: UnknownFileType,
|
|
|
|
}
|
2015-06-07 04:58:58 +02:00
|
|
|
err := m.readAtoms(r)
|
2015-03-19 13:21:53 +01:00
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
|
2015-04-14 16:08:53 +02:00
|
|
|
func (m metadataMP4) readAtoms(r io.ReadSeeker) error {
|
2015-03-19 13:21:53 +01:00
|
|
|
for {
|
2015-05-27 00:43:47 +02:00
|
|
|
name, size, err := readAtomHeader(r)
|
2015-03-19 13:21:53 +01:00
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch name {
|
|
|
|
case "meta":
|
|
|
|
// next_item_id (int32)
|
|
|
|
_, err := readBytes(r, 4)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fallthrough
|
2015-05-27 00:43:47 +02:00
|
|
|
|
2015-03-19 13:21:53 +01:00
|
|
|
case "moov", "udta", "ilst":
|
|
|
|
return m.readAtoms(r)
|
2015-05-27 00:43:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := atoms[name]
|
2019-02-01 02:22:21 +01:00
|
|
|
var data []string
|
2015-05-27 00:43:47 +02:00
|
|
|
if name == "----" {
|
2019-02-01 02:22:21 +01:00
|
|
|
name, data, err = readCustomAtom(r, size)
|
2015-04-14 16:08:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-27 00:43:47 +02:00
|
|
|
|
|
|
|
if name != "----" {
|
|
|
|
ok = true
|
2019-02-01 02:22:21 +01:00
|
|
|
size = 0 // already read data
|
2015-05-27 00:43:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
2018-04-02 03:06:41 +02:00
|
|
|
_, err := r.Seek(int64(size-8), io.SeekCurrent)
|
2015-05-25 00:33:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
2015-03-19 13:21:53 +01:00
|
|
|
}
|
|
|
|
|
2019-02-01 02:22:21 +01:00
|
|
|
err = m.readAtomData(r, name, size-8, data)
|
2015-03-19 13:21:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-27 00:43:47 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-19 13:21:53 +01:00
|
|
|
|
2019-02-01 02:22:21 +01:00
|
|
|
func (m metadataMP4) readAtomData(r io.ReadSeeker, name string, size uint32, processedData []string) error {
|
|
|
|
var b []byte
|
|
|
|
var err error
|
|
|
|
var contentType string
|
|
|
|
if len(processedData) > 0 {
|
|
|
|
b = []byte(strings.Join(processedData, ";")) // add delimiter if multiple data fields
|
|
|
|
contentType = "text"
|
|
|
|
} else {
|
|
|
|
// read the data
|
2019-11-22 12:50:59 +01:00
|
|
|
b, err = readBytes(r, uint(size))
|
2019-02-01 02:22:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(b) < 8 {
|
|
|
|
return fmt.Errorf("invalid encoding: expected at least %d bytes, got %d", 8, len(b))
|
|
|
|
}
|
2018-02-07 08:32:42 +01:00
|
|
|
|
2019-02-01 02:22:21 +01:00
|
|
|
// "data" + size (4 bytes each)
|
|
|
|
b = b[8:]
|
2015-05-27 00:43:47 +02:00
|
|
|
|
2019-02-01 02:22:21 +01:00
|
|
|
if len(b) < 3 {
|
|
|
|
return fmt.Errorf("invalid encoding: expected at least %d bytes, for class, got %d", 3, len(b))
|
|
|
|
}
|
|
|
|
class := getInt(b[1:4])
|
|
|
|
var ok bool
|
|
|
|
contentType, ok = atomTypes[class]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid content type: %v (%x) (%x)", class, b[1:4], b)
|
|
|
|
}
|
2015-05-27 00:43:47 +02:00
|
|
|
|
2019-02-01 02:22:21 +01:00
|
|
|
// 4: atom version (1 byte) + atom flags (3 bytes)
|
|
|
|
// 4: NULL (usually locale indicator)
|
|
|
|
if len(b) < 8 {
|
|
|
|
return fmt.Errorf("invalid encoding: expected at least %d bytes, for atom version and flags, got %d", 8, len(b))
|
|
|
|
}
|
|
|
|
b = b[8:]
|
2018-02-17 03:07:03 +01:00
|
|
|
}
|
2015-05-27 00:43:47 +02:00
|
|
|
|
|
|
|
if name == "trkn" || name == "disk" {
|
2018-02-17 03:07:03 +01:00
|
|
|
if len(b) < 6 {
|
|
|
|
return fmt.Errorf("invalid encoding: expected at least %d bytes, for track and disk numbers, got %d", 6, len(b))
|
|
|
|
}
|
|
|
|
|
2016-02-22 11:01:12 +01:00
|
|
|
m.data[name] = int(b[3])
|
|
|
|
m.data[name+"_count"] = int(b[5])
|
2015-05-27 00:43:47 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-06 12:24:05 +01:00
|
|
|
if contentType == "implicit" {
|
|
|
|
if name == "covr" {
|
|
|
|
if bytes.HasPrefix(b, pngHeader) {
|
|
|
|
contentType = "png"
|
|
|
|
}
|
|
|
|
// TODO(dhowden): Detect JPEG formats too (harder).
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 00:43:47 +02:00
|
|
|
var data interface{}
|
|
|
|
switch contentType {
|
2016-03-06 12:24:05 +01:00
|
|
|
case "implicit":
|
|
|
|
if _, ok := atoms[name]; ok {
|
|
|
|
return fmt.Errorf("unhandled implicit content type for required atom: %q", name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
2015-05-27 00:43:47 +02:00
|
|
|
case "text":
|
|
|
|
data = string(b)
|
|
|
|
|
|
|
|
case "uint8":
|
2018-02-17 03:07:03 +01:00
|
|
|
if len(b) < 1 {
|
|
|
|
return fmt.Errorf("invalid encoding: expected at least %d bytes, for integer tag data, got %d", 1, len(b))
|
|
|
|
}
|
2015-05-27 00:43:47 +02:00
|
|
|
data = getInt(b[:1])
|
|
|
|
|
|
|
|
case "jpeg", "png":
|
|
|
|
data = &Picture{
|
|
|
|
Ext: contentType,
|
|
|
|
MIMEType: "image/" + contentType,
|
|
|
|
Data: b,
|
2015-03-19 13:21:53 +01:00
|
|
|
}
|
2015-05-27 00:43:47 +02:00
|
|
|
}
|
2016-02-22 11:01:12 +01:00
|
|
|
m.data[name] = data
|
2015-03-19 13:21:53 +01:00
|
|
|
|
2015-05-27 00:43:47 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readAtomHeader(r io.ReadSeeker) (name string, size uint32, err error) {
|
|
|
|
err = binary.Read(r, binary.BigEndian, &size)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name, err = readString(r, 4)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generic atom.
|
|
|
|
// Should have 3 sub atoms : mean, name and data.
|
|
|
|
// We check that mean is "com.apple.iTunes" and we use the subname as
|
|
|
|
// the name, and move to the data atom.
|
2019-02-01 02:22:21 +01:00
|
|
|
// Data atom could have multiple data values, each with a header.
|
2015-05-27 00:43:47 +02:00
|
|
|
// If anything goes wrong, we jump at the end of the "----" atom.
|
2019-02-01 02:22:21 +01:00
|
|
|
func readCustomAtom(r io.ReadSeeker, size uint32) (_ string, data []string, _ error) {
|
2015-05-27 00:43:47 +02:00
|
|
|
subNames := make(map[string]string)
|
|
|
|
|
|
|
|
for size > 8 {
|
|
|
|
subName, subSize, err := readAtomHeader(r)
|
|
|
|
if err != nil {
|
2019-02-01 02:22:21 +01:00
|
|
|
return "", nil, err
|
2015-03-19 13:21:53 +01:00
|
|
|
}
|
|
|
|
|
2015-05-27 00:43:47 +02:00
|
|
|
// Remove the size of the atom from the size counter
|
2019-02-01 02:22:21 +01:00
|
|
|
if size >= subSize {
|
|
|
|
size -= subSize
|
|
|
|
} else {
|
|
|
|
return "", nil, errors.New("--- invalid size")
|
|
|
|
}
|
2015-05-27 00:43:47 +02:00
|
|
|
|
2019-11-22 12:50:59 +01:00
|
|
|
b, err := readBytes(r, uint(subSize-8))
|
2019-02-01 02:22:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(b) < 4 {
|
|
|
|
return "", nil, fmt.Errorf("invalid encoding: expected at least %d bytes, got %d", 4, len(b))
|
|
|
|
}
|
2015-05-27 00:43:47 +02:00
|
|
|
switch subName {
|
|
|
|
case "mean", "name":
|
|
|
|
subNames[subName] = string(b[4:])
|
|
|
|
case "data":
|
2019-02-01 02:22:21 +01:00
|
|
|
data = append(data, string(b[4:]))
|
2015-03-19 13:21:53 +01:00
|
|
|
}
|
|
|
|
}
|
2015-05-27 00:43:47 +02:00
|
|
|
|
|
|
|
// there should remain only the header size
|
|
|
|
if size != 8 {
|
|
|
|
err := errors.New("---- atom out of bounds")
|
2019-02-01 02:22:21 +01:00
|
|
|
return "", nil, err
|
2015-05-27 00:43:47 +02:00
|
|
|
}
|
|
|
|
|
2019-02-01 02:22:21 +01:00
|
|
|
if subNames["mean"] != "com.apple.iTunes" || subNames["name"] == "" || len(data) == 0 {
|
|
|
|
return "----", nil, nil
|
2015-05-27 00:43:47 +02:00
|
|
|
}
|
2019-02-01 02:22:21 +01:00
|
|
|
return subNames["name"], data, nil
|
2015-03-19 13:21:53 +01:00
|
|
|
}
|
|
|
|
|
2016-03-06 12:24:05 +01:00
|
|
|
func (metadataMP4) Format() Format { return MP4 }
|
2016-02-22 11:01:12 +01:00
|
|
|
func (m metadataMP4) FileType() FileType { return m.fileType }
|
2015-03-19 13:21:53 +01:00
|
|
|
|
2016-02-22 11:01:12 +01:00
|
|
|
func (m metadataMP4) Raw() map[string]interface{} { return m.data }
|
2015-03-19 13:21:53 +01:00
|
|
|
|
|
|
|
func (m metadataMP4) getString(n []string) string {
|
|
|
|
for _, k := range n {
|
2016-02-22 11:01:12 +01:00
|
|
|
if x, ok := m.data[k]; ok {
|
2015-03-19 13:21:53 +01:00
|
|
|
return x.(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m metadataMP4) getInt(n []string) int {
|
|
|
|
for _, k := range n {
|
2016-02-22 11:01:12 +01:00
|
|
|
if x, ok := m.data[k]; ok {
|
2015-03-19 13:21:53 +01:00
|
|
|
return x.(int)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m metadataMP4) Title() string {
|
|
|
|
return m.getString(atoms.Name("title"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m metadataMP4) Artist() string {
|
|
|
|
return m.getString(atoms.Name("artist"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m metadataMP4) Album() string {
|
|
|
|
return m.getString(atoms.Name("album"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m metadataMP4) AlbumArtist() string {
|
|
|
|
return m.getString(atoms.Name("album_artist"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m metadataMP4) Composer() string {
|
|
|
|
return m.getString(atoms.Name("composer"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m metadataMP4) Genre() string {
|
|
|
|
return m.getString(atoms.Name("genre"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m metadataMP4) Year() int {
|
|
|
|
date := m.getString(atoms.Name("year"))
|
|
|
|
if len(date) >= 4 {
|
|
|
|
year, _ := strconv.Atoi(date[:4])
|
|
|
|
return year
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m metadataMP4) Track() (int, int) {
|
|
|
|
x := m.getInt([]string{"trkn"})
|
2016-02-22 11:01:12 +01:00
|
|
|
if n, ok := m.data["trkn_count"]; ok {
|
2015-03-19 13:21:53 +01:00
|
|
|
return x, n.(int)
|
|
|
|
}
|
|
|
|
return x, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m metadataMP4) Disc() (int, int) {
|
|
|
|
x := m.getInt([]string{"disk"})
|
2016-02-22 11:01:12 +01:00
|
|
|
if n, ok := m.data["disk_count"]; ok {
|
2015-03-19 13:21:53 +01:00
|
|
|
return x, n.(int)
|
|
|
|
}
|
|
|
|
return x, 0
|
|
|
|
}
|
|
|
|
|
2015-05-18 09:32:54 +02:00
|
|
|
func (m metadataMP4) Lyrics() string {
|
2016-02-22 11:01:12 +01:00
|
|
|
t, ok := m.data["\xa9lyr"]
|
2015-05-18 09:32:54 +02:00
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return t.(string)
|
|
|
|
}
|
|
|
|
|
2018-11-04 21:56:00 +01:00
|
|
|
func (m metadataMP4) Comment() string {
|
|
|
|
t, ok := m.data["\xa9cmt"]
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return t.(string)
|
|
|
|
}
|
|
|
|
|
2015-03-19 13:21:53 +01:00
|
|
|
func (m metadataMP4) Picture() *Picture {
|
2016-02-22 11:01:12 +01:00
|
|
|
v, ok := m.data["covr"]
|
2015-03-19 13:21:53 +01:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-05 06:17:11 +01:00
|
|
|
p, _ := v.(*Picture)
|
|
|
|
return p
|
2015-03-19 13:21:53 +01:00
|
|
|
}
|