From a1421f9232fbe132146753271557ea77c4871a33 Mon Sep 17 00:00:00 2001 From: Caglar Pir Date: Fri, 4 Dec 2026 14:21:45 +0100 Subject: [PATCH] Upload a video larger than 32 GiB in pieces, which Mapillary can process The upload service stores an upload in chunks of 20 MB, and Mapillary keeps the handle of each chunk, about 210 bytes, with the sequence. A video of about 100 GB, which process allows (MAX_SEQUENCE_FILESIZE is 110 GiB), has a handle of over 1 MB, and its sequence is then too large to be stored with the positions of its frames: the video uploads, but fails to process on Mapillary. A video whose upload would be larger than MAX_VIDEO_UPLOAD_SIZE (32 GiB, MAPILLARY_TOOLS_MAX_VIDEO_UPLOAD_SIZE) is now uploaded in pieces of about equal length, as a video with gaps in its track is: each piece is cut from the video from the sync sample before its first point to the one after its last (transform_mp4_cut), with the CAMM track of its points, and is a sequence of its own. So is a part of a track between gaps whose piece would be larger. A piece that is still larger, such as where the bitrate is higher, is split again. Nothing is encoded again. The video between the last point of a piece and the first point of the next one may be in neither. A video that cannot be cut, or whose sync samples are so far apart that its pieces would upload a tenth more than it, is uploaded whole, as before, with a warning. A video file larger than the limit whose upload is not, as it leaves out the audio, is uploaded whole too. The upload history records each piece by its span, as for a video with gaps: an upload that stopped after some of the pieces uploads only the others the next time, with the same limit. At 32 GiB, the handle of a piece is at most about 370 kB, which leaves most of the room for the positions of its frames. --- docs/command_reference.md | 6 + mapillary_tools/constants.py | 8 + mapillary_tools/uploader.py | 276 ++++++++++++++++++++----- tests/unit/test_upload_video_pieces.py | 138 ++++++++++++- 4 files changed, 372 insertions(+), 56 deletions(-) diff --git a/docs/command_reference.md b/docs/command_reference.md index db367eda..cfb75f7a 100644 --- a/docs/command_reference.md +++ b/docs/command_reference.md @@ -442,6 +442,11 @@ Gotchas: - An import path that is gone, such as a file deleted or moved since `process`, is skipped with an error, and the files of the others are uploaded. The upload summary counts the import paths skipped. The command stops (exit code 3) only when none of them is found. +- A video whose upload would be larger than 32 GiB (`MAPILLARY_TOOLS_MAX_VIDEO_UPLOAD_SIZE`) is uploaded in pieces of + about equal length, one sequence for each, cut at its keyframes from its first GPS point to its last, without + re-encoding it; so is a part of its track between gaps (see [GPX and NMEA tracks](#gpx-and-nmea-tracks)). Mapillary + cannot process the upload of a video of about 100 GB. A video that cannot be cut, or whose keyframes are so far apart + that its pieces would upload a tenth more than it, is uploaded whole, with a warning. - Uploading a folder of zip files also needs a description file: pass the one of the zipped images with `--desc_path`. - `--delete_after_upload` deletes each image, video and zip file once Mapillary has confirmed that its upload @@ -654,6 +659,7 @@ in the user data folder (or `MAPILLARY_TOOLS_EXIFTOOL_INSTALL_DIR`) and prints t | `MAPILLARY_TOOLS_MAX_GPX_GAP_SECONDS` | 3600 | The longest gap in a GPX or NMEA track that files can be placed in, and how far from the rest of a track ExifTool points can be. | | `MAPILLARY_TOOLS_MAX_GPX_SEGMENT_GAP_SECONDS` | 5 | The longest time between two segments of a GPX track inside a video that `process` keeps no gap for. | | `MAPILLARY_UPLOAD_HISTORY_PATH` | in the user data folder | Where the upload history is kept. | +| `MAPILLARY_TOOLS_MAX_VIDEO_UPLOAD_SIZE` | `32G` | The largest upload of a video as one sequence: a larger one is uploaded in pieces (see [`upload`](#upload)). `INF` for no limit. | | `MAPILLARY_TOOLS_MAX_SEQUENCE_LENGTH` | 1000 | The most images in a sequence. | | `MAPILLARY_TOOLS_MAX_CAPTURE_SPEED_KMH` | 400 | The fastest average speed that passes the speed check. | | `MAPILLARY_TOOLS_IMAGE_DESCRIPTION_FILENAME` | `mapillary_image_description.json` | The default name of the description file. | diff --git a/mapillary_tools/constants.py b/mapillary_tools/constants.py index 21d5779e..bb190499 100644 --- a/mapillary_tools/constants.py +++ b/mapillary_tools/constants.py @@ -202,6 +202,14 @@ def _parse_scaled_integers( # but it affects the responsiveness of the upload progress bar UPLOAD_CHUNK_SIZE_MB: float = float(os.getenv(_ENV_PREFIX + "UPLOAD_CHUNK_SIZE_MB", 2)) MAX_UPLOAD_RETRIES: int = int(os.getenv(_ENV_PREFIX + "MAX_UPLOAD_RETRIES", 200)) +# The largest upload of a video as one sequence. A larger video, or part of +# its track between gaps, is uploaded in pieces of about equal length, each a +# sequence of its own (see uploader.VideoUploader). Mapillary keeps a handle +# of about 210 bytes for each 20 MB uploaded with the sequence, and cannot +# keep that of an upload of about 100 GB with the positions of its frames +MAX_VIDEO_UPLOAD_SIZE: int | None = _parse_filesize( + os.getenv(_ENV_PREFIX + "MAX_VIDEO_UPLOAD_SIZE", "32G") +) MAPILLARY__ENABLE_UPLOAD_HISTORY_FOR_DRY_RUN: bool = _yes_or_no( os.getenv("MAPILLARY__ENABLE_UPLOAD_HISTORY_FOR_DRY_RUN", "NO") ) diff --git a/mapillary_tools/uploader.py b/mapillary_tools/uploader.py index 034e2712..0630701a 100644 --- a/mapillary_tools/uploader.py +++ b/mapillary_tools/uploader.py @@ -32,6 +32,7 @@ else: from typing_extensions import Required +import humanize import requests from . import ( @@ -287,7 +288,7 @@ def upload_videos( ) if len(part) >= 2 ] - if 2 <= len(parts): + if 2 <= len(parts) or _larger_than_an_upload(video_metadata): yield from cls._upload_video_in_pieces( mly_uploader, video_metadata, parts, progress ) @@ -327,11 +328,13 @@ def _upload_video_in_pieces( progress: SequenceProgress, ) -> T.Generator[tuple[types.VideoMetadata, UploadResult], None, None]: """ - Upload a video in pieces, one for each part of its track between gaps - (see types.VideoMetadata.track_gaps), each a sequence of its own: the - video from the sync sample before the first point of the part to the + Upload a video in pieces, each a sequence of its own: one for each + part of its track between gaps (see types.VideoMetadata.track_gaps), + and pieces of about equal length of a part, or of a video without + gaps, that would upload more than constants.MAX_VIDEO_UPLOAD_SIZE. A + piece is the video from the sync sample before its first point to the one after its last (see simple_mp4_builder.transform_mp4_cut()), with - the CAMM track of the part. Each piece reads its own samples of the + the CAMM track of its points. Each piece reads its own samples of the video only, and the video in the gaps is not uploaded. Upload the video whole, as one sequence, if it cannot be cut, or if @@ -342,7 +345,6 @@ def _upload_video_in_pieces( pieces (see UploadResult). """ name = video_metadata.filename.name - keys = [_video_piece_key(part) for part in parts] # Yielded once the video is closed, so that it can be deleted once it # is uploaded (see upload.upload()), which no file open on Windows can last_result: UploadResult @@ -350,28 +352,31 @@ def _upload_video_in_pieces( try: camm_info = cls.prepare_camm_info(video_metadata) try: - cuts = cls._cut_pieces(src_fp, name, camm_info, parts) - if cuts is None: + pieces = cls._cut_pieces( + src_fp, name, camm_info, video_metadata.points, parts + ) + if pieces is None: whole = simple_mp4_builder.transform_mp4( src_fp, camm_builder.camm_sample_generator2(camm_info) ) except camm_builder.SampleDurationOverflowError as ex: raise GPSPointsTooFarApartError(f"{name}: {ex}") from ex - if cuts is None: + if pieces is None: last_result = UploadResult( result=cls._upload_and_finish(mly_uploader, whole, progress) ) except Exception as ex: last_result = UploadResult(error=ex) - cuts = None + pieces = None - if cuts is not None: + if pieces is not None: + keys = [_video_piece_key(part) for part, _ in pieces] LOG.info( - "%s: Uploading the video in %d pieces, one sequence for each part of its track between gaps", + "%s: Uploading the video in %d pieces, one sequence for each", name, - len(cuts), + len(pieces), ) - for piece_idx, cut in enumerate(cuts): + for piece_idx, (_, cut) in enumerate(pieces): piece_progress: SequenceProgress = { **progress, "sequence_md5sum": upload_key( @@ -389,7 +394,7 @@ def _upload_video_in_pieces( ) except Exception as ex: last_result.error = ex - if piece_idx + 1 < len(cuts): + if piece_idx + 1 < len(pieces): yield video_metadata, last_result yield video_metadata, last_result @@ -400,35 +405,46 @@ def _cut_pieces( src_fp: T.BinaryIO, name: str, camm_info: camm_parser.CAMMInfo, + points: T.Sequence[geo.Point], parts: T.Sequence[T.Sequence[geo.Point]], - ) -> list[simple_mp4_builder.MP4Cut] | None: + ) -> list[tuple[T.Sequence[geo.Point], simple_mp4_builder.MP4Cut]] | None: """ - Cut the video into the pieces of its parts, with the measurements of - **camm_info** within each, or return None if it cannot be cut, or if - the pieces would upload more of it than all of it. The video is then - uploaded whole, as it would be without gaps, also where cutting it - fails in a way that it should not + Cut the video into its pieces, each with its points and the + measurements of **camm_info** within them: one for each of its + **parts** between gaps, if there are 2 or more, and pieces of about + equal length of those, or of the whole track (**points**), that would + upload more than constants.MAX_VIDEO_UPLOAD_SIZE. + + Return None to upload the video whole, as one sequence: if its upload + is no larger than that and it has fewer than 2 parts, or if its sync + samples are so far apart that the pieces between the gaps would + upload more of it than all of it; or if it cannot be cut at the gaps, + also where cutting it fails in a way that it should not, or not into + smaller pieces """ - cuts: list[simple_mp4_builder.MP4Cut] = [] + max_size = constants.MAX_VIDEO_UPLOAD_SIZE + header: simple_mp4_builder.MP4Header + + def cut(part: T.Sequence[geo.Point]) -> simple_mp4_builder.MP4Cut: + start, end = part[0].time, part[-1].time + part_camm_info = _camm_info_between(camm_info, start, end) + + def sample_generator_factory(cut_start: float): + return camm_builder.camm_sample_generator2( + _shift_camm_info(part_camm_info, -cut_start) + ) + + return simple_mp4_builder.transform_mp4_cut( + src_fp, header, start, end, sample_generator_factory + ) + + pieces: list[tuple[T.Sequence[geo.Point], simple_mp4_builder.MP4Cut]] + pieces = [] try: header = simple_mp4_builder.MP4Header.read(src_fp) whole = simple_mp4_builder.transform_mp4_cut(src_fp, header, 0, math.inf) - for part in parts: - start, end = part[0].time, part[-1].time - part_camm_info = _camm_info_between(camm_info, start, end) - - def sample_generator_factory( - cut_start: float, part_camm_info=part_camm_info - ): - return camm_builder.camm_sample_generator2( - _shift_camm_info(part_camm_info, -cut_start) - ) - - cuts.append( - simple_mp4_builder.transform_mp4_cut( - src_fp, header, start, end, sample_generator_factory - ) - ) + if 2 <= len(parts): + pieces = [(part, cut(part)) for part in parts] except camm_builder.SampleDurationOverflowError: # The whole video has the points of the pieces raise @@ -436,25 +452,107 @@ def sample_generator_factory( LOG.warning( "%s: Uploading the video whole, as one sequence: it cannot be cut at the gaps of its track: %s", name, - ex - if isinstance(ex, simple_mp4_builder.MP4CutError) - else f"{type(ex).__name__}: {ex}", + _cut_error(ex), ) return None - # The pieces overlap where the sync samples of the video are further - # apart than the gaps are long - whole_duration = whole.end - whole.start - pieces_duration = sum(cut.end - cut.start for cut in cuts) - if whole_duration < pieces_duration: - LOG.warning( - "%s: Uploading the video whole, as one sequence: its sync samples are so far apart that its pieces between the gaps of its track would upload %.1f seconds of it, more than all of it (%.1f seconds)", - name, - pieces_duration, - whole_duration, - ) + if pieces: + # The pieces overlap where the sync samples of the video are + # further apart than the gaps are long + whole_duration = whole.end - whole.start + pieces_duration = sum(c.end - c.start for _, c in pieces) + if whole_duration < pieces_duration: + LOG.warning( + "%s: Uploading the video whole, as one sequence: its sync samples are so far apart that its pieces between the gaps of its track would upload %.1f seconds of it, more than all of it (%.1f seconds)", + name, + pieces_duration, + whole_duration, + ) + pieces = [] + + if max_size is None: + return pieces or None + if not pieces: + if _stream_size(whole.stream) <= max_size: + return None + track = sorted(points, key=lambda p: p.time) + if len(track) < 2: + return None + try: + pieces = [(track, cut(track))] + except camm_builder.SampleDurationOverflowError: + raise + except Exception as ex: + LOG.warning( + "%s: Uploading the video whole, as one sequence, although it is larger than %s: it cannot be cut: %s", + name, + humanize.naturalsize(max_size, binary=True), + _cut_error(ex), + ) + return None + + pieces = [ + smaller + for part, part_cut in pieces + for smaller in cls._split_piece(name, cut, part, part_cut, max_size) + ] + # The piece of the whole track, kept whole + if len(pieces) < 2: return None - return cuts + return pieces + + @classmethod + def _split_piece( + cls, + name: str, + cut: T.Callable[[T.Sequence[geo.Point]], simple_mp4_builder.MP4Cut], + part: T.Sequence[geo.Point], + part_cut: simple_mp4_builder.MP4Cut, + max_size: int, + ) -> list[tuple[T.Sequence[geo.Point], simple_mp4_builder.MP4Cut]]: + """ + Split the piece of a part of the track that would upload more than + **max_size** into pieces of about equal length that would not, each + cut with **cut**. Keep a piece whole, with a warning, where it cannot + be split, such as a part of 3 points or fewer, or where its sync + samples are so far apart that its pieces would upload a tenth more + than it, or more. The video between the last point of a piece and the + first of the next one may be in neither + """ + size = _stream_size(part_cut.stream) + if size <= max_size: + return [(part, part_cut)] + + smaller_parts = _split_part(part, math.ceil(size / max_size)) + if 2 <= len(smaller_parts): + try: + smaller_cuts = [cut(p) for p in smaller_parts] + except camm_builder.SampleDurationOverflowError: + raise + except Exception as ex: + reason = f"it cannot be cut: {_cut_error(ex)}" + else: + smaller_size = sum(_stream_size(c.stream) for c in smaller_cuts) + if smaller_size <= size * 1.1: + return [ + smaller + for p, c in zip(smaller_parts, smaller_cuts) + for smaller in cls._split_piece(name, cut, p, c, max_size) + ] + reason = f"its sync samples are so far apart that its pieces would upload {humanize.naturalsize(smaller_size, binary=True)}" + else: + reason = "its points cannot be split into pieces of 2 points or more" + + LOG.warning( + "%s: Uploading %s of the video, from %.1f to %.1f seconds, as one sequence, although it is larger than %s: %s", + name, + humanize.naturalsize(size, binary=True), + part[0].time, + part[-1].time, + humanize.naturalsize(max_size, binary=True), + reason, + ) + return [(part, part_cut)] @classmethod @contextmanager @@ -541,6 +639,78 @@ def prepare_camm_info( return camm_info +def _larger_than_an_upload(video_metadata: types.VideoMetadata) -> bool: + """ + Whether the video file is larger than constants.MAX_VIDEO_UPLOAD_SIZE, + so that its upload may need to be in pieces: its upload leaves out some + of its tracks, such as audio, and may be smaller + """ + max_size = constants.MAX_VIDEO_UPLOAD_SIZE + if max_size is None: + return False + try: + size = ( + utils.get_file_size(video_metadata.filename) + if video_metadata.filesize is None + else video_metadata.filesize + ) + except OSError: + # Fails where the video is uploaded whole + return False + return max_size < size + + +def _split_part(part: T.Sequence[geo.Point], count: int) -> list[T.Sequence[geo.Point]]: + """ + Split a part of a track (in time order) at its points into up to + **count** parts of about equal duration, each of 2 points or more + + >>> p = lambda t: geo.Point(time=t, lat=0, lon=0, alt=None, angle=None) + >>> [[q.time for q in s] for s in _split_part([p(t) for t in range(7)], 2)] + [[0, 1, 2], [3, 4, 5, 6]] + >>> [[q.time for q in s] for s in _split_part([p(t) for t in range(9)], 3)] + [[0, 1, 2], [3, 4, 5], [6, 7, 8]] + >>> [[q.time for q in s] for s in _split_part([p(0), p(1), p(2), p(9), p(10)], 3)] + [[0, 1, 2], [9, 10]] + >>> [[q.time for q in s] for s in _split_part([p(0), p(1), p(10)], 2)] + [[0, 1, 10]] + """ + start, end = part[0].time, part[-1].time + + def boundary(idx: int) -> float: + return start + (end - start) * idx / count + + smaller_parts: list[list[geo.Point]] = [[]] + boundary_idx = 1 + for point in part: + if ( + boundary_idx < count + and boundary(boundary_idx) <= point.time + and 2 <= len(smaller_parts[-1]) + ): + smaller_parts.append([]) + while boundary_idx < count and boundary(boundary_idx) <= point.time: + boundary_idx += 1 + smaller_parts[-1].append(point) + if 2 <= len(smaller_parts) and len(smaller_parts[-1]) < 2: + smaller_parts[-2].extend(smaller_parts.pop()) + return list(smaller_parts) + + +def _stream_size(stream: io.IOBase) -> int: + """The size of a stream, which is left at its start""" + size = stream.seek(0, io.SEEK_END) + stream.seek(0, io.SEEK_SET) + return size + + +def _cut_error(ex: Exception) -> str: + """Why a video cannot be cut, as a warning says it""" + if isinstance(ex, simple_mp4_builder.MP4CutError): + return str(ex) + return f"{type(ex).__name__}: {ex}" + + def _video_piece_key(part: T.Sequence[geo.Point]) -> str: """ The key of the piece of a video for a part of its track: its span, in diff --git a/tests/unit/test_upload_video_pieces.py b/tests/unit/test_upload_video_pieces.py index 12b2ad37..2f63c77b 100644 --- a/tests/unit/test_upload_video_pieces.py +++ b/tests/unit/test_upload_video_pieces.py @@ -10,6 +10,7 @@ from __future__ import annotations +import dataclasses import errno import io import typing as T @@ -94,7 +95,8 @@ def _uploaded(upload_dir: py.path.local) -> list[tuple[list[str], list[float]]]: data = path.read_bytes() moov = sample_parser.MovieBoxParser.parse_stream(io.BytesIO(data)) names = [ - name + # Without the padding of _LargeTrack + name.split(";")[0] for track in moov.extract_tracks() if track.is_video_track() for name in _sample_names(data, track) @@ -268,6 +270,108 @@ def test_whole_if_pieces_would_upload_more( assert "more than all of it" in caplog.text +@dataclasses.dataclass +class _LargeTrack(_Track): + """Samples of 10 kB, so that they make up most of an upload""" + + def sample_data(self, idx: int) -> bytes: + return super().sample_data(idx).ljust(10_000, b"x") + + +def _large_video(syncs=frozenset(range(0, 30, 5))) -> _Track: + """30 frames of 1 second, a sync sample every 5""" + return _LargeTrack("v", b"vide", 1000, [1000] * 30, set(syncs)) + + +class TestUploadInPiecesOfAtMostTheMaxSize: + def _metadata_without_gaps(self, video: Path) -> types.VideoMetadata: + return types.VideoMetadata( + video, types.FileType.VIDEO, points=_points(range(1, 29)) + ) + + def test_pieces_of_about_equal_length( + self, tmp_path: Path, setup_upload, monkeypatch + ): + video = _write_video(tmp_path / "video.mp4", _large_video()) + monkeypatch.setattr(constants, "MAX_VIDEO_UPLOAD_SIZE", 200_000) + + results, started = _upload_videos([self._metadata_without_gaps(video)]) + + assert [(r.result, r.error) for r in results] == [("0", None), ("0", None)] + assert [p["video_piece_keys"] for p in started] == [ + ["1000-14000", "15000-28000"] + ] * 2 + assert _uploaded(setup_upload) == [ + (_samples("v", 0, 15), [float(t) for t in range(1, 15)]), + (_samples("v", 15, 30), [float(t) for t in range(0, 14)]), + ] + assert all( + path.stat().st_size <= 200_000 for path in Path(setup_upload).glob("*.mp4") + ) + + def test_parts_between_gaps_in_smaller_pieces( + self, tmp_path: Path, setup_upload, monkeypatch + ): + video = _write_video(tmp_path / "video.mp4", _large_video()) + monkeypatch.setattr(constants, "MAX_VIDEO_UPLOAD_SIZE", 60_000) + + results, started = _upload_videos([_metadata(video)]) + + assert [r.error for r in results] == [None] * 4 + assert started[0]["video_piece_keys"] == [ + "1000-4000", + "5000-8000", + "21000-24000", + "25000-28000", + ] + assert [names for names, _ in _uploaded(setup_upload)] == [ + _samples("v", 0, 5), + _samples("v", 5, 10), + _samples("v", 20, 25), + _samples("v", 25, 30), + ] + + def test_whole_if_its_upload_is_no_larger( + self, tmp_path: Path, setup_upload, monkeypatch + ): + # The audio track makes the file larger, but is not uploaded + audio = _LargeTrack("a", b"soun", 48000, [48000] * 30, set(range(30))) + video = _write_video(tmp_path / "video.mp4", _large_video(), audio) + assert 400_000 < video.stat().st_size + monkeypatch.setattr(constants, "MAX_VIDEO_UPLOAD_SIZE", 400_000) + + results, started = _upload_videos([self._metadata_without_gaps(video)]) + + assert [(r.error, r.video_piece_idx) for r in results] == [(None, None)] + assert "video_piece_idx" not in started[0] + assert [names for names, _ in _uploaded(setup_upload)] == [_samples("v", 0, 30)] + + def test_whole_if_its_sync_samples_are_too_far_apart( + self, tmp_path: Path, setup_upload, monkeypatch, caplog + ): + # Each piece would start with the video + video = _write_video(tmp_path / "video.mp4", _large_video(syncs={0})) + monkeypatch.setattr(constants, "MAX_VIDEO_UPLOAD_SIZE", 200_000) + + results, started = _upload_videos([self._metadata_without_gaps(video)]) + + assert [(r.error, r.video_piece_idx) for r in results] == [(None, None)] + assert "video_piece_idx" not in started[0] + assert [names for names, _ in _uploaded(setup_upload)] == [_samples("v", 0, 30)] + assert "its sync samples are so far apart that its pieces" in caplog.text + + def test_not_in_pieces_without_a_max_size( + self, tmp_path: Path, setup_upload, monkeypatch + ): + video = _write_video(tmp_path / "video.mp4", _large_video()) + monkeypatch.setattr(constants, "MAX_VIDEO_UPLOAD_SIZE", None) + + results, started = _upload_videos([self._metadata_without_gaps(video)]) + + assert len(results) == 1 and results[0].error is None + assert "video_piece_idx" not in started[0] + + class TestUploadHistory: @pytest.fixture(autouse=True) def _setup(self, tmp_path: Path, monkeypatch, setup_upload): @@ -282,10 +386,22 @@ def _history(tmp_path: Path, monkeypatch) -> None: constants, "MAPILLARY__ENABLE_UPLOAD_HISTORY_FOR_DRY_RUN", True ) - def _upload(self, metadata: types.VideoMetadata, monkeypatch, fail_piece=None): - """Upload, failing to finish the piece of index fail_piece; return the pieces finished""" + def _upload( + self, + metadata: types.VideoMetadata, + monkeypatch, + fail_piece=None, + max_upload_size=None, + ): + """ + Upload, failing to finish the piece of index fail_piece, with + constants.MAX_VIDEO_UPLOAD_SIZE set to max_upload_size if given; + return the pieces finished + """ monkeypatch.undo() self._history(metadata.filename.parent.parent, monkeypatch) + if max_upload_size is not None: + monkeypatch.setattr(constants, "MAX_VIDEO_UPLOAD_SIZE", max_upload_size) finish_upload = uploader.Uploader.finish_upload finished: list[int] = [] @@ -328,6 +444,22 @@ def test_resumes_with_the_pieces_not_uploaded( assert self._upload(metadata, monkeypatch) == [1] assert self._video_recorded(metadata.filename) + def test_resumes_with_the_pieces_of_at_most_the_max_size_not_uploaded( + self, tmp_path: Path, monkeypatch + ): + video = _write_video(tmp_path / "videos" / "video.mp4", _large_video()) + metadata = types.VideoMetadata( + video, types.FileType.VIDEO, points=_points(range(1, 29)) + ) + + assert self._upload( + metadata, monkeypatch, fail_piece=1, max_upload_size=200_000 + ) == [0] + assert not self._video_recorded(metadata.filename) + + assert self._upload(metadata, monkeypatch, max_upload_size=200_000) == [1] + assert self._video_recorded(metadata.filename) + assert self._upload(metadata, monkeypatch) == [] def test_summary_counts_a_video_uploaded_in_pieces_once(