Skip to content

Commit

Permalink
Base should override the output (GH-66)
Browse files Browse the repository at this point in the history
The `PyAV` installation issue causing on Windows has been moved to #67
  • Loading branch information
ArtyomVancyan authored Apr 18, 2023
2 parents 427c952 + c373977 commit be2ff79
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 15 deletions.
6 changes: 4 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ url = https://github.com/pysnippet/thumbnails
keywords =
vtt
json
thumbnail
generator
thumbnails
thumbnail-generator
license = Apache 2.0
Expand All @@ -29,8 +31,8 @@ classifiers =
packages =
thumbnails
install_requires =
av==8.0.0;python_version=='3.7'
av==9.2.0;python_version>'3.7'
av==8.0.0;sys_platform=='win32'
av==9.2.0;sys_platform!='win32'
click>=8.0.3
imageio-ffmpeg>=0.4.7
imageio>=2.23.0
Expand Down
2 changes: 1 addition & 1 deletion src/thumbnails/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from .thumbnail import ThumbnailVTT
from .thumbnail import register_thumbnail

__version__ = "0.1.9"
__version__ = "0.1.10"
__all__ = (
"Generator",
"Thumbnail",
Expand Down
2 changes: 1 addition & 1 deletion src/thumbnails/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DEFAULT_BASE = ""
DEFAULT_BASE = None
DEFAULT_SKIP = False
DEFAULT_OUTPUT = None
DEFAULT_FORMAT = "vtt"
Expand Down
6 changes: 3 additions & 3 deletions src/thumbnails/thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def format_time(secs):
return ("0%s.000" % delta)[:12]

metadata = ["WEBVTT\n\n"]
prefix = self.base or os.path.relpath(self.thumbnail_dir)
prefix = self.base if self.base is not None else os.path.relpath(self.thumbnail_dir)
route = os.path.join(prefix, extract_name(self.filepath) + ".png")
route = pathlib.Path(route).as_posix()

Expand Down Expand Up @@ -157,8 +157,8 @@ def generate(self):
with Progress("Saving thumbnail metadata at '%s'" % self.metadata_path):
for frame, start, *_ in self.thumbnails():
frame = os.path.join(self.thumbnail_dir, os.path.basename(frame))
base = os.path.join(self.base, os.path.basename(self.thumbnail_dir))
prefix = base if self.base else os.path.relpath(self.thumbnail_dir)
base = os.path.join(self.base or "", os.path.basename(self.thumbnail_dir))
prefix = base if self.base is not None else os.path.relpath(self.thumbnail_dir)
route = os.path.join(prefix, os.path.basename(frame))
route = pathlib.Path(route).as_posix()
thumbnail_data = {
Expand Down
54 changes: 54 additions & 0 deletions tests/data/snapshots/specified-empty-base-json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"0": {
"src": "video/0-00-00.png",
"width": "1280px"
},
"8": {
"src": "video/0-00-08.200000.png",
"width": "1280px"
},
"16": {
"src": "video/0-00-16.400000.png",
"width": "1280px"
},
"24": {
"src": "video/0-00-24.600000.png",
"width": "1280px"
},
"32": {
"src": "video/0-00-32.800000.png",
"width": "1280px"
},
"41": {
"src": "video/0-00-41.png",
"width": "1280px"
},
"49": {
"src": "video/0-00-49.200000.png",
"width": "1280px"
},
"57": {
"src": "video/0-00-57.400000.png",
"width": "1280px"
},
"65": {
"src": "video/0-01-05.600000.png",
"width": "1280px"
},
"73": {
"src": "video/0-01-13.800000.png",
"width": "1280px"
},
"82": {
"src": "video/0-01-22.png",
"width": "1280px"
},
"90": {
"src": "video/0-01-30.200000.png",
"width": "1280px"
},
"98": {
"src": "video/0-01-38.400000.png",
"width": "1280px"
}
}
41 changes: 41 additions & 0 deletions tests/data/snapshots/specified-empty-base-vtt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
WEBVTT

00:00:00.000 --> 00:00:08.200
video.png#xywh=0,0,1280,536

00:00:08.200 --> 00:00:16.400
video.png#xywh=1280,0,1280,536

00:00:16.400 --> 00:00:24.600
video.png#xywh=2560,0,1280,536

00:00:24.600 --> 00:00:32.800
video.png#xywh=0,536,1280,536

00:00:32.800 --> 00:00:41.000
video.png#xywh=1280,536,1280,536

00:00:41.000 --> 00:00:49.200
video.png#xywh=2560,536,1280,536

00:00:49.200 --> 00:00:57.400
video.png#xywh=0,1072,1280,536

00:00:57.400 --> 00:01:05.600
video.png#xywh=1280,1072,1280,536

00:01:05.600 --> 00:01:13.800
video.png#xywh=2560,1072,1280,536

00:01:13.800 --> 00:01:22.000
video.png#xywh=0,1608,1280,536

00:01:22.000 --> 00:01:30.200
video.png#xywh=1280,1608,1280,536

00:01:30.200 --> 00:01:38.400
video.png#xywh=2560,1608,1280,536

00:01:38.400 --> 00:01:46.600
video.png#xywh=0,2144,1280,536

12 changes: 7 additions & 5 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ def thumbnail_generation_with_default_output(tmp_media, inputs, fmt):
ogv_result.close()


def thumbnail_generation_with_with_extras(tmp_media, inputs, fmt):
def thumbnail_generation_with_with_extras(tmp_media, inputs, fmt, base, snapshot_file):
generator = Generator(inputs)
generator.base = "/media/thumbnails/"
generator.base = base
generator.output = os.path.join(tmp_media, "thumbnails")
generator.format = fmt
generator.compress = 0.5
generator.interval = 8.2
generator.generate()

snapshot = open(os.path.join(tmp_media, "snapshots", "specified-base-%s" % fmt))
snapshot = open(os.path.join(tmp_media, "snapshots", snapshot_file + fmt))
result = open(os.path.join(tmp_media, "thumbnails", "video.%s" % fmt))

if fmt == "json":
Expand All @@ -64,7 +64,8 @@ def test_api_vtt_generation_file_inputs_default_output(tmp_media):

def test_api_vtt_generation_with_extras(tmp_media):
inputs = (os.path.join(tmp_media, "avi"), os.path.join(tmp_media, "ogv"))
thumbnail_generation_with_with_extras(tmp_media, inputs, "vtt")
thumbnail_generation_with_with_extras(tmp_media, inputs, "vtt", "", "specified-empty-base-")
thumbnail_generation_with_with_extras(tmp_media, inputs, "vtt", "/media/thumbnails/", "specified-base-")


def test_api_json_generation_directory_inputs_default_output(tmp_media):
Expand All @@ -79,4 +80,5 @@ def test_api_json_generation_file_inputs_default_output(tmp_media):

def test_api_json_generation_with_extras(tmp_media):
inputs = (os.path.join(tmp_media, "avi"), os.path.join(tmp_media, "ogv"))
thumbnail_generation_with_with_extras(tmp_media, inputs, "json")
thumbnail_generation_with_with_extras(tmp_media, inputs, "json", "", "specified-empty-base-")
thumbnail_generation_with_with_extras(tmp_media, inputs, "json", "/media/thumbnails/", "specified-base-")
4 changes: 1 addition & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@


def execute_cli(*args, must_fail=False):
stdout = subprocess.run(("thumbnails", *args), capture_output=True)
assert stdout.returncode == must_fail
return stdout.stdout.decode()
assert subprocess.call(("thumbnails", *args)) == must_fail


def test_cli_help():
Expand Down

0 comments on commit be2ff79

Please sign in to comment.