Skip to content

Commit

Permalink
Set last gps timestamp in uploaded video. Fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
malconsei committed Sep 13, 2023
1 parent d145411 commit 07092af
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
19 changes: 14 additions & 5 deletions mapillary_tools/geotag/camm_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def _create_camm_stbl(raw_samples: T.Iterable[sample_parser.RawSample]) -> BoxDi
def create_camm_trak(
raw_samples: T.Sequence[sample_parser.RawSample],
media_timescale: int,
creation_timestamp: int,
) -> BoxDict:
stbl = _create_camm_stbl(raw_samples)

Expand All @@ -158,8 +159,8 @@ def create_camm_trak(
# TODO: find timestamps from mvhd?
# do not set dynamic timestamps (e.g. time.time()) here because we'd like to
# make sure the md5 of the new mp4 file unchanged
"creation_time": 0,
"modification_time": 0,
"creation_time": creation_timestamp,
"modification_time": creation_timestamp,
"timescale": media_timescale,
"duration": media_duration,
"language": 21956,
Expand Down Expand Up @@ -203,8 +204,8 @@ def create_camm_trak(
# TODO: find timestamps from mvhd?
# do not set dynamic timestamps (e.g. time.time()) here because we'd like to
# make sure the md5 of the new mp4 file unchanged
"creation_time": 0,
"modification_time": 0,
"creation_time": creation_timestamp,
"modification_time": creation_timestamp,
# will update the track ID later
"track_ID": 0,
# If the duration of this track cannot be determined then duration is set to all 1s (32-bit maxint).
Expand Down Expand Up @@ -242,7 +243,15 @@ def _f(
camm_samples = list(
convert_points_to_raw_samples(video_metadata.points, media_timescale)
)
camm_trak = create_camm_trak(camm_samples, media_timescale)
# We adhere to some vendor's behavior and set creation_time to the time
# the recording ended, and add 2082844800 to rebase to quicktime epoch 1904-01-01
last_timestamp = (
video_metadata.last_unix_ts + 2082844800
if video_metadata.last_unix_ts
else 0
)
creation_timestamp = int(last_timestamp)
camm_trak = create_camm_trak(camm_samples, media_timescale, creation_timestamp)
elst = _create_edit_list(
[video_metadata.points], movie_timescale, media_timescale
)
Expand Down
9 changes: 9 additions & 0 deletions mapillary_tools/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class VideoMetadata:
points: T.Sequence[geo.Point]
make: T.Optional[str] = None
model: T.Optional[str] = None
last_unix_ts: T.Optional[int] = None # GPS time of the last point as Unix ts

def update_md5sum(self) -> None:
if self.md5sum is None:
Expand Down Expand Up @@ -156,6 +157,7 @@ class _VideoDescriptionRequired(TypedDict, total=True):
class VideoDescription(_VideoDescriptionRequired, total=False):
MAPDeviceMake: str
MAPDeviceModel: str
MAPLastUnixTimestamp: int


class _ErrorDescription(TypedDict, total=False):
Expand Down Expand Up @@ -328,6 +330,10 @@ def describe_error_metadata(
"type": "string",
"description": "Device model, e.g. HERO10 Black, DR900S-1CH, Insta360 Titan",
},
"MAPLastUnixTimestamp": {
"type": "integer",
"description": "UNIX timestamp of the last GPS point"
}
},
"required": [
"MAPGPSTrack",
Expand Down Expand Up @@ -493,6 +499,8 @@ def _as_video_desc(metadata: VideoMetadata) -> VideoDescription:
desc["MAPDeviceMake"] = metadata.make
if metadata.model:
desc["MAPDeviceModel"] = metadata.model
if metadata.last_unix_ts:
desc["MAPLastUnixTimestamp"] = metadata.last_unix_ts
return desc


Expand Down Expand Up @@ -594,6 +602,7 @@ def _from_video_desc(desc: VideoDescription) -> VideoMetadata:
points=[_decode_point(entry) for entry in desc["MAPGPSTrack"]],
make=desc.get("MAPDeviceMake"),
model=desc.get("MAPDeviceModel"),
last_unix_ts=desc.get("MAPLastUnixTimestamp")
)


Expand Down
12 changes: 8 additions & 4 deletions mapillary_tools/video_data_extraction/extract_video_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def process(self) -> T.List[MetadataOrError]:

def process_file(self, file: Path) -> VideoMetadataOrError:
parsers = video_data_parser_factory.make_parsers(file, self.options)
points: T.Sequence[geo.Point] = []
points: T.Sequence[geo.GpsPoint] = []
make = self.options["device_make"]
model = self.options["device_model"]

Expand All @@ -84,6 +84,9 @@ def process_file(self, file: Path) -> VideoMetadataOrError:
{**log_vars, "e": e},
)

last_ts = points[-1].unix_timestamp_ms if len(points) > 0 else None
last_unix_ts = int(last_ts/1000) if last_ts else None

# After trying all parsers, return the points if we found any, otherwise
# the last exception thrown or a default one.
# Note that if we have points, we return them, regardless of exceptions
Expand All @@ -93,9 +96,10 @@ def process_file(self, file: Path) -> VideoMetadataOrError:
filename=file,
filetype=FileType.VIDEO,
md5sum=None,
points=points,
points=self._gps_points_to_points(points),
make=make,
model=model,
last_unix_ts=last_unix_ts
)
video_metadata.update_md5sum()
return video_metadata
Expand All @@ -112,7 +116,7 @@ def process_file(self, file: Path) -> VideoMetadataOrError:

def _extract_points(
self, parser: BaseParser, log_vars: T.Dict
) -> T.Sequence[geo.Point]:
) -> T.Sequence[geo.GpsPoint]:
points = parser.extract_points()
if points:
LOG.debug(
Expand All @@ -125,7 +129,7 @@ def _extract_points(
if parser.must_rebase_times_to_zero:
points = self._rebase_times(points)

return self._gps_points_to_points(points)
return points

@staticmethod
def _check_paths(import_paths: T.Sequence[Path]):
Expand Down
4 changes: 4 additions & 0 deletions schema/image_description_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
"type": "string",
"description": "Device model, e.g. HERO10 Black, DR900S-1CH, Insta360 Titan"
},
"MAPLastUnixTimestamp": {
"type": "integer",
"description": "UNIX timestamp of the last GPS point"
},
"filename": {
"type": "string",
"description": "Absolute path of the video"
Expand Down

0 comments on commit 07092af

Please sign in to comment.