102 lines
4.6 KiB
Python
102 lines
4.6 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
|
|
from tools.metadata_editor import Field, REMOVE, parse_gps_coordinates, write_arguments
|
|
|
|
|
|
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")
|
|
|
|
|
|
class MetadataEditorTests(unittest.TestCase):
|
|
def test_decimal_and_ascii_dms_gps_are_equivalent(self) -> None:
|
|
self.assertEqual(parse_gps_coordinates("58.660600, 16.448300"), (58.6606, 16.4483))
|
|
latitude, longitude = parse_gps_coordinates("58 39 38.16 N, 16:26:53.88E")
|
|
self.assertAlmostEqual(latitude, 58.6606)
|
|
self.assertAlmostEqual(longitude, 16.4483)
|
|
|
|
def test_negative_decimal_coordinates_mean_south_and_west(self) -> None:
|
|
self.assertEqual(parse_gps_coordinates("-58.5, -16.25"), (-58.5, -16.25))
|
|
with self.assertRaisesRegex(ValueError, "cannot use N"):
|
|
parse_gps_coordinates("-58.5 N, 16.25 E")
|
|
|
|
def test_gps_writes_use_decimal_values_and_hemisphere_refs(self) -> None:
|
|
field = Field("GPS coordinates", (), "gps", pending="58.660600, -16.448300")
|
|
self.assertEqual(
|
|
write_arguments([field]),
|
|
[
|
|
"-EXIF:GPSLatitude=58.66060000",
|
|
"-EXIF:GPSLatitudeRef=N",
|
|
"-EXIF:GPSLongitude=16.44830000",
|
|
"-EXIF:GPSLongitudeRef=W",
|
|
],
|
|
)
|
|
|
|
def test_remove_gps_removes_coordinate_and_ref_tags(self) -> None:
|
|
field = Field("GPS coordinates", (), "gps", pending=REMOVE)
|
|
self.assertEqual(len(write_arguments([field])), 4)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|