simplefe

Log | Files | Refs

test_encode.py (2976B)


      1 from simplefe import encode
      2 from simplefe.settings import load_settings
      3 
      4 
      5 def test_build_stream_maps_uses_language_filter_when_available(monkeypatch):
      6     monkeypatch.setattr(encode, "_has_stream", lambda path, select, lang: True)
      7     settings = load_settings(env={})
      8     maps = encode.build_stream_maps("movie.mkv", settings)
      9     assert maps == ["-map", "0:v", "-map", "0:a:m:language:eng", "-map", "0:s:m:language:eng"]
     10 
     11 
     12 def test_build_stream_maps_falls_back_to_all_audio_when_no_english(monkeypatch):
     13     monkeypatch.setattr(encode, "_has_stream", lambda path, select, lang: False)
     14     settings = load_settings(env={})
     15     maps = encode.build_stream_maps("movie.mkv", settings)
     16     assert maps == ["-map", "0:v", "-map", "0:a"]
     17 
     18 
     19 def test_should_use_software_true_when_forced(monkeypatch):
     20     settings = load_settings(env={"ENCODE_FORCE_SOFTWARE": "true"})
     21 
     22     def fail_if_called(path):
     23         raise AssertionError("should not check height when force_software is set")
     24 
     25     monkeypatch.setattr(encode, "_video_height", fail_if_called)
     26     assert encode.should_use_software("movie.mkv", settings) is True
     27 
     28 
     29 def test_should_use_software_true_for_sd_content(monkeypatch):
     30     monkeypatch.setattr(encode, "_video_height", lambda path: 480)
     31     settings = load_settings(env={})
     32     assert encode.should_use_software("movie.mkv", settings) is True
     33 
     34 
     35 def test_should_use_software_false_for_hd_content(monkeypatch):
     36     monkeypatch.setattr(encode, "_video_height", lambda path: 1080)
     37     settings = load_settings(env={})
     38     assert encode.should_use_software("movie.mkv", settings) is False
     39 
     40 
     41 def test_build_ffmpeg_command_software():
     42     settings = load_settings(env={"ENCODE_CRF": "18"})
     43     command = encode.build_ffmpeg_command(
     44         "in.mkv", "out.mkv", settings, use_software=True
     45     )
     46     assert "libx265" in command
     47     assert "-crf" in command and "18" in command
     48     assert "-preset" in command and "medium" in command
     49     assert command[-1] == "out.mkv"
     50 
     51 
     52 def test_build_ffmpeg_command_hardware():
     53     settings = load_settings(env={"ENCODE_CRF": "20"})
     54     command = encode.build_ffmpeg_command(
     55         "in.mkv", "out.mkv", settings, use_software=False
     56     )
     57     assert "hevc_vaapi" in command
     58     assert "-qp" in command and "20" in command
     59     assert "-vaapi_device" in command and "/dev/dri/renderD128" in command
     60 
     61 
     62 def test_encode_runs_ffmpeg_and_returns_encoder_choice(tmp_path, monkeypatch):
     63     calls = []
     64     monkeypatch.setattr(encode, "should_use_software", lambda path, settings: True)
     65     monkeypatch.setattr(
     66         encode,
     67         "build_ffmpeg_command",
     68         lambda i, o, s, use_software: ["ffmpeg", "fake", "command"],
     69     )
     70     monkeypatch.setattr(
     71         encode.subprocess, "run", lambda cmd, check=True: calls.append(cmd)
     72     )
     73     settings = load_settings(env={})
     74     used_software = encode.encode(tmp_path / "in.mkv", tmp_path / "out.mkv", settings)
     75     assert used_software is True
     76     assert calls == [["ffmpeg", "fake", "command"]]