From 30fae5b7ee518758eb521e113c7b9ff41c1fd419 Mon Sep 17 00:00:00 2001 From: stephen mcquay Date: Sat, 10 Dec 2016 15:28:23 -0800 Subject: [PATCH] init --- license | 27 +++++++++++++++++++++++++ mov.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ readme.md | 3 +++ 3 files changed, 90 insertions(+) create mode 100644 license create mode 100644 mov.go create mode 100644 readme.md diff --git a/license b/license new file mode 100644 index 0000000..8b4436a --- /dev/null +++ b/license @@ -0,0 +1,27 @@ +Copyright (c) 2016, stephen mcquay + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of smcquay nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/mov.go b/mov.go new file mode 100644 index 0000000..88cfa5a --- /dev/null +++ b/mov.go @@ -0,0 +1,60 @@ +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") + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..b7e5bb8 --- /dev/null +++ b/readme.md @@ -0,0 +1,3 @@ +packge mov + +simple package to parse out creation time from a .mov file