feat(hls): carry a held media playlist over --remote and export, inflate gzip segments, add own_stream_only mux opt-in

This commit is contained in:
imSp4rky
2026-12-02 11:12:02 -07:00
parent ccdd14c845
commit 9842a61d3f
27 changed files with 404 additions and 62 deletions
+2
View File
@@ -201,6 +201,8 @@ def playlist_track(text: str, init_data: bytes = b"") -> Video:
track.url = "https://example.test/video.m3u8"
track.descriptor = Video.Descriptor.HLS
track.drm = None
track.data = {}
track.from_file = None
track.needs_drm_loading = True
track.session = RouteSession({"video.m3u8": text.encode(), "init.mp4": init_data})
return track
+3 -1
View File
@@ -169,7 +169,9 @@ def test_first_fragment_skips_the_header_boxes_of_a_one_file_track(monkeypatch:
def test_first_segment_of_an_hls_playlist_keeps_its_byte_offset() -> None:
playlist = '#EXTM3U\n#EXT-X-MAP:URI="init.mp4"\n#EXTINF:6.0,\n#EXT-X-BYTERANGE:5000@1200\nmedia.mp4\n'
session = SimpleNamespace(get=lambda url, timeout: SimpleNamespace(status_code=200, text=playlist))
track = SimpleNamespace(url="https://cdn/v/index.m3u8", data={}, descriptor=Video.Descriptor.HLS, from_file=None)
track = SimpleNamespace(
url="https://cdn/v/index.m3u8", data={}, descriptor=Video.Descriptor.HLS, held_playlist=lambda: None
)
assert hdr10plus.first_segment(track, session) == ("https://cdn/v/media.mp4", 1200) # type: ignore[arg-type]
+5 -1
View File
@@ -2,6 +2,7 @@
from __future__ import annotations
from functools import partial
from pathlib import Path
from types import SimpleNamespace
from typing import Any
@@ -128,5 +129,8 @@ def test_import_reads_the_kid_of_a_playlist_whose_key_has_none() -> None:
def get(url: str, headers: Any = None) -> Any:
return SimpleNamespace(text=playlist(keyed_segment(NO_KID_PSSH)), ok=True, content=b"init")
track = SimpleNamespace(id="vid", url="https://example.test/media.m3u8", get_key_id=lambda init: INIT_KID)
track = SimpleNamespace(
id="vid", url="https://example.test/media.m3u8", get_key_id=lambda init: INIT_KID, held_playlist=lambda: None
)
track.playlist_text = partial(Track.playlist_text, track)
assert hls_playlist_kids(track, SimpleNamespace(get=get)) == {INIT_KID}
+1 -1
View File
@@ -680,7 +680,7 @@ def test_the_server_takes_the_same_key_id_of_a_stitched_playlist(
assert track.drm is not None
assert [drm.kids for drm in track.drm] == [[KID], [KID]]
assert session.requests == ["media.m3u8", *requests]
assert session.requests == requests
@pytest.mark.parametrize(
+2
View File
@@ -63,6 +63,8 @@ def make_track(res: requests.Response) -> Video:
track.url = "https://example.com/video/720.m3u8"
track.session = FakeSession(res)
track.drm = None
track.data = {}
track.from_file = None
track.needs_drm_loading = True
return track
+26
View File
@@ -0,0 +1,26 @@
"""A segment whose body is gzip with no Content-Encoding header is inflated before the merge; no other body is."""
from __future__ import annotations
import gzip
import pytest
from tests.core.test_rolling_merge_hls_ism import INIT, PARTS, hls_track, make_ctx, no_cancel, server # noqa: F401
from unshackle.core.manifests import HLS
@pytest.mark.parametrize(
"body, expected",
[
pytest.param(gzip.compress, lambda part: part, id="gzip"),
pytest.param(lambda part: b"\x1f\x8b\x08" + part, lambda part: b"\x1f\x8b\x08" + part, id="gzip-magic-only"),
pytest.param(lambda part: gzip.compress(part)[:-4], lambda part: gzip.compress(part)[:-4], id="cut-gzip"),
],
)
def test_only_a_whole_gzip_segment_is_inflated(body, expected, server, tmp_path): # noqa: F811
for i, part in enumerate(PARTS):
server.routes[f"/part{i}.m4s"] = body(part)
ctx = make_ctx(tmp_path, "out.mp4")
HLS.download_track(hls_track(server, False), ctx)
assert ctx.save_path.read_bytes() == INIT + b"".join(map(expected, PARTS))
+17
View File
@@ -286,6 +286,23 @@ def test_mp4_names_skip_the_extra_tracks_of_a_3d_video_file(tmp_path: Path, mp4b
assert command[command.index("-name") + 1] == "3=English"
def test_an_mp4_mux_adds_only_the_own_stream_of_each_file(tmp_path: Path, mp4box_commands: list[list[str]]) -> None:
video = stereo_video(tmp_path, stereo=False)
video.data["own_stream_only"] = True
video.path.write_bytes(box(b"moov", box(b"trak") + box(b"trak")))
audio = Audio(url="https://x/a.mp4", language="en", codec=Audio.Codec.EC3, name="English")
audio.path = tmp_path / "a.mp4"
audio.path.write_bytes(b"\x00" * 16)
audio.data["own_stream_only"] = True
source = video.path
Tracks([video, audio])._mux_mp4(title="Film", output_path=tmp_path / "out.mkv")
command = mp4box_commands[0]
adds = [command[i + 1] for i, arg in enumerate(command) if arg == "-add"]
assert adds[0].startswith(f"{source}#video:")
assert "#audio:" in adds[1]
assert command[command.index("-name") + 1] == "2=English"
def test_a_3d_mux_that_loses_its_second_view_warns(
tmp_path: Path, mp4box_commands: list[list[str]], caplog: pytest.LogCaptureFixture
) -> None: