mov/mov.go

83 lines
1.6 KiB
Go
Raw Normal View History

2016-12-10 15:28:23 -08:00
package mov
import (
"bytes"
"encoding/binary"
"errors"
2016-12-10 15:41:31 -08:00
"io"
2016-12-10 15:28:23 -08:00
"time"
)
const epochAdjust = 2082844800
// Created attempts to find a created time from the metadata in a .mov file.
2016-12-10 15:41:31 -08:00
func Created(file io.ReadSeeker) (time.Time, error) {
2016-12-10 15:50:21 -08:00
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
}
2016-12-10 16:12:54 -08:00
// times seeks around in file and finds Created and Modified times.
//
// This was transcibed from http://stackoverflow.com/a/21395803
2016-12-10 15:50:21 -08:00
func times(file io.ReadSeeker) (time.Time, time.Time, error) {
var c, m time.Time
var i int64
2016-12-10 15:41:31 -08:00
var err error
2016-12-10 15:28:23 -08:00
buf := [8]byte{}
for {
2016-12-10 15:41:31 -08:00
_, err := file.Read(buf[:])
2016-12-10 15:28:23 -08:00
if err != nil {
2016-12-10 15:50:21 -08:00
return c, m, err
2016-12-10 15:28:23 -08:00
}
if bytes.Equal(buf[4:8], []byte("moov")) {
break
} else {
atomSize := binary.BigEndian.Uint32(buf[:])
2016-12-10 15:41:31 -08:00
file.Seek(int64(atomSize)-8, 1)
2016-12-10 15:28:23 -08:00
}
}
2016-12-10 15:41:31 -08:00
_, err = file.Read(buf[:])
2016-12-10 15:28:23 -08:00
if err != nil {
2016-12-10 15:50:21 -08:00
return c, m, err
2016-12-10 15:28:23 -08:00
}
s := string(buf[4:8])
switch s {
case "mvhd":
2016-12-10 15:41:31 -08:00
if _, err := file.Seek(4, 1); err != nil {
2016-12-10 15:50:21 -08:00
return c, m, err
}
_, err = file.Read(buf[:4])
if err != nil {
return c, m, err
2016-12-10 15:28:23 -08:00
}
2016-12-10 15:50:21 -08:00
i = int64(binary.BigEndian.Uint32(buf[:4]))
c := time.Unix(i-epochAdjust, 0).Local()
2016-12-10 15:41:31 -08:00
_, err = file.Read(buf[:4])
2016-12-10 15:28:23 -08:00
if err != nil {
2016-12-10 15:50:21 -08:00
return c, m, err
2016-12-10 15:28:23 -08:00
}
2016-12-10 15:50:21 -08:00
i = int64(binary.BigEndian.Uint32(buf[:4]))
m := time.Unix(i-epochAdjust, 0).Local()
return c, m, nil
2016-12-10 15:28:23 -08:00
case "cmov":
2016-12-10 15:50:21 -08:00
return c, m, errors.New("moov atom is compressed")
2016-12-10 15:28:23 -08:00
default:
2016-12-10 15:50:21 -08:00
return c, m, errors.New("expected to find 'mvhd' header, didn't")
2016-12-10 15:28:23 -08:00
}
}