61 lines
1.0 KiB
Go
61 lines
1.0 KiB
Go
package mov
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"errors"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
const epochAdjust = 2082844800
|
|
|
|
// Created attempts to find a created time from the metadata in a .mov file.
|
|
func Created(path string) (time.Time, error) {
|
|
var r time.Time
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return r, err
|
|
}
|
|
defer f.Close()
|
|
|
|
buf := [8]byte{}
|
|
for {
|
|
_, err := f.Read(buf[:])
|
|
if err != nil {
|
|
return r, err
|
|
}
|
|
if bytes.Equal(buf[4:8], []byte("moov")) {
|
|
break
|
|
} else {
|
|
atomSize := binary.BigEndian.Uint32(buf[:])
|
|
f.Seek(int64(atomSize)-8, 1)
|
|
}
|
|
}
|
|
|
|
_, err = f.Read(buf[:])
|
|
if err != nil {
|
|
return r, err
|
|
}
|
|
|
|
s := string(buf[4:8])
|
|
switch s {
|
|
case "mvhd":
|
|
if _, err := f.Seek(4, 1); err != nil {
|
|
return r, err
|
|
}
|
|
_, err = f.Read(buf[:4])
|
|
if err != nil {
|
|
return r, err
|
|
}
|
|
i := int64(binary.BigEndian.Uint32(buf[:4]))
|
|
c := i - epochAdjust
|
|
u := time.Unix(c, 0).Local()
|
|
return u, nil
|
|
case "cmov":
|
|
return r, errors.New("moov atom is compressed")
|
|
default:
|
|
return r, errors.New("expected to find 'mvhd' header, didn't")
|
|
}
|
|
}
|