added mov.Modified

This commit is contained in:
Stephen McQuay 2016-12-10 15:50:21 -08:00
parent eb77956dc5
commit a8f2df2302
No known key found for this signature in database
GPG Key ID: 1ABF428F71BAFC3D

45
mov.go
View File

@ -12,14 +12,27 @@ const epochAdjust = 2082844800
// Created attempts to find a created time from the metadata in a .mov file.
func Created(file io.ReadSeeker) (time.Time, error) {
var r time.Time
c, _, err := times(file)
return c, err
}
// Modified attempts to find a modification time from the metadata in a .mov
// file.
func Modified(file io.ReadSeeker) (time.Time, error) {
_, m, err := times(file)
return m, err
}
func times(file io.ReadSeeker) (time.Time, time.Time, error) {
var c, m time.Time
var i int64
var err error
buf := [8]byte{}
for {
_, err := file.Read(buf[:])
if err != nil {
return r, err
return c, m, err
}
if bytes.Equal(buf[4:8], []byte("moov")) {
break
@ -31,26 +44,36 @@ func Created(file io.ReadSeeker) (time.Time, error) {
_, err = file.Read(buf[:])
if err != nil {
return r, err
return c, m, err
}
s := string(buf[4:8])
switch s {
case "mvhd":
if _, err := file.Seek(4, 1); err != nil {
return r, err
return c, m, err
}
_, err = file.Read(buf[:4])
if err != nil {
return r, err
return c, m, err
}
i := int64(binary.BigEndian.Uint32(buf[:4]))
c := i - epochAdjust
u := time.Unix(c, 0).Local()
return u, nil
i = int64(binary.BigEndian.Uint32(buf[:4]))
c := time.Unix(i-epochAdjust, 0).Local()
_, err = file.Read(buf[:4])
if err != nil {
return c, m, err
}
i = int64(binary.BigEndian.Uint32(buf[:4]))
m := time.Unix(i-epochAdjust, 0).Local()
return c, m, nil
case "cmov":
return r, errors.New("moov atom is compressed")
return c, m, errors.New("moov atom is compressed")
default:
return r, errors.New("expected to find 'mvhd' header, didn't")
return c, m, errors.New("expected to find 'mvhd' header, didn't")
}
}