Metadata: extract duration fom QuickTime/MP4-style containers

We want the duration of videos for two reasons:
 - To display the duration of the video in the profile plot.
 - To be able to determine which dive a video is closer to if the
   start is not during a dive.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-07-10 20:03:26 +02:00
parent 52cc51f906
commit 02ad18d4d8
2 changed files with 10 additions and 0 deletions

View file

@ -158,18 +158,26 @@ static bool parseMP4(QFile &f, metadata *metadata)
if (f.read(&data[0], atom_size) != static_cast<int>(atom_size))
break;
uint64_t timestamp = 0;
uint32_t timescale = 0;
uint64_t duration = 0;
// First byte is version. We know version 0 and 1
switch (data[0]) {
case 0:
timestamp = getBE<uint32_t>(&data[4]);
timescale = getBE<uint32_t>(&data[12]);
duration = getBE<uint32_t>(&data[16]);
break;
case 1:
timestamp = getBE<uint64_t>(&data[4]);
timescale = getBE<uint32_t>(&data[20]);
duration = getBE<uint64_t>(&data[24]);
break;
default:
// For unknown versions: ignore -> maybe we find a parseable "mdhd" atom later in this file
break;
}
if (timescale > 0)
metadata->duration.seconds = lrint((double)duration / timescale);
// Timestamp is given as seconds since midnight 1904/1/1. To be convertible to the UNIX epoch
// it must be larger than 2082844800.
if (timestamp >= 2082844800) {
@ -194,6 +202,7 @@ static bool parseMP4(QFile &f, metadata *metadata)
extern "C" mediatype_t get_metadata(const char *filename_in, metadata *data)
{
data->timestamp = 0;
data->duration.seconds = 0;
data->latitude.udeg = 0;
data->longitude.udeg = 0;

View file

@ -5,6 +5,7 @@
struct metadata {
timestamp_t timestamp;
duration_t duration;
degrees_t latitude;
degrees_t longitude;
};