media: read timestamp from mvhd header of MP4/QuickTime videos

ExifTools (and probably other meta-data editors) modifies the
mvhd creation date, but leaves the individual creation dates
in the tracks unchanged. Therefore, use the mvhd atom.

Reported-by: Eric Tanguy <erictanguy2@orange.fr>
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-09-19 13:51:44 +02:00 committed by Dirk Hohndel
parent 4998052a78
commit 75be59c76d
2 changed files with 14 additions and 3 deletions

View file

@ -1,4 +1,4 @@
- media: read timestamp from mvhd atom of QuickTime/MP4 style videos
--- ---
* Always add new entries at the very top of this file above other existing entries and this note. * Always add new entries at the very top of this file above other existing entries and this note.
* Use this layout for new entries: `[Area]: [Details about the change] [reference thread / issue]` * Use this layout for new entries: `[Area]: [Details about the change] [reference thread / issue]`

View file

@ -184,6 +184,18 @@ static bool parseMP4(QFile &f, metadata *metadata)
// Recurse into "moov", "trak", "mdia" and "udta" atoms // Recurse into "moov", "trak", "mdia" and "udta" atoms
atom_stack.push_back(atom_size); atom_stack.push_back(atom_size);
continue; continue;
} else if (!memcmp(type, "mvhd", 4) && atom_size >= 100 && atom_size < 4096) {
std::vector<char> data(atom_size);
if (f.read(&data[0], atom_size) != static_cast<int>(atom_size))
break;
timestamp_t timestamp = getBE<uint32_t>(&data[4]);
// Timestamp is given as seconds since midnight 1904/1/1. To be convertible to the UNIX epoch
// it must be larger than 2082844800.
// Note that we only set timestamp if not already set, because we give priority to XMP data.
if (!metadata->timestamp && timestamp >= 2082844800)
metadata->timestamp = timestamp - 2082844800;
} else if (!memcmp(type, "mdhd", 4) && atom_size >= 24 && atom_size < 4096) { } else if (!memcmp(type, "mdhd", 4) && atom_size >= 24 && atom_size < 4096) {
// Parse "mdhd" (media header). // Parse "mdhd" (media header).
// Sanity check: size between 24 and 4096 // Sanity check: size between 24 and 4096
@ -235,9 +247,8 @@ static bool parseMP4(QFile &f, metadata *metadata)
break; break;
static const char xmp_uid[17] = "\xBE\x7A\xCF\xCB\x97\xA9\x42\xE8\x9C\x71\x99\x94\x91\xE3\xAF\xAC"; static const char xmp_uid[17] = "\xBE\x7A\xCF\xCB\x97\xA9\x42\xE8\x9C\x71\x99\x94\x91\xE3\xAF\xAC";
if (!memcmp(&d[0], xmp_uid, 16)) { if (!memcmp(&d[0], xmp_uid, 16))
parseXMP(&d[16], atom_size - 16, metadata); parseXMP(&d[16], atom_size - 16, metadata);
}
} else { } else {
// Jump over unknown atom // Jump over unknown atom
if (!f.seek(f.pos() + atom_size)) // TODO: switch to QFile::skip() if (!f.seek(f.pos() + atom_size)) // TODO: switch to QFile::skip()