fix(import): keep the values a service set on exported tracks and export a song as a song

This commit is contained in:
imSp4rky
2026-12-02 17:19:10 -07:00
parent 95d36378f4
commit 0c93f1dc63
6 changed files with 122 additions and 18 deletions
+16 -1
View File
@@ -22,7 +22,7 @@ from unshackle.core.drm.clearkey import ClearKey
from unshackle.core.drm.clearkey_cenc import ClearKeyCENC
from unshackle.core.export_writer import ExportWriter
from unshackle.core.import_service import ImportService
from unshackle.core.titles import Movie
from unshackle.core.titles import Album, Movie, Song
from unshackle.core.tracks import Audio, Chapter, Subtitle, Video
from unshackle.core.wire import drm_from_dict
@@ -570,3 +570,18 @@ def test_a_title_with_no_record_logs_that_nothing_was_exported(
writer.log_title(second)
assert caplog.messages == ["Exported nothing for 'Example Movie' (2024)"]
def test_a_song_stays_a_song_of_an_album_through_export_and_import(tmp_path: Path) -> None:
writer = make_writer(tmp_path)
title = Song(
id_="song-1", service=StubService, name="Example Song", artist="Example Artist", album="Example Album", track=4
)
title.tracks.add(make_title().tracks.audio[0])
writer.record(title, title.tracks.audio[0])
titles = ImportService(import_ctx(), "EXAMPLE", "song-1", str(written(writer))).get_titles()
assert isinstance(titles, Album)
assert [(type(t).__name__, t.artist, t.album, t.track) for t in titles] == [
("Song", "Example Artist", "Example Album", 4)
]
+44 -1
View File
@@ -21,7 +21,8 @@ import requests
from unshackle.core.import_service import ImportService
from unshackle.core.manifests import DASH
from unshackle.core.tracks import Audio, Subtitle, Video
from unshackle.core.tracks import Audio, Subtitle, Tracks, Video
from unshackle.core.wire import track_to_dict
MPD_TEMPLATE = """<?xml version="1.0"?>
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" type="static" mediaPresentationDuration="PT10M">
@@ -300,3 +301,45 @@ def test_a_file_row_can_name_its_codec_and_range_by_name(tmp_path: Path) -> None
def test_a_file_row_marked_stereo_imports_as_a_3d_video(tmp_path: Path) -> None:
service = file_export(tmp_path, [{**FILE_ROWS[0], "stereo": True}])
assert service.get_tracks(next(iter(service.get_titles()))).videos[0].stereo
MPD_SERVICE_VALUES = """<?xml version="1.0"?>
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" type="static" mediaPresentationDuration="PT10M">
<Period id="1">
<AdaptationSet contentType="audio" mimeType="audio/mp4" lang="en">
<Representation id="a1" codecs="ec-3" bandwidth="640000" audioSamplingRate="48000">
<BaseURL>a.mp4</BaseURL><SegmentBase indexRange="0-1"/>
</Representation>
</AdaptationSet>
<AdaptationSet contentType="text" mimeType="application/mp4" lang="en">
<Representation id="s1" codecs="stpp" bandwidth="1000"><BaseURL>s.mp4</BaseURL><SegmentBase indexRange="0-1"/></Representation>
</AdaptationSet>
</Period>
</MPD>
"""
SERVICE_VALUES = {"atmos": True, "bit_depth": 24, "sample_rate": 96000, "note": "EXAMPLE_TIER", "name": "Example Mix"}
def import_with(tmp_path: Path, kind: str, drop: str = "", **values: Any) -> Tracks:
"""Import the manifest with the export dict of its ``kind`` track, given the values a service set after the parse."""
parsed = getattr(
DASH.from_text(MPD_SERVICE_VALUES, "https://example.invalid/m.mpd").to_tracks(language="en"), kind
)[0]
exported = {k: v for k, v in json.loads(json.dumps(track_to_dict(parsed))).items() if k != drop}
return get_tracks(make_service(tmp_path, "en", {exported["id"]: {**exported, **values}}), MPD_SERVICE_VALUES)
def test_the_values_a_service_set_on_a_track_survive_the_reparse(tmp_path: Path) -> None:
audio = import_with(tmp_path, "audio", **SERVICE_VALUES).audio
assert [(t.atmos, t.bit_depth, t.sample_rate, t.note, t.name) for t in audio] == [tuple(SERVICE_VALUES.values())]
assert audio[0].data.get("dash") is not None
def test_a_value_an_older_export_does_not_record_keeps_the_manifest_value(tmp_path: Path) -> None:
assert [t.sample_rate for t in import_with(tmp_path, "audio", drop="sample_rate").audio] == [48000]
def test_a_subtitle_keeps_the_manifest_codec_its_download_converted(tmp_path: Path) -> None:
subtitles = import_with(tmp_path, "subtitles", codec="TimedTextMarkupLang", sdh=True).subtitles
assert [(t.codec, t.sdh) for t in subtitles] == [(Subtitle.Codec.fTTML, True)]
+9 -4
View File
@@ -758,14 +758,19 @@ def test_export_movie_meta() -> None:
def test_export_meta_of_another_title_is_a_movie_named_by_str() -> None:
assert title_to_meta(song()) == {
"id": "song-0001",
"language": "en",
assert title_to_meta(OtherTitle()) == {
"id": "other-0001",
"language": None,
"type": "movie",
"name": "Example Artist - Example Album (2022) / 03. Example Song",
"name": "Other Thing",
}
def test_export_song_meta_rebuilds_the_song_the_rest_form_rebuilds() -> None:
rebuilt = title_fields(build_title(json_copy(title_to_meta(song())), "EXAMPLE", "fallback-id"))
assert rebuilt == title_fields(build_title(json_copy(REST_SONG), "EXAMPLE", "fallback-id"))
def test_build_title_from_rest_episode() -> None:
assert title_fields(build_title(json_copy(REST_EPISODE), "EXAMPLE", "fallback-id")) == EPISODE_FIELDS