feat: 合成 MP4 时带上独立音频并支持按剧重新下载
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
import json
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
from server.services.downloads import remux_tree
|
||||
from server.services.playlist import (
|
||||
audio_download_candidates,
|
||||
audio_rendition_uris,
|
||||
guess_audio_playlist_urls,
|
||||
localize_media,
|
||||
)
|
||||
|
||||
|
||||
def test_guess_and_saved_playlist() -> None:
|
||||
segment = "https://vz-example.b-cdn.net/bcdn_token=abc&token_path=%2Fuuid%2F&expires=123/uuid/1080p/video0.m4s"
|
||||
guessed = guess_audio_playlist_urls(segment)
|
||||
assert guessed[0].endswith("/uuid/audio/audio.m3u8"), guessed
|
||||
assert guessed[1].endswith("/1080p/audio.m3u8"), guessed
|
||||
|
||||
master = """#EXTM3U
|
||||
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio",NAME="audio",DEFAULT=YES,AUTOSELECT=YES,URI="audio/audio.m3u8"
|
||||
#EXT-X-STREAM-INF:BANDWIDTH=1,CODECS="avc1.640028,mp4a.40.2",AUDIO="audio"
|
||||
video.m3u8
|
||||
"""
|
||||
assert audio_rendition_uris(master) == ["audio/audio.m3u8"]
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
source = Path(tmp)
|
||||
(source / "playlist.m3u8").write_text(master, encoding="utf-8")
|
||||
(source / "segments.json").write_text(
|
||||
json.dumps([{"name": "video0.m4s", "url": segment}]),
|
||||
encoding="utf-8",
|
||||
)
|
||||
found = audio_download_candidates(source)
|
||||
assert found[0].endswith("/uuid/audio/audio.m3u8"), found
|
||||
assert any(item.endswith("/1080p/audio.m3u8") for item in found), found
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
source = Path(tmp)
|
||||
(source / "playlist.m3u8").write_text(master, encoding="utf-8")
|
||||
assert audio_download_candidates(source) == []
|
||||
|
||||
absolute = '#EXTM3U\n#EXT-X-MEDIA:TYPE=AUDIO,URI="https://cdn.example/audio/audio.m3u8"\nvideo.m3u8\n'
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
source = Path(tmp)
|
||||
(source / "playlist.m3u8").write_text(absolute, encoding="utf-8")
|
||||
assert audio_download_candidates(source) == ["https://cdn.example/audio/audio.m3u8"]
|
||||
|
||||
localized, items = localize_media(
|
||||
'#EXTM3U\n#EXT-X-MAP:URI="init.mp4"\nseg0.m4s\n',
|
||||
"https://cdn.example/audio/audio.m3u8",
|
||||
name_prefix="audio-",
|
||||
)
|
||||
assert 'URI="audio-init.mp4"' in localized, localized
|
||||
assert items[0]["name"] == "audio-init.mp4"
|
||||
assert items[1]["name"] == "audio-seg0.m4s"
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
try:
|
||||
remux_tree(Path(tmp))
|
||||
except RuntimeError as exc:
|
||||
assert "没有找到" in str(exc), exc
|
||||
else:
|
||||
raise AssertionError("empty directory should fail")
|
||||
|
||||
|
||||
def test_seed_master_from_saved_address() -> None:
|
||||
from server.services import playlist as playlists
|
||||
from server.services.downloads import _seed_master_urls
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
folder = Path(tmp)
|
||||
first = folder / "source" / "EP.1"
|
||||
second = folder / "source" / "EP.2"
|
||||
first.mkdir(parents=True)
|
||||
second.mkdir()
|
||||
(second / "master.url").write_text("https://keep.example/old.m3u8\n", encoding="utf-8")
|
||||
_seed_master_urls(
|
||||
"9",
|
||||
folder,
|
||||
[
|
||||
{"cid": 1, "idx": 1, "title": "EP.1", "src": "https://cdn.example/a.m3u8"},
|
||||
{"cid": 2, "idx": 2, "title": "EP.2", "src": "https://cdn.example/b.m3u8"},
|
||||
{"cid": 3, "idx": 3, "title": "EP.3", "src": ""},
|
||||
],
|
||||
)
|
||||
assert (first / "master.url").read_text(encoding="utf-8").strip() == "https://cdn.example/a.m3u8"
|
||||
assert (second / "master.url").read_text(encoding="utf-8").strip() == "https://keep.example/old.m3u8"
|
||||
|
||||
third = folder / "source" / "EP.3"
|
||||
third.mkdir()
|
||||
original = playlists.cached_episode_src
|
||||
playlists.cached_episode_src = lambda vid, cid: "https://cache.example/m.m3u8" if int(cid) == 3 else ""
|
||||
try:
|
||||
_seed_master_urls("9", folder, [{"cid": 3, "idx": 3, "title": "EP.3", "src": ""}])
|
||||
finally:
|
||||
playlists.cached_episode_src = original
|
||||
assert (third / "master.url").read_text(encoding="utf-8").strip() == "https://cache.example/m.m3u8"
|
||||
|
||||
|
||||
def test_finished_episode_is_kept_until_redownload() -> None:
|
||||
from server.services.downloads import _keep_finished_episode
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
mp4 = Path(tmp) / "episode.mp4"
|
||||
mp4.write_bytes(b"mp4")
|
||||
assert _keep_finished_episode("done", mp4)
|
||||
assert not _keep_finished_episode("pending", mp4)
|
||||
assert not _keep_finished_episode("done", Path(tmp) / "missing.mp4")
|
||||
|
||||
|
||||
def test_cache_keeps_address_when_vip_returns_empty() -> None:
|
||||
from server.services.playlist import merge_episodes
|
||||
|
||||
cached = [{"cid": 1, "idx": 1, "title": "EP.1", "src": "https://cdn.example/a.m3u8"}]
|
||||
fresh = [{"cid": 1, "idx": 1, "title": "EP.1", "src": ""}]
|
||||
merged, save = merge_episodes(cached, fresh)
|
||||
assert merged[0]["src"] == "https://cdn.example/a.m3u8"
|
||||
assert save is False
|
||||
merged, _save = merge_episodes(cached, [])
|
||||
assert merged[0]["src"] == "https://cdn.example/a.m3u8"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_guess_and_saved_playlist()
|
||||
test_seed_master_from_saved_address()
|
||||
test_finished_episode_is_kept_until_redownload()
|
||||
test_cache_keeps_address_when_vip_returns_empty()
|
||||
print("ok")
|
||||
Reference in New Issue
Block a user