Skip to content

Commit 4c9fcc6

Browse files
committed
- utils.prod to extend math.prod to earlier python
- use utils.get_samplesize on all occasions
1 parent 86b411a commit 4c9fcc6

8 files changed

Lines changed: 41 additions & 41 deletions

File tree

src/ffmpegio/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def _run_read(*args, shape=None, pix_fmt_in=None, s_in=None, show_log=None, **kw
4444
if out.returncode:
4545
raise FFmpegError(out.stderr)
4646

47-
nbytes = utils.dtype_itemsize(dtype) * prod(shape)
47+
nbytes = utils.get_samplesize(shape, dtype)
4848

4949
return plugins.get_hook().bytes_to_video(
5050
b=out.stdout[-nbytes:], dtype=dtype, shape=shape

src/ffmpegio/plugins/rawdata_bytes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from math import prod
1+
from ..utils import get_samplesize
22
from pluggy import HookimplMarker
33

44
hookimpl = HookimplMarker("ffmpegio")
@@ -71,7 +71,7 @@ def bytes_to_video(b: bytes, dtype: str, shape: tuple[int, int, int]) -> object:
7171
return {
7272
"buffer": b,
7373
"dtype": dtype,
74-
"shape": (len(b) // (prod(shape) * int(dtype[-1])), *shape),
74+
"shape": (len(b) // get_samplesize(shape, dtype), *shape),
7575
}
7676

7777

@@ -92,5 +92,5 @@ def bytes_to_audio(b: bytes, dtype: str, shape: tuple[int]) -> object:
9292
return {
9393
"buffer": b,
9494
"dtype": dtype,
95-
"shape": (len(b) // (prod(shape) * int(dtype[-1])), *shape),
95+
"shape": (len(b) // get_samplesize(shape, dtype), *shape),
9696
}

src/ffmpegio/streams/SimpleStreams.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import logging
2-
from math import prod
32
from .. import utils, configure, ffmpegprocess, probe, plugins
43

54
# from ..utils import bytes_to_ndarray as _as_array
@@ -719,14 +718,15 @@ def _get_output_info(self, timeout):
719718

720719
def _start_reader(self):
721720

721+
self._bps_out = utils.get_samplesize(self.shape, self.dtype)
722+
self._bps_in = utils.get_samplesize(self.shape_in, self.dtype_in)
723+
self._out2in = self.rate / self.rate_in
724+
722725
# start the FFmpeg output reader
726+
self._reader.itemsize = self._bps_out
723727
self._reader.stdout = self._proc.stdout
724-
self._reader.itemsize = utils.get_samplesize(self.shape, self.dtype)
725728
self._reader.start()
726729

727-
self._bps_out = prod(self.shape) * int(self.dtype[-1])
728-
self._bps_in = prod(self.shape_in) * int(self.dtype_in[-1])
729-
self._out2in = self.rate / self.rate_in
730730
self._reader_needs_info = False
731731

732732
def close(self):

src/ffmpegio/utils/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
from math import cos, prod, radians, sin
1+
try:
2+
from math import prod
3+
except:
4+
from functools import reduce
5+
from operator import mul
6+
7+
prod = lambda seq: reduce(mul, seq, 1)
8+
9+
from math import cos, radians, sin
210
import re, fractions
311
from .. import caps
412

tests/test_ffmpegprocess.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import pytest
2-
3-
import logging, time
1+
import logging
42
from ffmpegio import probe, configure, ffmpegprocess, utils
5-
import numpy as np
63

74
# logging.basicConfig(level=logging.DEBUG)
85

@@ -115,8 +112,7 @@ def progress(*args):
115112
ffmpeg_args, pix_fmt_in, s_in, r_in
116113
)
117114

118-
out = np.empty(shape, dtype)
119-
blksize = out.itemsize * np.prod(shape) # 1 element
115+
samplesize = utils.get_samplesize(shape,dtype)
120116

121117
with ffmpegprocess.Popen(
122118
ffmpeg_args,
@@ -126,10 +122,8 @@ def progress(*args):
126122
for k in range(15):
127123
print(proc.stderr.readline())
128124
for j in range(10):
129-
memoryview(out).cast("b")[:] = memoryview(proc.stdout.read(blksize)).cast(
130-
"b"
131-
)
132-
print(j, out.shape, out.dtype)
125+
out = proc.stdout.read(samplesize)
126+
assert len(out)==samplesize
133127

134128

135129
if __name__ == "__main__":

tests/test_plugins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from math import prod
1+
from ffmpegio.utils import prod
22
from ffmpegio import plugins
33

44

tests/test_simplestreams.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
from distutils.debug import DEBUG
2-
import fractions
3-
import io
4-
from math import prod
5-
from time import sleep
61
import ffmpegio
72
import tempfile, re
83
from os import path
9-
import numpy as np
104
import logging
11-
from ffmpegio import streams
5+
from ffmpegio import streams, utils
126

137
logging.basicConfig(level=logging.DEBUG)
148

@@ -30,9 +24,17 @@ def test_read_video():
3024

3125
def test_read_write_video():
3226
fs, F = ffmpegio.video.read(url, t=1)
33-
bps = prod(F["shape"][1:]) * int(F["dtype"][-1])
34-
F0 = {"buffer": F["buffer"][:bps], "shape": F["shape"], "dtype": F["dtype"]}
35-
F1 = {"buffer": F["buffer"][bps:], "shape": F["shape"], "dtype": F["dtype"]}
27+
bps = utils.get_samplesize(F["shape"][-3:], F["dtype"])
28+
F0 = {
29+
"buffer": F["buffer"][:bps],
30+
"shape": (1, *F["shape"][1:]),
31+
"dtype": F["dtype"],
32+
}
33+
F1 = {
34+
"buffer": F["buffer"][bps:],
35+
"shape": (F["shape"][0] - 1, *F["shape"][1:]),
36+
"dtype": F["dtype"],
37+
}
3638

3739
with tempfile.TemporaryDirectory() as tmpdirname:
3840
out_url = path.join(tmpdirname, re.sub(r"\..*?$", outext, path.basename(url)))
@@ -45,7 +47,7 @@ def test_read_audio(caplog):
4547
# caplog.set_level(logging.DEBUG)
4648

4749
fs, x = ffmpegio.audio.read(url)
48-
bps = prod(x["shape"][1:]) * int(x["dtype"][-1])
50+
bps = utils.get_samplesize(x["shape"][-1:], x["dtype"])
4951

5052
with ffmpegio.open(url, "ra", show_log=True, blocksize=1024 ** 2) as f:
5153
# x = f.read(1024)

tests/test_video.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
from math import prod
2-
from ffmpegio import video, probe
1+
from ffmpegio import video, probe, utils
32
import tempfile, re
43
from os import path
5-
import numpy as np
64

75

86
def test_create():
@@ -50,7 +48,6 @@ def test_read_write():
5048
with tempfile.TemporaryDirectory() as tmpdirname:
5149
out_url = path.join(tmpdirname, re.sub(r"\..*?$", outext, path.basename(url)))
5250
print(out_url)
53-
print(prod(A["shape"]))
5451
video.write(out_url, fs, A)
5552
print(probe.video_streams_basic(out_url))
5653
fs, A = video.read(out_url, vframes=10)
@@ -73,14 +70,14 @@ def test_read():
7370
N = 5
7471
fs, B = video.read(url, vframes=10, ss_in=float(n0 / fs))
7572
print(B["shape"], A["shape"])
76-
nbytes = prod(A["shape"][1:]) * int(A["dtype"][-1])
77-
assert A["buffer"][n0 * nbytes :] == B['buffer'][: (10 - n0) * nbytes]
73+
nbytes = utils.get_samplesize(A["shape"][-3:], A["dtype"])
74+
assert A["buffer"][n0 * nbytes :] == B["buffer"][: (10 - n0) * nbytes]
7875

7976
fs, C = video.read(url, ss_in=float(n0 / fs), t_in=float(N / fs))
8077

8178
print(C["shape"])
82-
assert A["buffer"][n0 * nbytes :(n0+N)*nbytes] == C['buffer']
83-
79+
assert A["buffer"][n0 * nbytes : (n0 + N) * nbytes] == C["buffer"]
80+
8481
# fs, D = video.read(url, ss_in=n0, t_in=N, units='frames')
8582
# assert np.array_equal(D, C)
8683

@@ -113,7 +110,6 @@ def test_two_pass_write():
113110
# test_create()
114111
from ffmpegio import configure, utils, ffmpegprocess
115112
from ffmpegio.utils import log as log_utils
116-
import numpy as np
117113
from pprint import pprint
118114
import re
119115

0 commit comments

Comments
 (0)