Skip to content

Accept ISO 8601 (with timezone) for --video_start_time - #822

Merged
caglarpir merged 3 commits into
mainfrom
video-start-time-iso8601
Dec 2, 2026
Merged

caglarpir merged 3 commits into
mainfrom
video-start-time-iso8601

Conversation

@ptpt

@ptpt ptpt commented Jun 24, 2026 •

Copy link
Copy Markdown
Member

Summary

Addresses part of #819 (GoPro MAX timestamping). The --video_start_time option only accepted the proprietary YYYY_MM_DD_HH_MM_SS_sss format, which is always interpreted as UTC. This left no clean way to correct timestamps for cameras whose RTC has no timezone and therefore records local time (e.g. GoPro MAX).

This PR makes --video_start_time also accept ISO 8601 with a UTC offset:

Input Interpreted as Result (UTC)
2020_12_28_13_36_36_508 (legacy) UTC 13:36:36Z
2020-12-28T13:36:36.508Z UTC 13:36:36Z
2020-12-28T13:36:36.508+01:00 offset as given 12:36:36Z
2020-12-28T13:36:36.508 (no offset) refused error asking for the offset of the camera clock
  • The legacy format is unchanged (still UTC).
  • An ISO 8601 value with an offset (Z, +hh, +hhmm or +hh:mm) is honored. Seconds and fractions of any length are optional.
  • A value without an offset is refused: read in the computer's local time zone, the same command would give another time on a server in UTC or a laptop abroad. The error asks for the offset of the clock the time was read from (such as +01:00 for a camera set to Central European Time), and for Z only if that clock is set to UTC.
  • As before, --video_start_time applies only to videos without a GPS clock of their own.

Implementation

_parse_video_start_time() in sample_video.py tries the legacy format first, then parses ISO 8601 with a regular expression. datetime.fromisoformat isn't used because before Python 3.11 it accepts neither Z nor fractions of other than 3 or 6 digits (2026-04-06T14:36:07.88+02:00, 2020-12-28T13:36:36.5Z). The help text and docs/command_reference.md list ISO 8601 first, and the video_process example uses it.

The second commit applies the review points from the discussion below to the first.

This does not address the other items in #819.

Test plan

  • Unit tests for the parser (offsets, fraction lengths, no seconds, refused and invalid values) and for interval sampling with ISO start times; they pass on Python 3.9 and 3.13
  • sample_video CLI: --video_start_time 2026-06-30T18:28:44.5+02:00 gives frames at 16:28:44.5 UTC; a value without an offset exits with code 2 and the error above
  • ruff, usort, mypy (3.9 and 3.13) clean

@JakeSmarter

Copy link
Copy Markdown
Contributor

The bug persists. Although --video_start_time does now accept ISO 8601 format timestamps, the sampled output Exif data remains partially incorrect.

mapillary_tools sample_video --video_sample_distance -1 --video_sample_interval 1 --video_start_time '2026-04-06T14:36:07.88+02:00' 'video.mp4'
⋮
exiv2 -pv 'video.mp4/video_v_000001.jpg'
File 1/1: video.mp4/video_v_000001.jpg
0x8769 Image        ExifTag                     Long        1  38
0x9003 Photo        DateTimeOriginal            Ascii      20  2026:04:06 12:36:07
0x9011 Photo        OffsetTimeOriginal          Ascii       7  +00:00
0x9291 Photo        SubSecTimeOriginal          Ascii       7  880000
0x8825 Image        GPSTag                      Long        1  110
0x0007 GPSInfo      GPSTimeStamp                Rational    3  12/1 36/1 197/25
0x001d GPSInfo      GPSDateStamp                Ascii      11  2026:04:06

In other words, GPSDateStamp and GPSTimeStamp are correct but DateTimeOriginal and OffsetTimeOriginal are not.

There are two subsequent issues here though.

  1. Even if this gets fixed and although the user can specify a video start timestamp with a timezone specifier they still have to know beforehand the exact timestamp when video recording was started. So, unless they run ffprobe on the video file beforehand or run the sample_video command, read the creation_time tag from ffmpeg's verbose output, examine the output Exif timestamps, and then run sample_video again with the correct video start timestamp and a timezone specifier, they have no way of knowing the video start timestamp at all. However, most users should usually know their timezone (beforehand). Hence, a --timezone option may be a good idea after all. Making --video_start_time accept ISO 8601 format timestamps is great but it gets us only half way there.

    And yes, one can pass the --offset_time option to the process command later to sort of adjust the timezone but we should not produce incorrect intermediate results either.

  2. When a video file has no GPS clock signal data (which usually is safe to assume as the reliable clock source) we cannot always rely on creation_time timestamps to be in UTC, especially when written by cameras that have no timezone support. I know, it is working around a bug inflicted on you but since GoPro MAX is a Mapillary recommended camera, we kind of have to live and deal with it. In this case, unless the user passes --video_start_time or --timezone, we could have an interactive prompt that asks the user to verify the detected video start timestamp for correctness (including timezone). This way, incorrect timestamps will not slip through and automation would be served too.
    Unfortunately then though, video files with correctly written UTC creation_time tags would kind of be at a disadvantage in that they should not need any user verification but would still require a --timezone or an explicit lengthy --video_start_time option to pass. 😞 There is no easy way out.

@caglarpir

Copy link
Copy Markdown
Contributor

Thanks for putting this together, ISO 8601 is much friendlier than the underscore format. Sorry it sat for so long, it got stuck behind the 0.15.0 work. It still applies cleanly to main and the doctests pass. A few things I'd like to change before merging:

  1. Naive ISO times. I know @JakeSmarter liked the local-time reading, but it means the same command gives different results depending on where it runs (a server in UTC, a laptop while travelling). I'd rather reject a value without an offset, with an error asking for Z or +hh:mm. Someone copying a wall-clock time off the camera only has to append their offset.
  2. Python 3.9 and 3.10. fromisoformat there only accepts fractional seconds with exactly 3 or 6 digits, so @JakeSmarter's 2026-04-06T14:36:07.88+02:00 raises a ValueError on both, and so does 2020-12-28T13:36:36.5Z. A small regex-based parser would behave the same on every version we support.
  3. Help text and docs. As @JakeSmarter suggested, list ISO 8601 first and the underscore format second. docs/command_reference.md only describes the old format, so it needs an update too.

@JakeSmarter on DateTimeOriginal: mapillary_tools writes capture times in UTC, so 2026:04:06 12:36:07 with OffsetTimeOriginal +00:00 is the same moment as 14:36:07+02:00. The time is right, it's just shown in UTC. The --timezone and prompt ideas fit better in #819. 0.15.0 changed a lot in this area (a GPX is lined up by the video's own GPS clock, and a GPX can be used for a video with no GPS of its own), so a retest of #819 with 0.15.0 would really help.

Happy to make these changes myself if you're short on time, just let me know.

@JakeSmarter

JakeSmarter commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Thank you @caglarpir for considering it. ☺️

  1. Naive ISO times. I know @JakeSmarter liked the local-time reading, but it means the same command gives different results depending on where it runs (a server in UTC, a laptop while travelling). I'd rather reject a value without an offset, with an error asking for Z or +hh:mm. Someone copying a wall-clock time off the camera only has to append their offset.

I fully support your reasoning and approach here. Requiring a timestamp with a timezone offset is a smart move. 👍 However, the error message wording has to be chosen carefully so that users do not simply append Z just to silence the nagging.

  1. Python 3.9 and 3.10. fromisoformat there only accepts fractional seconds with exactly 3 or 6 digits, so @JakeSmarter's 2026-04-06T14:36:07.88+02:00 raises a ValueError on both, and so does 2020-12-28T13:36:36.5Z. A small regex-based parser would behave the same on every version we support.

This is weird but nevertheless a bummer. It sounds reasonable to me to have separate code paths for affected Python versions that extend ISO 8601 timestamps to 6 fraction decimal places.

@JakeSmarter on DateTimeOriginal: mapillary_tools writes capture times in UTC, so 2026:04:06 12:36:07 with OffsetTimeOriginal +00:00 is the same moment as 14:36:07+02:00. The time is right, it's just shown in UTC. The --timezone and prompt ideas fit better in #819. 0.15.0 changed a lot in this area (a GPX is lined up by the video's own GPS clock, and a GPX can be used for a video with no GPS of its own), so a retest of #819 with 0.15.0 would really help.

You are right, it is the same timestamp due to the OffsetTimeOriginal tag. I must have gotten confused because most legacy apps, like image viewers, are oblivious to OffsetTime* tags, so they always treat DateTimeOriginal (solely) as local time. So, it might still be a good idea to write local time values to DateTimeOriginal and original timezone values to OffsetTimeOriginal because although shifting to +00:00 does not change the effective timestamp it does change the semantics of the tags.

ptpt and others added 2 commits December 2, 2026 11:22
The --video_start_time option only accepted the proprietary
YYYY_MM_DD_HH_MM_SS_sss format, which is always interpreted as UTC. This
left no clean way to correct timestamps for cameras whose RTC has no
timezone and records local time (e.g. GoPro MAX), as reported in #819.

Add a _parse_video_start_time() helper that also accepts ISO 8601:
- an explicit UTC offset (or Z) is honored, and
- a naive value is interpreted in the system local timezone.

The legacy format is unchanged and still treated as UTC. Documented via
doctests and updated the CLI help text.
…on version

Review follow-up to the ISO 8601 support for --video_start_time:

- A time without a UTC offset is refused. Read in the local time zone of
  the computer, the same command would place the video at another time
  on a server in UTC or a laptop abroad. The error asks for the offset of
  the clock the time was read from, such as +01:00 for a camera set to
  Central European Time, and for Z only if that clock is set to UTC.
- ISO 8601 is parsed with a regular expression rather than
  datetime.fromisoformat(), which before Python 3.11 accepts neither Z nor
  fractions of other than 3 or 6 digits, such as 2026-04-06T14:36:07.88+02:00.
  Offsets may be Z, +hh, +hhmm or +hh:mm; seconds and fractions are optional.
- The help text and the command reference list ISO 8601 first, and the
  video_process example uses it.
@caglarpir
caglarpir force-pushed the video-start-time-iso8601 branch from 5fb87e2 to c787d00 Compare December 2, 2026 10:28
@caglarpir

Copy link
Copy Markdown
Contributor

I've finished this as offered, so it can go into the next release. On top of the original commit, rebased onto main:

  1. A time without a UTC offset is refused. The error asks for the offset of the clock the time was read from (such as +01:00 for a camera set to Central European Time), and for Z only if that clock is set to UTC, so it doesn't just suggest appending Z.
  2. ISO 8601 is parsed with a regular expression, so 2026-04-06T14:36:07.88+02:00 and 2020-12-28T13:36:36.5Z work the same on Python 3.9 to 3.13.
  3. The help text and docs/command_reference.md list ISO 8601 first.

Thanks @ptpt for the original change, and @JakeSmarter for the review.

…ge times

The README still described --video_start_time as UTC only, and both of its
examples used the underscore format. It now gives ISO 8601 with a UTC offset
first, as the help text and the command reference do, and points to the
command reference for cameras that record local time. The examples use
2025-03-14T08:00:30+01:00, the same instant as before.

A time with an offset at the ends of the datetime range, such as
9999-12-31T23:59-01:00, falls outside that range in UTC. astimezone() raised
OverflowError, which escaped as a traceback instead of an invalid-parameter
error with exit code 2. It is now refused as an invalid video start time.
@caglarpir
caglarpir merged commit 2f62f72 into main Dec 2, 2026
19 checks passed
@JakeSmarter

Copy link
Copy Markdown
Contributor

Excellent! I shall test it the next release. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants