Skip to content

Commit 9db66aa

Browse files
GH-39: Remove usages of the future components (GH-40)
* Fix path creation for the win32 platform * Fix the routes in the final thumbnails * Fix the PyAV version depending on the environment * Add assertions after CLI test cases
1 parent c62ac44 commit 9db66aa

File tree

14 files changed

+108
-65
lines changed

14 files changed

+108
-65
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,24 @@ thumbnails --help
3434

3535
## Development
3636

37-
Install the dependencies:
37+
### Install the dependencies
3838
```bash
3939
python3 -m pip install -r requirements.txt
4040
```
4141

42-
Install the development dependencies:
42+
### Install the development dependencies
4343
```bash
44-
python3 -m pip install -r requirements-dev.txt
44+
python3 -m pip install -r tests/requirements.txt
4545
```
4646

47-
Install the package in editable mode:
47+
### Install the package in editable mode
4848
```bash
4949
python3 -m pip install -e .
5050
```
51+
If you are using a non-default version of Python (for example, you work with a virtual environment), you may get an error with `distutils`. See how to [fix](https://askubuntu.com/questions/1261162) the issue.
5152

52-
Run the tests to ensure everything is working before opening a PR:
53+
### Run the tests
54+
Run this in the local environment to ensure everything is working before opening a PR
5355
```bash
5456
tox
5557
```

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
av>=9.2.0
1+
av>=8.0.0,<=9.2.0
22
click>=8.0.3
33
imageio-ffmpeg>=0.4.7
44
imageio>=2.23.0

setup.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ classifiers =
1515
packages =
1616
thumbnails
1717
install_requires =
18-
av>=9.2.0
18+
av==8.0.0;python_version=='3.7'
19+
av==9.2.0;python_version>'3.7'
1920
click>=8.0.3
2021
imageio-ffmpeg>=0.4.7
2122
imageio>=2.23.0

src/thumbnails/frame.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import functools
21
import math
32

43

@@ -10,6 +9,9 @@ def __init__(self, size):
109
_min_width = 300
1110
_min_height = math.ceil(_min_width * height / width)
1211

12+
self.__width = None
13+
self.__height = None
14+
1315
self._width = width / 10
1416
self._height = height / 10
1517
self._min_width = _min_width
@@ -20,12 +22,16 @@ def compress(self):
2022
"""Defines an interface for the compress property."""
2123
raise NotImplementedError
2224

23-
@functools.cached_property
25+
@property
2426
def width(self):
2527
"""Calculates and caches the frame width."""
26-
return max(self._min_width, self._width * self.compress)
28+
if not self.__width:
29+
self.__width = max(self._min_width, self._width * self.compress)
30+
return self.__width
2731

28-
@functools.cached_property
32+
@property
2933
def height(self):
3034
"""Calculates and caches the frame height."""
31-
return max(self._min_height, self._height * self.compress)
35+
if not self.__height:
36+
self.__height = max(self._min_height, self._height * self.compress)
37+
return self.__height

src/thumbnails/pathtools.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import functools
21
import os
32
from distutils.dir_util import create_tree
43

@@ -11,7 +10,6 @@ def listdir(directory):
1110
yield os.path.abspath(filepath)
1211

1312

14-
@functools.cache
1513
def metadata_path(path, out, fmt):
1614
"""Calculates the thumbnail metadata output path."""
1715
out = os.path.abspath(out or os.path.dirname(path))

src/thumbnails/thumbnail.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import os
3+
import pathlib
34
from abc import ABCMeta
45
from abc import abstractmethod
56
from datetime import timedelta
@@ -99,13 +100,12 @@ def prepare_frames(self):
99100
master = Image.new(mode="RGBA", size=next(thumbnails))
100101
master_path = os.path.join(self.thumbnail_dir, extract_name(self.filepath) + ".png")
101102

102-
for frame, *_, x, y in self.thumbnails():
103+
for frame, *_, x, y in thumbnails:
103104
with Image.open(frame) as image:
104105
image = image.resize((self.width, self.height), Image.ANTIALIAS)
105106
master.paste(image, (x, y))
106107

107108
master.save(master_path)
108-
self.tempdir.cleanup()
109109

110110
def generate(self):
111111
def format_time(secs):
@@ -115,6 +115,7 @@ def format_time(secs):
115115
metadata = ["WEBVTT\n\n"]
116116
prefix = self.base or os.path.relpath(self.thumbnail_dir)
117117
route = os.path.join(prefix, extract_name(self.filepath) + ".png")
118+
route = pathlib.Path(route).as_posix()
118119

119120
for _, start, end, x, y in self.thumbnails():
120121
thumbnail_data = "%s --> %s\n%s#xywh=%d,%d,%d,%d\n\n" % (
@@ -126,6 +127,8 @@ def format_time(secs):
126127
with open(self.metadata_path, "w") as fp:
127128
fp.writelines(metadata)
128129

130+
self.tempdir.cleanup()
131+
129132

130133
@register_thumbnail("json")
131134
class ThumbnailJSON(Thumbnail):
@@ -139,7 +142,6 @@ def prepare_frames(self):
139142
if os.path.exists(self.thumbnail_dir):
140143
remove_tree(self.thumbnail_dir)
141144
copy_tree(self.tempdir.name, self.thumbnail_dir)
142-
self.tempdir.cleanup()
143145

144146
def generate(self):
145147
metadata = {}
@@ -151,6 +153,7 @@ def generate(self):
151153
base = os.path.join(self.base, os.path.basename(self.thumbnail_dir))
152154
prefix = base if self.base else os.path.relpath(self.thumbnail_dir)
153155
route = os.path.join(prefix, os.path.basename(frame))
156+
route = pathlib.Path(route).as_posix()
154157
thumbnail_data = {
155158
"src": route,
156159
"width": "%spx" % self.width,
@@ -159,3 +162,5 @@ def generate(self):
159162

160163
with open(self.metadata_path, "w") as fp:
161164
json.dump(metadata, fp, indent=2)
165+
166+
self.tempdir.cleanup()

src/thumbnails/video.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import concurrent.futures
2-
import functools
32
import glob
43
import math
54
import os
@@ -15,7 +14,6 @@
1514
ffmpeg_bin = get_ffmpeg_exe()
1615

1716

18-
@functools.cache
1917
def arange(start, stop, step):
2018
"""Roughly equivalent to numpy.arange."""
2119

@@ -67,7 +65,9 @@ def calc_columns(frames_count, width, height):
6765
def _extract_frame(self, start_time):
6866
"""Extracts a single frame from the video by the offset."""
6967
offset = str(timedelta(seconds=start_time))
70-
output = "%s/%s.png" % (self.tempdir.name, offset)
68+
filename = "%s.png" % offset.replace(":", "-")
69+
output = os.path.join(self.tempdir.name, filename)
70+
os.close(os.open(output, os.O_CREAT, mode=0o664))
7171

7272
cmd = (
7373
ffmpeg_bin,

tests/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from distutils.dir_util import copy_tree
23
from tempfile import TemporaryDirectory
34

@@ -7,5 +8,5 @@
78
@pytest.fixture
89
def tmp_media():
910
with TemporaryDirectory() as tempdir:
10-
copy_tree("tests/data", tempdir)
11+
copy_tree(os.path.join("tests", "data"), tempdir)
1112
yield tempdir
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
{
22
"0": {
3-
"src": "%(tmp_media)s/video/0:00:00.png",
3+
"src": "%(tmp_media)s/video/0-00-00.png",
44
"width": "300px"
55
},
66
"10": {
7-
"src": "%(tmp_media)s/video/0:00:10.png",
7+
"src": "%(tmp_media)s/video/0-00-10.png",
88
"width": "300px"
99
},
1010
"20": {
11-
"src": "%(tmp_media)s/video/0:00:20.png",
11+
"src": "%(tmp_media)s/video/0-00-20.png",
1212
"width": "300px"
1313
},
1414
"30": {
15-
"src": "%(tmp_media)s/video/0:00:30.png",
15+
"src": "%(tmp_media)s/video/0-00-30.png",
1616
"width": "300px"
1717
},
1818
"40": {
19-
"src": "%(tmp_media)s/video/0:00:40.png",
19+
"src": "%(tmp_media)s/video/0-00-40.png",
2020
"width": "300px"
2121
},
2222
"50": {
23-
"src": "%(tmp_media)s/video/0:00:50.png",
23+
"src": "%(tmp_media)s/video/0-00-50.png",
2424
"width": "300px"
2525
},
2626
"60": {
27-
"src": "%(tmp_media)s/video/0:01:00.png",
27+
"src": "%(tmp_media)s/video/0-01-00.png",
2828
"width": "300px"
2929
},
3030
"70": {
31-
"src": "%(tmp_media)s/video/0:01:10.png",
31+
"src": "%(tmp_media)s/video/0-01-10.png",
3232
"width": "300px"
3333
},
3434
"80": {
35-
"src": "%(tmp_media)s/video/0:01:20.png",
35+
"src": "%(tmp_media)s/video/0-01-20.png",
3636
"width": "300px"
3737
},
3838
"90": {
39-
"src": "%(tmp_media)s/video/0:01:30.png",
39+
"src": "%(tmp_media)s/video/0-01-30.png",
4040
"width": "300px"
4141
},
4242
"100": {
43-
"src": "%(tmp_media)s/video/0:01:40.png",
43+
"src": "%(tmp_media)s/video/0-01-40.png",
4444
"width": "300px"
4545
}
4646
}
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
{
22
"0": {
3-
"src": "/media/thumbnails/video/0:00:00.png",
3+
"src": "/media/thumbnails/video/0-00-00.png",
44
"width": "300px"
55
},
66
"10": {
7-
"src": "/media/thumbnails/video/0:00:10.png",
7+
"src": "/media/thumbnails/video/0-00-10.png",
88
"width": "300px"
99
},
1010
"20": {
11-
"src": "/media/thumbnails/video/0:00:20.png",
11+
"src": "/media/thumbnails/video/0-00-20.png",
1212
"width": "300px"
1313
},
1414
"30": {
15-
"src": "/media/thumbnails/video/0:00:30.png",
15+
"src": "/media/thumbnails/video/0-00-30.png",
1616
"width": "300px"
1717
},
1818
"40": {
19-
"src": "/media/thumbnails/video/0:00:40.png",
19+
"src": "/media/thumbnails/video/0-00-40.png",
2020
"width": "300px"
2121
},
2222
"50": {
23-
"src": "/media/thumbnails/video/0:00:50.png",
23+
"src": "/media/thumbnails/video/0-00-50.png",
2424
"width": "300px"
2525
},
2626
"60": {
27-
"src": "/media/thumbnails/video/0:01:00.png",
27+
"src": "/media/thumbnails/video/0-01-00.png",
2828
"width": "300px"
2929
},
3030
"70": {
31-
"src": "/media/thumbnails/video/0:01:10.png",
31+
"src": "/media/thumbnails/video/0-01-10.png",
3232
"width": "300px"
3333
},
3434
"80": {
35-
"src": "/media/thumbnails/video/0:01:20.png",
35+
"src": "/media/thumbnails/video/0-01-20.png",
3636
"width": "300px"
3737
},
3838
"90": {
39-
"src": "/media/thumbnails/video/0:01:30.png",
39+
"src": "/media/thumbnails/video/0-01-30.png",
4040
"width": "300px"
4141
},
4242
"100": {
43-
"src": "/media/thumbnails/video/0:01:40.png",
43+
"src": "/media/thumbnails/video/0-01-40.png",
4444
"width": "300px"
4545
}
4646
}

0 commit comments

Comments
 (0)