Files
sigal/tests/test_video.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

124 lines
3.9 KiB
Python
Raw Normal View History

2013-07-19 23:48:35 +02:00
import os
2021-10-15 09:06:57 +02:00
from datetime import datetime
2021-07-06 22:44:19 +02:00
from unittest.mock import patch
2020-03-15 22:58:03 -03:00
import pytest
2013-07-19 23:48:35 +02:00
2021-02-07 17:58:08 -03:00
from sigal.gallery import Video
2020-03-15 22:58:03 -03:00
from sigal.settings import Status, create_settings
2020-11-25 23:33:21 -03:00
from sigal.video import generate_thumbnail, generate_video, process_video, video_size
2013-07-19 23:48:35 +02:00
CURRENT_DIR = os.path.dirname(__file__)
2023-02-12 17:31:08 +01:00
SRCDIR = os.path.join(CURRENT_DIR, "sample", "pictures")
TEST_VIDEO = "example video.ogv"
SRCFILE = os.path.join(SRCDIR, "video", TEST_VIDEO)
2013-07-19 23:48:35 +02:00
2014-07-20 23:35:28 +02:00
def test_video_size():
size_src = video_size(SRCFILE)
assert size_src == (240, 98)
2023-02-12 17:31:08 +01:00
size_src = video_size("missing/file.mp4")
2015-08-07 00:35:07 +02:00
assert size_src == (0, 0)
2014-07-20 23:35:28 +02:00
2020-11-25 23:33:21 -03:00
def test_generate_thumbnail(tmpdir):
2023-02-12 17:31:08 +01:00
outname = str(tmpdir.join("test.jpg"))
2020-11-25 23:33:21 -03:00
generate_thumbnail(SRCFILE, outname, (50, 50), 5)
assert os.path.isfile(outname)
def test_process_video(tmpdir):
base, ext = os.path.splitext(TEST_VIDEO)
2021-02-07 17:58:08 -03:00
settings = create_settings(
2023-02-12 17:31:08 +01:00
video_format="ogv",
2021-02-07 17:58:08 -03:00
use_orig=True,
orig_link=True,
2023-02-12 17:31:08 +01:00
source=os.path.join(SRCDIR, "video"),
2021-02-07 17:58:08 -03:00
destination=str(tmpdir),
)
2023-02-12 17:31:08 +01:00
video = Video(TEST_VIDEO, ".", settings)
2021-02-07 17:58:08 -03:00
process_video(video)
2023-02-12 17:31:08 +01:00
dstfile = str(tmpdir.join(base + ".ogv"))
assert os.path.realpath(dstfile) == SRCFILE
2023-02-12 17:31:08 +01:00
settings = create_settings(video_format="mjpg")
2021-02-07 17:58:08 -03:00
assert process_video(video) == Status.FAILURE
settings = create_settings(thumb_video_delay=-1)
2021-02-07 17:58:08 -03:00
assert process_video(video) == Status.FAILURE
2021-10-15 09:06:57 +02:00
def test_metadata(tmpdir):
base, ext = os.path.splitext(TEST_VIDEO)
settings = create_settings(
2023-02-12 17:31:08 +01:00
video_format="ogv",
2021-10-15 09:06:57 +02:00
use_orig=True,
orig_link=True,
2023-02-12 17:31:08 +01:00
source=os.path.join(SRCDIR, "video"),
2021-10-15 09:06:57 +02:00
destination=str(tmpdir),
)
2023-02-12 17:31:08 +01:00
video = Video(TEST_VIDEO, ".", settings)
assert video.meta == {"date": ["2020-01-01T09:00:00"]}
2021-10-15 09:06:57 +02:00
assert video.date == datetime(2020, 1, 1, 9, 0)
2023-02-12 17:31:08 +01:00
@pytest.mark.parametrize("fmt", ["webm", "mp4"])
def test_generate_video_fit(tmpdir, fmt):
2013-07-19 23:48:35 +02:00
"""largest fitting dimension is width"""
base, ext = os.path.splitext(TEST_VIDEO)
2023-02-12 17:31:08 +01:00
dstfile = str(tmpdir.join(base + "." + fmt))
settings = create_settings(video_size=(125, 50), video_format=fmt)
generate_video(SRCFILE, dstfile, settings)
2013-07-19 23:48:35 +02:00
size_src = video_size(SRCFILE)
size_dst = video_size(dstfile)
2013-07-19 23:48:35 +02:00
assert size_dst[1] == 50
# less than 2% error on ratio
2020-11-25 23:33:21 -03:00
assert abs(size_dst[0] / size_dst[1] - size_src[0] / size_src[1]) < 2e-2
2013-07-19 23:48:35 +02:00
2023-02-12 17:31:08 +01:00
@pytest.mark.parametrize("fmt", ["webm", "mp4", "ogv"])
def test_generate_video_dont_enlarge(tmpdir, fmt):
"""Video dimensions should not be enlarged."""
2013-07-19 23:48:35 +02:00
base, ext = os.path.splitext(TEST_VIDEO)
2023-02-12 17:31:08 +01:00
dstfile = str(tmpdir.join(base + "." + fmt))
settings = create_settings(video_size=(1000, 1000), video_format=fmt)
generate_video(SRCFILE, dstfile, settings)
size_src = video_size(SRCFILE)
size_dst = video_size(dstfile)
2013-07-19 23:48:35 +02:00
assert size_src == size_dst
2023-02-12 17:31:08 +01:00
@patch("sigal.video.generate_video_pass")
@pytest.mark.parametrize("fmt", ["webm", "mp4"])
def test_second_pass_video(mock_generate_video_pass, fmt, tmpdir):
"""Video should be run through ffmpeg."""
base, ext = os.path.splitext(TEST_VIDEO)
2023-02-12 17:31:08 +01:00
dstfile = str(tmpdir.join(base + "." + fmt))
settings_1 = "-c:v libvpx-vp9 -b:v 0 -crf 30 -pass 1 -an -f null dev/null"
settings_2 = f"-c:v libvpx-vp9 -b:v 0 -crf 30 -pass 2 -f {fmt}"
settings_opts = {
2023-02-12 17:31:08 +01:00
"video_size": (100, 50),
"video_format": fmt,
fmt + "_options": settings_1.split(" "),
fmt + "_options_second_pass": settings_2.split(" "),
}
settings = create_settings(**settings_opts)
generate_video(SRCFILE, dstfile, settings)
call_args_list = mock_generate_video_pass.call_args_list
# The method is called twice
assert len(call_args_list) == 2
# The first call to the method should have 3 args, without the outname
args, kwargs = call_args_list[0]
assert len(args) == 3
# The second call to the method should have 4 args, with the outname
args, kwargs = call_args_list[1]
assert len(args) == 4