feat(music): pick the song container from the codec in the downloaded file

FFprobe sets the codec; DTS goes to .mka, PCM to .flac or .wav, AC-4/MPEG-H/DTS-UHD to MP4Box. The remux also cuts --sample, --no-mux no longer applies to a Song, and the tagger adds cover fallback, WebP, ARTISTS and ReplayGain. Song tag data now reaches a --remote client.
This commit is contained in:
imSp4rky
2026-11-14 19:58:21 -07:00
parent 703bcf872c
commit 7a5ad5024a
24 changed files with 785 additions and 235 deletions
+130 -5
View File
@@ -13,11 +13,13 @@ from __future__ import annotations
import shutil
import subprocess
from pathlib import Path
from typing import Optional
import mutagen
import pytest
from tests.core.test_music_tagger_roundtrip import make_song
from unshackle.core import binaries
from unshackle.core.music.tagger import write_music_metadata
from unshackle.core.tracks import Audio
@@ -79,7 +81,7 @@ def test_remux_states_the_full_length_in_the_codec_container(
track = Audio(id_="a1", url="https://example.test/a1.mp4", language="en", codec=codec)
track.path = source
assert track.to_music_container() is True
track.to_music_container()
assert track.path.suffix == extension
assert not source.exists()
assert header_length(track.path) == pytest.approx(6.0, abs=0.5)
@@ -118,14 +120,14 @@ def test_flac_is_re_encoded_because_a_copy_states_no_length(tmp_path: Path) -> N
assert mutagen.File(track.path).info.bits_per_sample == 16, "the re-encode must not change the sample format"
def test_dolby_digital_plus_falls_back_to_the_mp4_muxer(tmp_path: Path) -> None:
"""A .m4a name picks FFmpeg's ipod muxer, which refuses Dolby Digital Plus; the remux must not fail on it."""
def test_dolby_digital_plus_takes_the_mp4_muxer(tmp_path: Path) -> None:
"""FFmpeg's ipod muxer refuses Dolby Digital Plus, so it goes straight to the plain MP4 muxer."""
source = encode(tmp_path / "track.mp4", "eac3", "-ar", "48000")
track = Audio(id_="a1", url="https://example.test/a1.mp4", language="en", codec=Audio.Codec.EC3)
track.path = source
assert track.to_music_container() is True
track.to_music_container()
assert track.path.suffix == ".m4a"
assert header_length(track.path) == pytest.approx(6.0, abs=0.5)
assert write_music_metadata(track.path, make_song()).written
@@ -139,8 +141,131 @@ def test_a_failed_remux_keeps_the_source_and_removes_its_partial_file(tmp_path:
track = Audio(id_="a1", url="https://example.test/a1.mp4", language="en", codec=None)
track.path = source
with pytest.raises(subprocess.CalledProcessError):
with pytest.raises(RuntimeError):
track.to_music_container()
assert track.path == source and source.exists(), "a failed remux must keep the downloaded file"
assert not list(tmp_path.glob("track_music*")), "a failed remux must not leave a partial file"
def make_track(path: Path, codec: Optional[Audio.Codec]) -> Audio:
track = Audio(id_="a1", url="https://example.test/a1.mp4", language="en", codec=codec)
track.path = path
return track
@pytest.mark.parametrize(
("encoder", "fmt", "declared", "extension", "probed"),
[
("libopus", "webm", None, ".opus", Audio.Codec.OPUS),
("libvorbis", "webm", Audio.Codec.AAC, ".ogg", Audio.Codec.OGG),
("flac", "flac", Audio.Codec.AAC, ".flac", Audio.Codec.FLAC),
("libmp3lame", "mp3", None, ".mp3", Audio.Codec.MP3),
("pcm_s16le", "wav", None, ".flac", Audio.Codec.PCM),
("pcm_s24le", "wav", None, ".flac", Audio.Codec.PCM),
("pcm_f32le", "wav", None, ".wav", Audio.Codec.PCM),
],
)
def test_the_file_codec_decides_the_container(
tmp_path: Path, encoder: str, fmt: str, declared: Optional[Audio.Codec], extension: str, probed: Audio.Codec
) -> None:
"""Every track downloads under a .mp4 name, and a service can declare no codec or the wrong one."""
track = make_track(encode(tmp_path / "track.mp4", encoder, "-f", fmt), declared)
track.to_music_container()
assert track.path.suffix == extension
assert track.codec == probed
assert header_length(track.path) == pytest.approx(6.0, abs=0.5)
assert write_music_metadata(track.path, make_song()).written
def test_24_bit_pcm_stays_24_bit_in_flac(tmp_path: Path) -> None:
track = make_track(encode(tmp_path / "track.mp4", "pcm_s24le", "-f", "wav"), Audio.Codec.PCM)
track.to_music_container()
assert mutagen.File(track.path).info.bits_per_sample == 24
def test_dts_goes_to_matroska(tmp_path: Path) -> None:
"""FFmpeg writes DTS into MP4 with the wrong sample entry, so DTS takes Matroska."""
track = make_track(encode(tmp_path / "track.mp4", "dca", "-ar", "48000", "-f", "dts"), None)
track.to_music_container()
assert track.path.suffix == ".mka"
assert track.codec == Audio.Codec.DTS
def test_without_ffprobe_the_declared_codec_decides(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(binaries, "FFProbe", None)
track = make_track(fragmented_mp4(tmp_path / "track.mp4", "aac"), Audio.Codec.AAC)
track.to_music_container()
assert track.path.suffix == ".m4a"
@pytest.mark.parametrize(("encoder", "extra"), [("flac", ()), ("eac3", ("-ar", "48000"))])
def test_a_sample_window_is_cut_in_the_remux(tmp_path: Path, encoder: str, extra: tuple[str, ...]) -> None:
"""The cut runs in the same step, so a FLAC header states the cut length and EC-3 takes the MP4 muxer."""
source = encode(tmp_path / "track.mp4", encoder, *extra)
track = make_track(source, None)
track.to_music_container(start=1.0, duration=2.0)
assert header_length(track.path) == pytest.approx(2.0, abs=0.3)
def test_the_remux_writes_no_encoder_tag(tmp_path: Path) -> None:
track = make_track(encode(tmp_path / "track.mp4", "libmp3lame", "-f", "mp3"), Audio.Codec.MP3)
track.to_music_container()
tags = mutagen.File(track.path).tags
assert not tags or "TSSE" not in tags
def test_float_pcm_goes_to_wav_whatever_its_bit_depth(tmp_path: Path) -> None:
"""16- and 24-bit float PCM report a bit depth FLAC can hold, but FLAC has no float samples."""
track = make_track(encode(tmp_path / "track.mp4", "pcm_f32le", "-f", "wav"), Audio.Codec.PCM)
stream = {"codec_name": "pcm_f24le", "sample_fmt": "flt", "bits_per_raw_sample": "24"}
output = track.ffmpeg_music_container(track.path, stream, 0.0, None)
assert output.suffix == ".wav"
assert mutagen.File(output).info.bits_per_sample == 32
def test_a_sample_window_after_the_end_keeps_the_download(tmp_path: Path) -> None:
source = encode(tmp_path / "track.mp4", "flac")
track = make_track(source, None)
with pytest.raises(ValueError):
track.to_music_container(start=60.0, duration=30.0)
assert track.path == source and source.exists()
assert not list(tmp_path.glob("track_music*"))
@pytest.mark.skipif(binaries.MP4Box is None, reason="requires MP4Box")
def test_mp4box_writes_a_cut_m4a_and_leaves_no_temp_file(tmp_path: Path) -> None:
track = make_track(fragmented_mp4(tmp_path / "track.mp4", "aac"), Audio.Codec.AAC)
output = track.mp4box_music_container(track.path, 1.0, 2.0)
assert output.suffix == ".m4a"
assert header_length(output) == pytest.approx(2.0, abs=0.3)
assert sorted(p.name for p in tmp_path.iterdir()) == ["track.mp4", "track_music.m4a"]
def test_mp4box_route_removes_its_temp_file_when_preparation_fails(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
def strip(source: Path, output: Path) -> bool:
output.write_bytes(b"init")
return True
def fail(path: Path) -> None:
raise OSError("disk full")
monkeypatch.setattr(binaries, "MP4Box", Path("MP4Box"))
monkeypatch.setattr("unshackle.core.tracks.track.strip_duplicate_init_boxes", strip)
monkeypatch.setattr("unshackle.core.tracks.track.fix_data_reference_indexes", fail)
track = make_track(fragmented_mp4(tmp_path / "track.mp4", "aac"), Audio.Codec.AC4)
with pytest.raises(OSError):
track.mp4box_music_container(track.path, 0.0, None)
assert sorted(p.name for p in tmp_path.iterdir()) == ["track.mp4"]
+79
View File
@@ -16,10 +16,12 @@ import pytest
from mutagen.flac import FLAC
from mutagen.mp3 import MP3
from mutagen.mp4 import MP4
from mutagen.oggopus import OggOpus
from mutagen.oggvorbis import OggVorbis
from unshackle.core.music.tagger import write_music_metadata
from unshackle.core.titles.music import Song
from unshackle.core.tracks import Attachment
pytestmark = pytest.mark.skipif(shutil.which("ffmpeg") is None, reason="requires ffmpeg")
@@ -196,3 +198,80 @@ def test_a_nested_metadata_dict_reaches_the_tags(flac_path: Path) -> None:
assert audio["COMPOSER"] == ["Example Composer"]
assert audio["RELEASEDATE"] == ["2020-01-02"]
assert audio["DATE"] == ["2020-01-02"], "a full release date must beat the bare year"
def test_an_image_attachment_is_the_cover_when_the_song_gives_no_artwork_url(flac_path: Path, tmp_path: Path) -> None:
image = tmp_path / "thumbnail.png"
image.write_bytes(PNG_STUB)
song = make_song()
song.artwork_url = None
song.tracks.attachments.append(Attachment(path=image, name="thumbnail", mime_type="image/png"))
assert write_music_metadata(flac_path, song).artwork_embedded
assert FLAC(flac_path).pictures[0].data == PNG_STUB
def test_a_webp_cover_is_embedded_as_jpeg(tmp_path: Path) -> None:
webp = tmp_path / "cover.webp"
subprocess.run(
["ffmpeg", "-nostdin", "-v", "error", "-f", "lavfi", "-i", "color=red:s=16x16", "-frames:v", "1", str(webp)],
check=True,
)
m4a = tmp_path / "track.m4a"
subprocess.run(
["ffmpeg", "-nostdin", "-v", "error", "-f", "lavfi", "-i", "anullsrc", "-t", "0.2", "-c:a", "aac", str(m4a)],
check=True,
)
write_music_metadata(m4a, make_song(), session=FakeSession(webp.read_bytes()))
cover = MP4(m4a)["covr"][0]
assert bytes(cover).startswith(b"\xff\xd8\xff")
def test_a_cover_over_16_mib_is_skipped(flac_path: Path, caplog: pytest.LogCaptureFixture) -> None:
result = write_music_metadata(flac_path, make_song(), session=FakeSession(PNG_STUB + b"\x00" * 2**24))
assert not result.artwork_embedded
assert not FLAC(flac_path).pictures
assert "16 MiB" in caplog.text
def test_artists_and_replaygain_read_back(flac_path: Path) -> None:
song = make_song()
song.data = {"artists": ["Example Artist", "Guest Artist"], "replaygain_track_gain": -7.25, "quality": "Hi-Res"}
write_music_metadata(flac_path, song)
audio = FLAC(flac_path)
assert audio["ARTISTS"] == ["Example Artist", "Guest Artist"]
assert audio["REPLAYGAIN_TRACK_GAIN"] == ["-7.25 dB"]
assert "COMMENT" not in audio, "a quality label is display text, not a comment"
def test_a_replaygain_value_that_is_not_a_number_is_skipped(flac_path: Path) -> None:
song = make_song()
song.data = {"replaygain_track_gain": "nan", "replaygain_track_peak": "inf", "replaygain_album_gain": -3}
write_music_metadata(flac_path, song)
audio = FLAC(flac_path)
assert "REPLAYGAIN_TRACK_GAIN" not in audio and "REPLAYGAIN_TRACK_PEAK" not in audio
assert audio["REPLAYGAIN_ALBUM_GAIN"] == ["-3.00 dB"]
def test_opus_gets_no_replaygain(tmp_path: Path) -> None:
"""RFC 7845 section 5.2.1: an Opus file should not carry REPLAYGAIN_* tags."""
path = tmp_path / "04. Example Track.opus"
subprocess.run(
["ffmpeg", "-nostdin", "-v", "error", "-f", "lavfi", "-i", "anullsrc=r=48000:cl=mono", "-t", "0.2", str(path)],
check=True,
)
song = make_song()
song.data = {"replaygain_track_gain": -7.25}
write_music_metadata(path, song)
audio = OggOpus(path)
assert audio["TITLE"] == ["Example Track"]
assert not [key for key in audio.keys() if key.upper().startswith("REPLAYGAIN_")]
+21
View File
@@ -118,3 +118,24 @@ def test_a_movie_or_episode_still_takes_the_mkv_path(calls: Calls, title: Any) -
assert calls.music == [], "write_music_metadata must not see a Matroska file"
assert [call[0] for call in calls.mkv] == [path]
def test_a_dts_song_in_matroska_takes_mkvpropedit(calls: Calls) -> None:
path = Path("04. Example Track.mka")
tags_module.tag_file(path, make_song(), session=SESSION)
assert calls.music == []
assert calls.mkv and calls.mkv[0][1]["TITLE"] == "Example Track"
def test_a_matroska_tagger_fault_warns_and_never_raises(
calls: Calls, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
def broken_apply_tags(path: Path, tags: dict[str, str]) -> None:
raise OSError("mkvpropedit not found")
monkeypatch.setattr(tags_module, "apply_tags", broken_apply_tags)
with caplog.at_level("WARNING", logger="TAGS"):
tags_module.tag_file(Path("04. Example Track.mka"), make_song(), session=SESSION)
assert "mkvpropedit not found" in caplog.text
+44 -2
View File
@@ -20,9 +20,10 @@ from pymediainfo import MediaInfo
from tests.core.test_music_container import fragmented_mp4, header_length
from tests.core.test_music_tagger_roundtrip import make_song
from unshackle.commands.dl import remux_music_tracks
from unshackle.commands.dl import mux_skipped, no_mux_extension, remux_music_tracks
from unshackle.core.config import config
from unshackle.core.tracks import Audio, Tracks
from unshackle.core.titles.movie import Movie
from unshackle.core.tracks import Audio, Tracks, Video
from unshackle.core.utils import tags
pytestmark = pytest.mark.skipif(shutil.which("ffmpeg") is None, reason="requires ffmpeg")
@@ -120,3 +121,44 @@ def test_a_fragmented_flac_song_ends_playable_named_and_tagged(tmp_path: Path, d
audio = FLAC(final_path)
assert audio["TITLE"] == ["Example Track"]
assert audio["ARTIST"] == ["Example Artist"]
def test_the_hook_cuts_a_song_to_the_sample_window(tmp_path: Path) -> None:
song = make_song()
track = make_audio(fragmented_mp4(tmp_path / "Audio_a1.mp4", "flac"), Audio.Codec.FLAC, "a1")
track.sample_start = 10.0
song.tracks = Tracks([track])
remux_music_tracks(song, log, sample=(11, 13))
assert header_length(track.path) == pytest.approx(2.0, abs=0.3)
def test_an_mp3_song_is_named_mp3(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""MediaInfo calls MP3 'MPEG Audio', which named a file …MPEG.Audio2.0.mp3."""
monkeypatch.setattr(config, "output_template", {"songs": "{track_number}.{title}.{audio}"})
song = make_song()
track = make_audio(fragmented_mp4(tmp_path / "Audio_a1.mp4", "libmp3lame"), Audio.Codec.MP3, "a1")
song.tracks = Tracks([track])
remux_music_tracks(song, log)
assert song.get_filename(MediaInfo.parse(track.path), show_service=False).endswith(".MP3")
def test_no_mux_never_skips_the_song_path() -> None:
"""A --no-mux Song must still be renamed, tagged and get its .lrc, like any other Song."""
movie = Movie(id_="movie-0001", service=type("EXAMPLE", (), {}), name="Some Movie", year=2024)
assert mux_skipped(True, make_song()) is False
assert mux_skipped(True, movie) is True
assert mux_skipped(False, movie) is False
def test_a_no_mux_audio_track_in_mpeg_4_is_saved_as_m4a(tmp_path: Path) -> None:
path = fragmented_mp4(tmp_path / "Audio_a1.mp4", "aac")
media_info = MediaInfo.parse(path)
video = Video(id_="v1", url="https://example.test/v1.mp4", language="en", codec=Video.Codec.AVC)
assert no_mux_extension(make_audio(path, Audio.Codec.AAC, "a1"), path, media_info) == ".m4a"
assert no_mux_extension(video, path, media_info) == ".mp4"
@@ -20,6 +20,7 @@ from unshackle.core.api.handlers import serialize_title, stamp_service_flags
from unshackle.core.remote_service import build_title
from unshackle.core.titles.episode import Episode
from unshackle.core.titles.movie import Movie
from unshackle.core.titles.music import Song
pytestmark = pytest.mark.unit
@@ -213,3 +214,46 @@ def test_title_to_meta_round_trip_keeps_the_movie_anime_flag() -> None:
movie.anime = True
rebuilt = build_title(dl.title_to_meta(movie), "FAKE", "fallback-id")
assert rebuilt.anime is True
def test_song_tag_data_survives_the_remote_round_trip() -> None:
song = Song(
id_="song-0001",
service=_FakeSvc,
name="Track 1",
artist="A",
album="B",
track=1,
data={
"release_date": "2020-01-02",
"composer": "C",
"track_id": "123",
"hires": True,
"atmos": True,
"unrelated": object(),
},
)
rebuilt = build_title(json.loads(json.dumps(serialize_title(song))), "EXAMPLE", "song-0001")
assert rebuilt.data["release_date"] == "2020-01-02"
assert rebuilt.data["composer"] == "C"
assert rebuilt.data["track_id"] == "123"
assert rebuilt.data["hires"] is True and rebuilt.data["atmos"] is True
assert "unrelated" not in rebuilt.data
@pytest.mark.parametrize(
"music_data",
[["not", "a", "dict"], {"metadata": {"title": "Replaced"}, "composer": "C"}],
ids=["not-a-dict", "unknown-key"],
)
def test_the_client_keeps_only_known_music_data_keys(music_data: Any) -> None:
song = Song(id_="song-0001", service=_FakeSvc, name="Track 1", artist="A", album="B", track=1)
info = json.loads(json.dumps(serialize_title(song)))
info["music_data"] = music_data
rebuilt = build_title(info, "EXAMPLE", "song-0001")
assert "metadata" not in (rebuilt.data or {})
assert rebuilt.name == "Track 1"