From 99266c3834748136e24e3f02e8153babe21ee86e Mon Sep 17 00:00:00 2001 From: Martin Riedl Date: Thu, 17 Oct 2024 18:12:50 +0200 Subject: [PATCH] new time conversion of timestamp fields --- DateTime.go | 21 +++++++++++++++++++++ MovieHeaderBox.go | 17 +++++++++-------- 2 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 DateTime.go diff --git a/DateTime.go b/DateTime.go new file mode 100644 index 0000000..48616a4 --- /dev/null +++ b/DateTime.go @@ -0,0 +1,21 @@ +// Copyright 2024 Martin Riedl +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package gomp4 + +import "time" + +func since1904ToTime(sec int64) time.Time { + return time.Date(1904, time.January, 1, 0, 0, 0, 0, time.UTC).Add(time.Second * time.Duration(sec)) +} diff --git a/MovieHeaderBox.go b/MovieHeaderBox.go index 680ff6f..66486f3 100644 --- a/MovieHeaderBox.go +++ b/MovieHeaderBox.go @@ -16,6 +16,7 @@ package gomp4 import ( "encoding/binary" + "time" ) // MovieHeaderBox Movie Header Box struct @@ -32,16 +33,16 @@ type MovieHeaderBox struct { *FullBox // is an integer that declares the creation time of the presentation (in seconds // since midnight, Jan. 1, 1904, in UTC time) - CreationTimeV0 uint32 + CreationTimeV0 time.Time // is an integer that declares the creation time of the presentation (in seconds // since midnight, Jan. 1, 1904, in UTC time) - CreationTimeV1 uint64 + CreationTimeV1 time.Time // is an integer that declares the most recent time the presentation was // modified (in seconds since midnight, Jan. 1, 1904, in UTC time) - ModificationTimeV0 uint32 + ModificationTimeV0 time.Time // is an integer that declares the most recent time the presentation was // modified (in seconds since midnight, Jan. 1, 1904, in UTC time) - ModificationTimeV1 uint64 + ModificationTimeV1 time.Time // is an integer that specifies the time‐scale for the entire presentation; this is the // number of time units that pass in one second. For example, a time coordinate system that // measures time in sixtieths of a second has a time scale of 60. @@ -81,14 +82,14 @@ func ParseMovieHeaderBox(filePosition uint64, headerSize uint32, content []byte) position := 4 if box.Version == 1 { - box.CreationTimeV1 = binary.BigEndian.Uint64(content[4:12]) - box.ModificationTimeV1 = binary.BigEndian.Uint64(content[12:20]) + box.CreationTimeV1 = since1904ToTime(int64(binary.BigEndian.Uint64(content[4:12]))) + box.ModificationTimeV1 = since1904ToTime(int64(binary.BigEndian.Uint64(content[12:20]))) box.Timescale = binary.BigEndian.Uint32(content[20:24]) box.DurationV1 = binary.BigEndian.Uint64(content[24:32]) position += 28 } else { // version == 0 - box.CreationTimeV0 = binary.BigEndian.Uint32(content[4:8]) - box.ModificationTimeV0 = binary.BigEndian.Uint32(content[8:12]) + box.CreationTimeV0 = since1904ToTime(int64(binary.BigEndian.Uint32(content[4:8]))) + box.ModificationTimeV0 = since1904ToTime(int64(binary.BigEndian.Uint32(content[8:12]))) box.Timescale = binary.BigEndian.Uint32(content[12:16]) box.DurationV0 = binary.BigEndian.Uint32(content[16:20]) position += 16