-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ffmpeg_commands.py
More file actions
192 lines (162 loc) · 9.07 KB
/
Copy pathtest_ffmpeg_commands.py
File metadata and controls
192 lines (162 loc) · 9.07 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import unittest
from pathlib import Path
from unittest.mock import MagicMock, patch
import gi
gi.require_version("Gtk", "4.0")
gi.require_version("Adw", "1")
from gi.repository import Adw, Gtk
from video_converter.app import VideoConverterApp
from video_converter.ui.window import VideoConverterWindow
class TestFfmpegCommands(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.app = VideoConverterApp()
cls.app.register(None)
with patch("gi.repository.GLib.timeout_add"):
cls.app.do_startup()
cls.window = cls.app.win
cls.window.track_widgets = [
{
"widget": Adw.ComboRow(
model=Gtk.StringList.new(["Copy", "Re-encode", "Skip"])
),
"stream_index": 1,
"codec_type": "audio",
},
{
"widget": Adw.ComboRow(model=Gtk.StringList.new(["Copy", "Skip"])),
"stream_index": 2,
"codec_type": "subtitle",
},
]
@classmethod
def tearDownClass(cls):
cls.app.quit()
@patch("pathlib.Path.is_file", return_value=True)
@patch("video_converter.ui.window.get_video_duration", return_value=30)
def test_default_command(self, mock_get_video_duration, mock_is_file):
command = self.window.get_ffmpeg_command()
self.assertIsInstance(command, list)
self.assertIn("ffmpeg", command)
@patch("pathlib.Path.is_file", return_value=True)
@patch("video_converter.ui.window.get_video_duration", return_value=30)
def test_track_selection_commands(self, mock_get_video_duration, mock_is_file):
# Test copy audio, skip subtitle
self.window.track_widgets[0]["widget"].set_selected(0) # Copy
self.window.track_widgets[1]["widget"].set_selected(1) # Skip
command = " ".join(self.window.get_ffmpeg_command())
self.assertIn("-map 0:1", command)
self.assertIn("-c:0 copy", command)
self.assertNotIn("-map 0:2", command)
# Test re-encode audio, copy subtitle
self.window.track_widgets[0]["widget"].set_selected(1) # Re-encode
self.window.track_widgets[1]["widget"].set_selected(0) # Copy
command = " ".join(self.window.get_ffmpeg_command())
self.assertIn("-map 0:1", command)
self.assertIn("-c:0 libopus -b:0 128k", command)
self.assertIn("-map 0:2", command)
self.assertIn("-c:1 copy", command)
# Test skip audio, copy subtitle
self.window.track_widgets[0]["widget"].set_selected(2) # Skip
self.window.track_widgets[1]["widget"].set_selected(0) # Copy
command = " ".join(self.window.get_ffmpeg_command())
self.assertNotIn("-map 0:1", command)
self.assertIn("-map 0:2", command)
self.assertIn("-c:0 copy", command)
@patch("pathlib.Path.is_file", return_value=True)
@patch("video_converter.ui.window.get_video_duration", return_value=30)
def test_all_setting_combinations_produce_unique_commands(
self,
mock_get_video_duration,
mock_is_file,
):
generated_commands = {}
hwaccel_model = self.window.hwaccel_combo.get_model()
container_model = self.window.container_combo.get_model()
mode_model = self.window.mode_combo.get_model()
for h in range(hwaccel_model.get_n_items()):
self.window.hwaccel_combo.set_selected(h)
hwaccel_name = hwaccel_model.get_item(h).get_string()
codec_model = self.window.codec_combo.get_model()
for i in range(codec_model.get_n_items()):
self.window.codec_combo.set_selected(i)
codec_name = codec_model.get_item(i).value
quality_model = self.window.quality_combo.get_model()
cq_model = self.window.cq_combo.get_model()
for j in range(container_model.get_n_items()):
self.window.container_combo.set_selected(j)
container_name = container_model.get_item(j).get_string()
for l in range(quality_model.get_n_items()):
self.window.quality_combo.set_selected(l)
quality_name = quality_model.get_item(l).get_string()
for k in range(mode_model.get_n_items()):
self.window.mode_combo.set_selected(k)
mode_name = mode_model.get_item(k).get_string()
mode_key = self.window.mode_keys[k]
def check_command(sub_setting_name, sub_setting_value):
current_settings = {
"hw": hwaccel_name,
"codec": codec_name,
"container": container_name,
"quality_preset": quality_name,
"mode": mode_name,
"sub_setting_name": sub_setting_name,
"sub_setting_value": sub_setting_value,
}
current_settings_string = (
f"HW: '{hwaccel_name}', Codec: '{codec_name}', Container: '{container_name}', "
f"Quality: '{quality_name}', Mode: '{mode_name}', "
f"{sub_setting_name}: '{sub_setting_value}'"
)
mock_output_file = MagicMock(spec=Path)
mock_output_file.is_file.return_value = True
if container_name == "auto":
mock_output_file.suffix = ".mkv"
else:
mock_output_file.suffix = f".{container_name}"
with patch.object(
self.window, "output_file", mock_output_file
):
command = " ".join(self.window.get_ffmpeg_command())
if command in generated_commands:
conflicting_settings = generated_commands[command]
# Symmetrically check for m4v/mp4 alias
is_alias = {
current_settings["container"],
conflicting_settings["container"],
} == {"mp4", "m4v"}
# Check if all other settings are identical
other_settings_match = all(
current_settings[key] == conflicting_settings[key]
for key in current_settings
if key != "container"
)
if is_alias and other_settings_match:
return # Expected alias, do not fail
conflicting_settings_string = (
f"HW: '{conflicting_settings['hw']}', Codec: '{conflicting_settings['codec']}', Container: '{conflicting_settings['container']}', "
f"Quality: '{conflicting_settings['quality_preset']}', Mode: '{conflicting_settings['mode']}', "
f"{conflicting_settings['sub_setting_name']}: '{conflicting_settings['sub_setting_value']}'"
)
self.fail(
f'"{current_settings_string}" produces the same ffmpeg command as "{conflicting_settings_string}"\n=> {command}'
)
generated_commands[command] = current_settings
if mode_key == "cq":
if cq_model and cq_model.get_n_items() > 0:
for m in range(cq_model.get_n_items()):
self.window.cq_combo.set_selected(m)
cq_name = cq_model.get_item(m).get_string()
check_command("CQ Level", cq_name)
else:
check_command("CQ Level", "N/A")
elif "size" in mode_key:
for size in ["10", "50"]:
self.window.target_size_entry.set_value(int(size))
check_command("Target Size", size)
else: # cbr/vbr
for bitrate in ["800", "1200"]:
self.window.bitrate_entry.set_value(int(bitrate))
check_command("Bitrate", bitrate)
if __name__ == "__main__":
unittest.main()