74 lines
3.3 KiB
Python
74 lines
3.3 KiB
Python
from __future__ import annotations
|
|
|
|
import unittest
|
|
from pathlib import Path
|
|
from tempfile import TemporaryDirectory
|
|
|
|
from tools.audio_batch import (
|
|
AudioInput,
|
|
AudioSettings,
|
|
AudioStream,
|
|
_action_for,
|
|
matching_actions,
|
|
same_container,
|
|
unique_audio_target,
|
|
universal_containers,
|
|
)
|
|
from tools.audio_settings import parse_bitrate, resolve_output_dir
|
|
|
|
|
|
class AudioBatchTests(unittest.TestCase):
|
|
def audio_input(self, name: str, container: str, codec: str, *, video: bool = False) -> AudioInput:
|
|
return AudioInput(Path(name), container, [AudioStream(0, codec, 2, 48000, 192000)], video)
|
|
|
|
def test_m4a_is_available_only_for_supported_audio_codecs(self) -> None:
|
|
self.assertEqual(universal_containers({"aac", "opus", "flac"}), ["MKA", "M4A"])
|
|
self.assertEqual(universal_containers({"pcm_s16le"}), ["MKA"])
|
|
|
|
def test_video_mp4_is_not_an_already_audio_only_m4a(self) -> None:
|
|
self.assertFalse(same_container(self.audio_input("clip.mp4", "mov,mp4,m4a", "aac", video=True), ".m4a"))
|
|
self.assertTrue(same_container(self.audio_input("clip.m4a", "mov,mp4,m4a", "aac"), ".m4a"))
|
|
|
|
def test_matching_actions_only_offer_skip_when_it_applies(self) -> None:
|
|
matching = self.audio_input("clip.m4a", "mov,mp4,m4a", "aac")
|
|
other = self.audio_input("clip.flac", "flac", "flac")
|
|
self.assertEqual(matching_actions([matching, other], {matching.path: 0, other.path: 0}, ".m4a", "aac"), ["skip", "remux", "re-encode"])
|
|
self.assertEqual(matching_actions([other], {other.path: 0}, ".m4a", "aac"), ["re-encode"])
|
|
|
|
def test_skip_only_applies_to_matching_audio_only_container(self) -> None:
|
|
item = self.audio_input("clip.m4a", "mov,mp4,m4a", "aac")
|
|
settings = AudioSettings("encode", "aac", "M4A", "skip", "aac", "192k")
|
|
self.assertEqual(_action_for(item, item.streams[0], settings), ("skip", "aac"))
|
|
|
|
def test_bitrate_accepts_bare_kilobits(self) -> None:
|
|
self.assertEqual(parse_bitrate("192"), "192k")
|
|
self.assertEqual(parse_bitrate("192K"), "192k")
|
|
with self.assertRaisesRegex(ValueError, "positive number"):
|
|
parse_bitrate("fast")
|
|
|
|
def test_output_directory_resolution(self) -> None:
|
|
working_dir = Path.cwd().resolve()
|
|
self.assertIsNone(resolve_output_dir("", working_dir))
|
|
self.assertEqual(resolve_output_dir("exports", working_dir), working_dir / "exports")
|
|
self.assertEqual(resolve_output_dir(str(working_dir / "elsewhere"), working_dir), working_dir / "elsewhere")
|
|
|
|
with TemporaryDirectory() as temporary:
|
|
file_path = Path(temporary) / "not-a-directory"
|
|
file_path.touch()
|
|
with self.assertRaisesRegex(ValueError, "not a directory"):
|
|
resolve_output_dir(str(file_path), working_dir)
|
|
|
|
def test_output_names_reserve_same_batch_collisions(self) -> None:
|
|
output_dir = Path.cwd() / "exports"
|
|
reserved: set[Path] = set()
|
|
first = unique_audio_target(Path("one/song.mp4"), ".opus", reserved, output_dir)
|
|
second = unique_audio_target(Path("two/song.mkv"), ".opus", reserved, output_dir)
|
|
self.assertEqual(first, output_dir / "song.opus")
|
|
self.assertEqual(second, output_dir / "song-1.opus")
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|