1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
"""Tests for audio helpers: speed parameters, chunk cleanup, and encoding."""
import tempfile
import unittest
from pathlib import Path
from converter import config
from converter.audio import _encode_args, build_ffmetadata, cleanup_chunks, speed_export_params
class SpeedExportParamsTests(unittest.TestCase):
def test_normal_speed_no_filter(self):
self.assertEqual(speed_export_params(1.0), [])
def test_simple_speedup(self):
self.assertEqual(speed_export_params(1.5), ["-filter:a", "atempo=1.5"])
def test_simple_slowdown(self):
self.assertEqual(speed_export_params(0.75), ["-filter:a", "atempo=0.75"])
def test_chained_speedup_beyond_2x(self):
self.assertEqual(speed_export_params(3.0), ["-filter:a", "atempo=2.0,atempo=1.5"])
def test_chained_slowdown_below_half(self):
self.assertEqual(speed_export_params(0.25), ["-filter:a", "atempo=0.5,atempo=0.5"])
def test_zero_speed_rejected(self):
with self.assertRaises(ValueError):
speed_export_params(0)
def test_negative_speed_rejected(self):
with self.assertRaises(ValueError):
speed_export_params(-1.5)
class CleanupChunksTests(unittest.TestCase):
def test_removes_only_chunk_files(self):
with tempfile.TemporaryDirectory() as tmp:
chunks_dir = Path(tmp)
(chunks_dir / "chunk_0001.wav").write_bytes(b"stale")
(chunks_dir / "chunk_0002.wav").write_bytes(b"stale")
(chunks_dir / "keep.txt").write_bytes(b"keep")
original = config.CHUNKS_FOLDER
config.CHUNKS_FOLDER = chunks_dir
try:
cleanup_chunks()
finally:
config.CHUNKS_FOLDER = original
self.assertFalse((chunks_dir / "chunk_0001.wav").exists())
self.assertFalse((chunks_dir / "chunk_0002.wav").exists())
self.assertTrue((chunks_dir / "keep.txt").exists())
def test_removes_chapter_files(self):
with tempfile.TemporaryDirectory() as tmp:
chunks_dir = Path(tmp)
(chunks_dir / "chapter_0001.m4b").write_bytes(b"stale")
(chunks_dir / "chunk_0001.wav").write_bytes(b"stale")
original = config.CHUNKS_FOLDER
config.CHUNKS_FOLDER = chunks_dir
try:
cleanup_chunks()
finally:
config.CHUNKS_FOLDER = original
self.assertFalse((chunks_dir / "chapter_0001.m4b").exists())
self.assertFalse((chunks_dir / "chunk_0001.wav").exists())
class EncodeArgsTests(unittest.TestCase):
def test_mp3_uses_bitrate_only(self):
self.assertEqual(_encode_args("mp3"), ["-b:a", config.AUDIO_BITRATE])
def test_m4b_uses_aac(self):
self.assertEqual(_encode_args("m4b"), ["-c:a", "aac", "-b:a", config.AUDIO_BITRATE])
class BuildFFMetadataTests(unittest.TestCase):
def test_writes_chapters(self):
chapters = [(0, 1200, "One"), (1200, 2500, "Two")]
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "meta.txt"
build_ffmetadata(chapters, path)
content = path.read_text(encoding="utf-8")
self.assertTrue(content.startswith(";FFMETADATA1\n"))
self.assertIn("[CHAPTER]", content)
self.assertIn("TIMEBASE=1/1000", content)
self.assertIn("START=0", content)
self.assertIn("END=1200", content)
self.assertIn("title=One", content)
self.assertIn("START=1200", content)
self.assertIn("title=Two", content)
if __name__ == "__main__":
unittest.main()
|