316 lines
12 KiB
Python
316 lines
12 KiB
Python
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
from ffmpeg_handling.audio_commands import (
|
|
cmd_extract_audio_track,
|
|
cmd_filter_audio,
|
|
cmd_remove_audio_track,
|
|
cmd_remove_russian_from_db,
|
|
cmd_set_primary_audio,
|
|
)
|
|
from ffmpeg_handling.helpers import probe_media
|
|
from ffmpeg_handling.metadata_commands import cmd_apply_chapters, cmd_set_metadata
|
|
from ffmpeg_handling.video_commands import (
|
|
cmd_convert_audio,
|
|
cmd_convert_av,
|
|
cmd_convert_av_all_suffix,
|
|
cmd_convert_video,
|
|
cmd_cut_segment,
|
|
cmd_downsize,
|
|
cmd_info,
|
|
cmd_remove_subs,
|
|
)
|
|
|
|
ASSET_DIR = Path(__file__).resolve().parents[1] / "testfiles"
|
|
|
|
|
|
def command_args(input_path: Path, output_path: Path, **values):
|
|
defaults = {
|
|
"input": input_path,
|
|
"output": output_path,
|
|
"dry_run": False,
|
|
"loud": False,
|
|
"audio_index": [],
|
|
"kidflag": False,
|
|
"backup": False,
|
|
}
|
|
defaults.update(values)
|
|
return SimpleNamespace(**defaults)
|
|
|
|
|
|
class MediaCommandIntegrationTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.temp_dir = tempfile.TemporaryDirectory(prefix="ffmpeg_handling_test_")
|
|
self.output_dir = Path(self.temp_dir.name)
|
|
|
|
def tearDown(self):
|
|
self.temp_dir.cleanup()
|
|
|
|
def output(self, name):
|
|
return self.output_dir / name
|
|
|
|
def assert_stream_counts(self, path, *, audio=None, subtitles=None, video=None):
|
|
streams = probe_media(path).get("streams", [])
|
|
counts = {
|
|
kind: sum(stream.get("codec_type") == kind for stream in streams)
|
|
for kind in ("video", "audio", "subtitle")
|
|
}
|
|
if audio is not None:
|
|
self.assertEqual(counts["audio"], audio)
|
|
if subtitles is not None:
|
|
self.assertEqual(counts["subtitle"], subtitles)
|
|
if video is not None:
|
|
self.assertEqual(counts["video"], video)
|
|
|
|
def test_info_and_all_supported_fixture_assets_probe(self):
|
|
fixtures = sorted(
|
|
path for path in ASSET_DIR.glob("test_auto*")
|
|
if path.suffix.lower() in {".avi", ".m4b", ".mkv", ".mp4"}
|
|
)
|
|
self.assertGreaterEqual(len(fixtures), 7)
|
|
for fixture in fixtures:
|
|
with self.subTest(fixture=fixture.name):
|
|
self.assertTrue(probe_media(fixture).get("streams"))
|
|
self.assertEqual(
|
|
cmd_info(command_args(ASSET_DIR / "test_auto.mp4", self.output("unused"))),
|
|
(False, "nooutputneeded"),
|
|
)
|
|
|
|
def test_remove_subtitles_and_audio_preserves_other_streams(self):
|
|
source = ASSET_DIR / "test_auto.mkv"
|
|
no_subtitles = self.output("no_subtitles.mkv")
|
|
self.assertTrue(cmd_remove_subs(command_args(source, no_subtitles))[0])
|
|
self.assert_stream_counts(no_subtitles, audio=2, subtitles=0)
|
|
|
|
source_without_subtitles = ASSET_DIR / "test_auto_no_subs.mkv"
|
|
copied_without_subtitles = self.output("still_no_subtitles.mkv")
|
|
self.assertTrue(
|
|
cmd_remove_subs(
|
|
command_args(source_without_subtitles, copied_without_subtitles)
|
|
)[0]
|
|
)
|
|
self.assert_stream_counts(copied_without_subtitles, audio=2, subtitles=0)
|
|
|
|
one_subtitle_removed = self.output("one_subtitle_removed.mkv")
|
|
args = command_args(source, one_subtitle_removed, num=[3])
|
|
self.assertTrue(cmd_remove_subs(args)[0])
|
|
self.assert_stream_counts(one_subtitle_removed, subtitles=2)
|
|
|
|
one_audio = self.output("one_audio.mkv")
|
|
args = command_args(source, one_audio, audio_index=[1])
|
|
self.assertTrue(cmd_remove_audio_track(args)[0])
|
|
self.assert_stream_counts(one_audio, audio=1, subtitles=3)
|
|
|
|
def test_refusing_to_remove_the_last_audio_and_invalid_index(self):
|
|
source = ASSET_DIR / "test_auto_only_russian.mkv"
|
|
args = command_args(source, self.output("empty_audio.mkv"), audio_index=[1])
|
|
self.assertFalse(cmd_remove_audio_track(args)[0])
|
|
|
|
args.audio_index = [99]
|
|
with self.assertRaisesRegex(ValueError, "not present"):
|
|
cmd_remove_audio_track(args)
|
|
|
|
def test_audio_extraction_and_segment_cut(self):
|
|
source = ASSET_DIR / "test_auto.mkv"
|
|
extracted = self.output("extracted.mp3")
|
|
self.assertTrue(
|
|
cmd_extract_audio_track(
|
|
command_args(source, extracted, audio_index=[1])
|
|
)[0]
|
|
)
|
|
self.assertEqual(probe_media(extracted)["streams"][0]["codec_type"], "audio")
|
|
chapter_titles = self.output("audio_chapters.txt")
|
|
chapter_titles.write_text("Extracted track\n", encoding="utf-8")
|
|
chapter_args = command_args(
|
|
extracted,
|
|
self.output("renamed.mp3"),
|
|
chapter_file=chapter_titles,
|
|
file_counter=0,
|
|
)
|
|
self.assertTrue(cmd_apply_chapters(chapter_args)[0])
|
|
tagged_audio = self.output("001 - Extracted track.mp3")
|
|
self.assertEqual(
|
|
probe_media(tagged_audio)["format"]["tags"]["title"], "Extracted track"
|
|
)
|
|
multiple_args = command_args(
|
|
source, self.output("multiple.mp3"), audio_index=[1, 2]
|
|
)
|
|
self.assertTrue(cmd_extract_audio_track(multiple_args)[0])
|
|
for index in (1, 2):
|
|
output = self.output(f"multiple_stream_{index}.mp3")
|
|
self.assertTrue(output.is_file())
|
|
self.assertEqual(
|
|
probe_media(output)["streams"][0]["codec_type"], "audio"
|
|
)
|
|
|
|
cut = self.output("cut.mkv")
|
|
args = command_args(source, cut, start="1s", duration="3s")
|
|
self.assertTrue(cmd_cut_segment(args)[0])
|
|
self.assertLess(float(probe_media(cut)["format"]["duration"]), 8)
|
|
self.assertGreater(float(probe_media(cut)["format"]["duration"]), 2)
|
|
|
|
def test_audio_video_transcodes_and_downsize(self):
|
|
source = ASSET_DIR / "test_auto.mkv"
|
|
audio = self.output("audio_aac.mkv")
|
|
self.assertTrue(
|
|
cmd_convert_audio(
|
|
command_args(source, audio, audio_codec="aac")
|
|
)[0]
|
|
)
|
|
self.assertTrue(
|
|
all(
|
|
stream["codec_name"] == "aac"
|
|
for stream in probe_media(audio)["streams"]
|
|
if stream.get("codec_type") == "audio"
|
|
)
|
|
)
|
|
|
|
video = self.output("video_h264.mkv")
|
|
self.assertTrue(
|
|
cmd_convert_video(
|
|
command_args(source, video, video_codec="libx264")
|
|
)[0]
|
|
)
|
|
self.assert_stream_counts(video, audio=2, subtitles=3)
|
|
self.assertTrue(
|
|
all(
|
|
stream["codec_name"] == "h264"
|
|
for stream in probe_media(video)["streams"]
|
|
if stream.get("codec_type") == "video"
|
|
)
|
|
)
|
|
|
|
smaller = self.output("smaller.mkv")
|
|
args = command_args(source, smaller, width=320, height=180, crf=32)
|
|
self.assertTrue(cmd_downsize(args)[0])
|
|
first_video = next(
|
|
stream for stream in probe_media(smaller)["streams"]
|
|
if stream.get("codec_type") == "video"
|
|
)
|
|
self.assertEqual((first_video["width"], first_video["height"]), (320, 180))
|
|
|
|
def test_full_reencode_and_suffix_filter(self):
|
|
source = ASSET_DIR / "test_auto.mkv"
|
|
output = self.output("full.mp4")
|
|
args = command_args(
|
|
source, output, video_codec="libx264", audio_codec="aac",
|
|
preset="ultrafast", quality=30, new_suffix=None,
|
|
)
|
|
self.assertTrue(cmd_convert_av(args)[0])
|
|
converted_streams = probe_media(output)["streams"]
|
|
self.assertTrue(converted_streams)
|
|
self.assertTrue(
|
|
all(
|
|
stream["codec_name"] == "h264"
|
|
for stream in converted_streams
|
|
if stream.get("codec_type") == "video"
|
|
)
|
|
)
|
|
self.assertTrue(
|
|
all(
|
|
stream["codec_name"] == "aac"
|
|
for stream in converted_streams
|
|
if stream.get("codec_type") == "audio"
|
|
)
|
|
)
|
|
|
|
args.suffix = ".mp4"
|
|
self.assertFalse(cmd_convert_av_all_suffix(args)[0])
|
|
args.suffix = ".mkv"
|
|
args.new_suffix = ".mp4"
|
|
args.output = self.output("filtered.mkv")
|
|
self.assertTrue(cmd_convert_av_all_suffix(args)[0])
|
|
self.assertTrue(self.output("filtered.mp4").is_file())
|
|
|
|
def test_audio_filter_and_primary_stream_order(self):
|
|
source = ASSET_DIR / "test_auto.mkv"
|
|
filtered = self.output("filtered_audio.mkv")
|
|
args = command_args(source, filtered, audio_index=[2])
|
|
self.assertTrue(cmd_filter_audio(args)[0])
|
|
self.assert_stream_counts(filtered, audio=3)
|
|
self.assertTrue(
|
|
all(
|
|
stream["codec_name"] == "vorbis"
|
|
for stream in probe_media(filtered)["streams"]
|
|
if stream.get("codec_type") == "audio"
|
|
)
|
|
)
|
|
|
|
primary = self.output("primary.mkv")
|
|
args.output = primary
|
|
self.assertTrue(cmd_set_primary_audio(args)[0])
|
|
audio_streams = [
|
|
stream for stream in probe_media(primary)["streams"]
|
|
if stream.get("codec_type") == "audio"
|
|
]
|
|
self.assertEqual(audio_streams[0].get("tags", {}).get("language"), "ger")
|
|
self.assertEqual(audio_streams[0].get("disposition", {}).get("default"), 1)
|
|
|
|
def test_metadata_and_chapter_operations(self):
|
|
source = ASSET_DIR / "test_auto.m4b"
|
|
metadata = self.output("metadata.m4b")
|
|
args = command_args(
|
|
source, metadata, key="artist", value="Unit Test", chapter_file=None
|
|
)
|
|
self.assertTrue(cmd_set_metadata(args)[0])
|
|
self.assertEqual(
|
|
probe_media(metadata)["format"]["tags"]["artist"], "Unit Test"
|
|
)
|
|
|
|
chapter_output = self.output("chapters.m4b")
|
|
args.output = chapter_output
|
|
args.chapter_file = ASSET_DIR / "test_auto_chapters.txt"
|
|
self.assertTrue(cmd_apply_chapters(args)[0])
|
|
chapter_info = probe_media(chapter_output)
|
|
expected_titles = [
|
|
line for line in args.chapter_file.read_text(encoding="utf-8").splitlines()
|
|
if line
|
|
]
|
|
self.assertEqual(
|
|
[chapter["tags"]["title"] for chapter in chapter_info["chapters"]],
|
|
expected_titles,
|
|
)
|
|
|
|
def test_chapter_count_mismatch_fails_clearly(self):
|
|
source = ASSET_DIR / "test_auto.m4b"
|
|
titles = self.output("too_many_chapters.txt")
|
|
titles.write_text("Only one title\n", encoding="utf-8")
|
|
args = command_args(
|
|
source, self.output("bad_chapters.m4b"), chapter_file=titles
|
|
)
|
|
with self.assertRaisesRegex(ValueError, "Chapter count mismatch"):
|
|
cmd_apply_chapters(args)
|
|
|
|
def test_russian_track_removal_leaves_non_russian_streams(self):
|
|
source = ASSET_DIR / "test_auto.mkv"
|
|
output = self.output("no_russian.mkv")
|
|
args = command_args(source, output)
|
|
self.assertTrue(cmd_remove_russian_from_db(args)[0])
|
|
media = probe_media(output)
|
|
self.assertEqual(
|
|
sum(stream.get("codec_type") == "audio" for stream in media["streams"]),
|
|
1,
|
|
)
|
|
self.assertFalse(
|
|
any(
|
|
stream.get("tags", {}).get("language") == "rus"
|
|
for stream in media["streams"]
|
|
if stream.get("codec_type") in {"audio", "subtitle"}
|
|
)
|
|
)
|
|
|
|
def test_dry_run_does_not_create_output(self):
|
|
output = self.output("dry_run.mkv")
|
|
args = command_args(
|
|
ASSET_DIR / "test_auto.mkv", output, dry_run=True, width=320,
|
|
height=180, crf=30,
|
|
)
|
|
self.assertTrue(cmd_downsize(args)[0])
|
|
self.assertFalse(output.exists())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|