File: test_ffmpeg.py

package info (click to toggle)
python-imageio 2.37.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,016 kB
  • sloc: python: 26,044; makefile: 138
file content (708 lines) | stat: -rw-r--r-- 21,718 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
""" Test ffmpeg

"""

import gc
import os
import platform
import sys
import threading
import time
import warnings
from io import BytesIO
from pathlib import Path

import numpy as np
import pytest
from conftest import IS_PYPY, deprecated_test

import imageio.plugins
import imageio.v2 as iio
import imageio.v3 as iio3
from imageio import core

psutil = pytest.importorskip(
    "psutil", reason="ffmpeg support cannot be tested without psutil"
)

imageio_ffmpeg = pytest.importorskip(
    "imageio_ffmpeg", reason="imageio-ffmpeg is not installed"
)

try:
    imageio_ffmpeg.get_ffmpeg_version()
except RuntimeError:
    pytest.skip("No compatible FFMPEG binary could be found.", allow_module_level=True)


def get_ffmpeg_pids():
    pids = set()
    for p in psutil.process_iter():
        if "ffmpeg" in p.name().lower():
            pids.add(p.pid)
    return pids


@pytest.mark.skipif(
    platform.machine() == "aarch64", reason="Can't download binary on aarch64"
)
def test_get_exe_installed():
    # backup any user-defined path
    if "IMAGEIO_FFMPEG_EXE" in os.environ:
        oldpath = os.environ["IMAGEIO_FFMPEG_EXE"]
    else:
        oldpath = ""
    # Test if download works
    os.environ["IMAGEIO_FFMPEG_EXE"] = ""
    path = imageio_ffmpeg.get_ffmpeg_exe()
    # cleanup
    os.environ.pop("IMAGEIO_FFMPEG_EXE")
    if oldpath:
        os.environ["IMAGEIO_FFMPEG_EXE"] = oldpath
    print(path)
    assert os.path.isfile(path)


def test_get_exe_env():
    # backup any user-defined path
    if "IMAGEIO_FFMPEG_EXE" in os.environ:
        oldpath = os.environ["IMAGEIO_FFMPEG_EXE"]
    else:
        oldpath = ""
    # set manual path
    path = "invalid/path/to/my/ffmpeg"
    os.environ["IMAGEIO_FFMPEG_EXE"] = path
    try:
        path2 = imageio_ffmpeg.get_ffmpeg_exe()
    except Exception:
        path2 = "none"
        pass
    # cleanup
    os.environ.pop("IMAGEIO_FFMPEG_EXE")
    if oldpath:
        os.environ["IMAGEIO_FFMPEG_EXE"] = oldpath
    assert path == path2


@deprecated_test
def test_select(test_images):
    fname1 = test_images / "cockatoo.mp4"

    F = iio.formats["ffmpeg"]
    assert F.name == "FFMPEG"

    assert F.can_read(core.Request(fname1, "rI"))
    assert F.can_write(core.Request(fname1, "wI"))

    # ffmpeg is default
    assert type(iio.formats[".mp4"]) is type(F)
    assert type(iio.formats.search_write_format(core.Request(fname1, "wI"))) is type(F)
    assert type(iio.formats.search_read_format(core.Request(fname1, "rI"))) is type(F)


def test_integer_reader_length(test_images):
    # Avoid regression for #280
    r = iio.get_reader(test_images / "cockatoo.mp4")
    assert r.get_length() == float("inf")
    assert isinstance(len(r), int)
    assert len(r) == sys.maxsize
    assert bool(r)
    assert True if r else False


def test_read_and_write(test_images, tmp_path):
    fname1 = test_images / "cockatoo.mp4"

    R = iio.read(fname1, "ffmpeg")
    assert isinstance(R, imageio.plugins.ffmpeg.FfmpegFormat.Reader)

    fname2 = tmp_path / "cockatoo.out.mp4"

    frame1, frame2, frame3 = 41, 131, 227

    # Read
    ims1 = []
    with iio.read(fname1, "ffmpeg") as R:
        for i in range(10):
            im = R.get_next_data()
            ims1.append(im)
            assert im.shape == (720, 1280, 3)
            assert (im.sum() / im.size) > 0  # pypy mean is broken
        assert im.sum() > 0

        # Seek to reference frames in steps. OUR code will skip steps
        im11 = R.get_data(frame1)
        im12 = R.get_data(frame2)
        im13 = R.get_data(frame3)

        # Now go backwards, seek will kick in
        R.get_next_data()
        im23 = R.get_data(frame3)
        im22 = R.get_data(frame2)
        im21 = R.get_data(frame1)

        # Also use set_image_index
        R.set_image_index(frame2)
        im32 = R.get_next_data()
        R.set_image_index(frame3)
        im33 = R.get_next_data()
        R.set_image_index(frame1)
        im31 = R.get_next_data()

        for im in (im11, im12, im13, im21, im22, im23, im31, im32, im33):
            assert im.shape == (720, 1280, 3)

        assert (im11 == im21).all() and (im11 == im31).all()
        assert (im12 == im22).all() and (im12 == im32).all()
        assert (im13 == im23).all() and (im13 == im33).all()

        assert not (im11 == im12).all()
        assert not (im11 == im13).all()

    # Save
    with iio.save(fname2, "ffmpeg") as W:
        for im in ims1:
            W.append_data(im)

    # Read the result
    ims2 = iio.mimread(fname2, "ffmpeg")
    assert len(ims1) == len(ims2)
    for im in ims2:
        assert im.shape == (720, 1280, 3)

    # Check
    for im1, im2 in zip(ims1, ims2):
        diff = np.abs(im1.astype(np.float32) - im2.astype(np.float32))
        if IS_PYPY:
            assert (diff.sum() / diff.size) < 100
        else:
            assert diff.mean() < 2.5


def test_v3_read(test_images):
    # this should trigger the plugin default
    # and read all frames by default
    frames = iio3.imread(test_images / "cockatoo.mp4")
    assert frames.shape == (280, 720, 1280, 3)


def test_write_not_contiguous(test_images, tmp_path):
    fname1 = test_images / "cockatoo.mp4"

    R = iio.read(fname1, "ffmpeg")
    assert isinstance(R, imageio.plugins.ffmpeg.FfmpegFormat.Reader)

    fname2 = tmp_path / "cockatoo.out.mp4"

    # Read
    ims1 = []
    with iio.read(fname1, "ffmpeg") as R:
        for i in range(10):
            im = R.get_next_data()
            ims1.append(im)

    # Save non contiguous data
    with iio.save(fname2, "ffmpeg") as W:
        for im in ims1:
            # DOn't slice the first dimension since it won't be
            # a multiple of 16. This will cause the writer to expand
            # the data to make it fit, we won't be able to compare
            # the difference between the saved and the original images.
            im = im[:, ::2]
            assert not im.flags.c_contiguous
            W.append_data(im)

    ims2 = iio.mimread(fname2, "ffmpeg")

    # Check
    for im1, im2 in zip(ims1, ims2):
        diff = np.abs(im1[:, ::2].astype(np.float32) - im2.astype(np.float32))
        if IS_PYPY:
            assert (diff.sum() / diff.size) < 100
        else:
            assert diff.mean() < 2.5


def write_audio(test_images, tmp_path, codec=None) -> dict:
    in_filename = test_images / "realshort.mp4"
    out_filename = tmp_path / "realshort_audio.mp4"

    in_file = []
    with iio.read(in_filename, "ffmpeg") as R:
        for i in range(5):
            im = R.get_next_data()
            in_file.append(im)

    # Now write with audio to preserve the audio track
    with iio.save(
        out_filename,
        format="ffmpeg",
        audio_path=in_filename.as_posix(),
        audio_codec=codec,
    ) as W:
        for im in in_file:
            W.append_data(im)

    R = iio.read(out_filename, "ffmpeg", loop=True)
    meta = R.get_meta_data()
    R.close()

    return meta


def test_write_audio_ac3(test_images, tmp_path):
    meta = write_audio(test_images, tmp_path, "ac3")
    assert "audio_codec" in meta and meta["audio_codec"] == "ac3"


def test_write_audio_default_codec(test_images, tmp_path):
    meta = write_audio(test_images, tmp_path)
    assert "audio_codec" in meta


def test_reader_more(test_images, tmp_path):
    fname1 = test_images / "cockatoo.mp4"

    fname3 = tmp_path / "cockatoo.stub.mp4"

    # Get meta data
    R = iio.read(fname1, "ffmpeg", loop=True)
    meta = R.get_meta_data()
    assert len(R) == 280
    assert isinstance(meta, dict)
    assert "fps" in meta
    R.close()

    # Test size argument
    im = iio.read(fname1, "ffmpeg", size=(50, 50)).get_data(0)
    assert im.shape == (50, 50, 3)
    im = iio.read(fname1, "ffmpeg", size="40x40").get_data(0)
    assert im.shape == (40, 40, 3)
    with pytest.raises(ValueError):
        iio.read(fname1, "ffmpeg", size=20)
    with pytest.raises(ValueError):
        iio.read(fname1, "ffmpeg", pixelformat=20)

    # Read all frames and test length
    R = iio.read(test_images / "realshort.mp4", "ffmpeg")
    count = 0
    while True:
        try:
            R.get_next_data()
        except IndexError:
            break
        else:
            count += 1
    assert count == R.count_frames()
    assert count in (35, 36)  # allow one frame off size that we know
    with pytest.raises(IndexError):
        R.get_data(-1)  # Test index error -1

    # Now read beyond (simulate broken file)
    with pytest.raises(StopIteration):
        R._read_frame()  # ffmpeg seems to have an extra frame
        R._read_frame()

    # Set the image index to 0 and go again
    R.set_image_index(0)
    count2 = 0
    while True:
        try:
            R.get_next_data()
        except IndexError:
            break
        else:
            count2 += 1
    assert count2 == count
    with pytest.raises(IndexError):
        R.get_data(-1)  # Test index error -1

    # Test loop
    R = iio.read(test_images / "realshort.mp4", "ffmpeg", loop=1)
    im1 = R.get_next_data()
    for i in range(1, len(R)):
        R.get_next_data()
    im2 = R.get_next_data()
    im3 = R.get_data(0)
    im4 = R.get_data(2)  # touch skipping frames
    assert (im1 == im2).all()
    assert (im1 == im3).all()
    assert not (im1 == im4).all()
    R.close()

    # Read invalid
    open(fname3, "wb")
    with pytest.raises(IOError):
        iio.read(fname3, "ffmpeg")

    # Read printing info
    iio.read(fname1, "ffmpeg", print_info=True)


def test_writer_more(test_images, tmp_path):
    fname2 = tmp_path / "cockatoo.out.mp4"

    W = iio.save(fname2, "ffmpeg")
    with pytest.raises(ValueError):  # Invalid shape
        W.append_data(np.zeros((20, 20, 5), np.uint8))
    W.append_data(np.zeros((20, 20, 3), np.uint8))
    with pytest.raises(ValueError):  # Different shape from first image
        W.append_data(np.zeros((20, 19, 3), np.uint8))
    with pytest.raises(ValueError):  # Different depth from first image
        W.append_data(np.zeros((20, 20, 4), np.uint8))
    with pytest.raises(RuntimeError):  # No meta data
        W.set_meta_data({"foo": 3})
    W.close()


def test_writer_file_properly_closed(tmpdir):
    # Test to catch if file is correctly closed.
    # Otherwise it won't play in most players. This seems to occur on windows.
    tmpf = tmpdir.join("test.mp4")
    W = iio.get_writer(str(tmpf))
    for i in range(12):
        W.append_data(np.zeros((100, 100, 3), np.uint8))
    W.close()
    W = iio.get_reader(str(tmpf))
    # If Duration: N/A reported by ffmpeg, then the file was not
    # correctly closed.
    # This will cause the file to not be readable in many players.
    assert 1.1 < W._meta["duration"] < 1.3


def test_writer_pixelformat_size_verbose(tmpdir):
    # Check that video pixel format and size get written as expected.

    # Make sure verbose option works and that default pixelformat is yuv420p
    tmpf = tmpdir.join("test.mp4")
    W = iio.get_writer(str(tmpf), ffmpeg_log_level="warning")
    nframes = 4  # Number of frames in video
    for i in range(nframes):
        # Use size divisible by 16 or it gets changed.
        W.append_data(np.zeros((64, 64, 3), np.uint8))
    W.close()

    # Check that video is correct size & default output video pixel format
    # is correct
    W = iio.get_reader(str(tmpf))
    assert W.count_frames() == nframes
    assert W._meta["size"] == (64, 64)
    assert W._meta["pix_fmt"] in ("yuv420p", "yuv420p(progressive)")

    # Now check that macroblock size gets turned off if requested
    W = iio.get_writer(str(tmpf), macro_block_size=1, ffmpeg_log_level="warning")
    for i in range(nframes):
        W.append_data(np.zeros((100, 106, 3), np.uint8))
    W.close()
    W = iio.get_reader(str(tmpf))
    assert W.count_frames() == nframes
    assert W._meta["size"] == (106, 100)
    assert W._meta["pix_fmt"] in ("yuv420p", "yuv420p(progressive)")

    # Now double check values different than default work
    W = iio.get_writer(str(tmpf), macro_block_size=4, ffmpeg_log_level="warning")
    for i in range(nframes):
        W.append_data(np.zeros((64, 65, 3), np.uint8))
    W.close()
    W = iio.get_reader(str(tmpf))
    assert W.count_frames() == nframes
    assert W._meta["size"] == (68, 64)
    assert W._meta["pix_fmt"] in ("yuv420p", "yuv420p(progressive)")

    # Now check that the macroblock works as expected for the default of 16
    W = iio.get_writer(str(tmpf), ffmpeg_log_level="debug")
    for i in range(nframes):
        W.append_data(np.zeros((111, 140, 3), np.uint8))
    W.close()
    W = iio.get_reader(str(tmpf))
    assert W.count_frames() == nframes
    # Check for warning message with macroblock
    assert W._meta["size"] == (144, 112)
    assert W._meta["pix_fmt"] in ("yuv420p", "yuv420p(progressive)")


def test_writer_ffmpeg_params(tmpdir):
    # Test optional ffmpeg_params with a valid option
    # Also putting in an image size that is not divisible by macroblock size
    # To check that the -vf scale overwrites what it does.
    tmpf = tmpdir.join("test.mp4")
    W = iio.get_writer(str(tmpf), ffmpeg_params=["-vf", "scale=320:240"])
    for i in range(10):
        W.append_data(np.zeros((100, 100, 3), np.uint8))
    W.close()
    W = iio.get_reader(str(tmpf))
    # Check that the optional argument scaling worked.
    assert W._meta["size"] == (320, 240)


def test_writer_wmv(tmpdir):
    # WMV has different default codec, make sure it works.
    tmpf = tmpdir.join("test.wmv")
    W = iio.get_writer(str(tmpf), ffmpeg_params=["-v", "info"])
    for i in range(10):
        W.append_data(np.zeros((100, 100, 3), np.uint8))
    W.close()

    W = iio.get_reader(str(tmpf))
    # Check that default encoder is msmpeg4 for wmv
    assert W._meta["codec"].startswith("msmpeg4")


def test_framecatcher():
    class FakeGenerator:
        def __init__(self, nframebytes):
            self._f = BytesIO()
            self._n = nframebytes
            self._lock = threading.RLock()
            self._bb = b""

        def write_and_rewind(self, bb):
            with self._lock:
                t = self._f.tell()
                self._f.write(bb)
                self._f.seek(t)

        def __next__(self):
            while True:
                time.sleep(0.001)
                with self._lock:
                    if self._f.closed:
                        raise StopIteration()
                    self._bb += self._f.read(self._n)
                if len(self._bb) >= self._n:
                    bb = self._bb[: self._n]
                    self._bb = self._bb[self._n :]
                    return bb

        def close(self):
            with self._lock:
                self._f.close()

    # Test our class
    N = 100
    file = FakeGenerator(N)
    file.write_and_rewind(b"v" * N)
    assert file.__next__() == b"v" * N

    file = FakeGenerator(N)
    T = imageio.plugins.ffmpeg.FrameCatcher(file)  # the file looks like a generator

    # Init None
    time.sleep(0.1)
    assert T._frame is None  # get_frame() would stall

    # Read frame
    file.write_and_rewind(b"x" * (N - 20))
    time.sleep(0.2)  # Let it read a part
    assert T._frame is None  # get_frame() would stall
    file.write_and_rewind(b"x" * 20)
    time.sleep(0.2)  # Let it read the rest
    frame, is_new = T.get_frame()
    assert frame == b"x" * N
    assert is_new, "is_new should be True the first time a frame is retrieved"

    # Read frame that has not been updated
    frame, is_new = T.get_frame()
    assert frame == b"x" * N, "frame content should be the same as before"
    assert not is_new, "is_new should be False if the frame has already been retrieved"

    # Read frame when we pass plenty of data
    file.write_and_rewind(b"y" * N * 3)
    time.sleep(0.2)
    frame, is_new = T.get_frame()
    assert frame == b"y" * N
    assert is_new, "is_new should be True again if the frame has been updated"

    # Close
    file.close()


def test_webcam():
    good_paths = ["<video0>", "<video42>"]
    for path in good_paths:
        # regression test for https://github.com/imageio/imageio/issues/676
        with iio3.imopen(path, "r", plugin="FFMPEG"):
            pass

        try:
            iio.read(path, format="ffmpeg")
        except IndexError:
            # IndexError should be raised when
            # path string is good but camera doesnt exist
            continue

    bad_paths = ["<videof1>", "<video0x>", "<video>"]
    for path in bad_paths:
        with pytest.raises(ValueError):
            iio.read(path)


def test_webcam_get_next_data():
    try:
        reader = iio.get_reader("<video0>")
    except IndexError:
        pytest.xfail("no webcam")

    # Get a number of frames and check for if they are new
    counter_new_frames = 0
    number_of_iterations = 100
    for i in range(number_of_iterations):
        frame = reader.get_next_data()
        if frame.meta["new"]:
            counter_new_frames += 1

    assert counter_new_frames < number_of_iterations, (
        "assuming the loop is faster than the webcam, the number of unique "
        "frames should be smaller than the number of iterations"
    )
    reader.close()


@pytest.mark.skipif(IS_PYPY, reason="This test is flakey on Pypy.")
def test_process_termination(test_images):
    pids0 = get_ffmpeg_pids()
    r1 = iio.get_reader(test_images / "cockatoo.mp4")
    r2 = iio.get_reader(test_images / "cockatoo.mp4")

    assert len(get_ffmpeg_pids().difference(pids0)) == 2

    r1.close()
    r2.close()

    assert len(get_ffmpeg_pids().difference(pids0)) == 0

    pids0 = get_ffmpeg_pids()
    r1 = iio.get_reader(test_images / "cockatoo.mp4")
    r2 = iio.get_reader(test_images / "cockatoo.mp4")

    assert len(get_ffmpeg_pids().difference(pids0)) == 2

    del r1
    del r2
    gc.collect()

    assert len(get_ffmpeg_pids().difference(pids0)) == 0


def test_webcam_process_termination():
    """
    Test for issue #343. Ensures that an ffmpeg process streaming from
    webcam is terminated properly when the reader is closed.

    """

    pids0 = get_ffmpeg_pids()

    try:
        # Open the first webcam found.
        with iio.get_reader("<video0>") as reader:
            assert reader._read_gen is not None
            assert get_ffmpeg_pids().difference(pids0)
        # Ensure that the corresponding ffmpeg process has been terminated.
        assert reader._read_gen is None
        assert not get_ffmpeg_pids().difference(pids0)
    except IndexError:
        pytest.xfail("no webcam")


def test_webcam_resource_warnings():
    """
    Test for issue #697. Ensures that ffmpeg Reader standard streams are
    properly closed by checking for ResourceWarning "unclosed file".

    todo: use pytest.does_not_warn() as soon as it becomes available
     (see https://github.com/pytest-dev/pytest/issues/9404)
    """
    try:
        with warnings.catch_warnings(record=True) as warns:
            warnings.simplefilter("error")
            with iio.get_reader("<video0>"):
                pass
    except IndexError:
        pytest.xfail("no webcam")

    if imageio_ffmpeg.__version__ == "0.4.5":
        # We still expect imagio_ffmpeg 0.4.5 to generate (at most) one warning.
        # todo: remove this assertion when a fix for imageio_ffmpeg issue #61
        #  has been released
        assert len(warns) < 2
        return

    # There should not be any warnings, but show warning messages if there are.
    assert not [w.message for w in warns]


def show_in_console(test_images):
    reader = iio.read(test_images / "cockatoo.mp4", "ffmpeg")
    # reader = iio.read('<video0>')
    im = reader.get_next_data()
    while True:
        im = reader.get_next_data()
        print(
            "frame min/max/mean: %1.1f / %1.1f / %1.1f"
            % (im.min(), im.max(), (im.sum() / im.size))
        )


def show_in_visvis(test_images):
    # reader = iio.read(test_images / "cockatoo.mp4", "ffmpeg")
    reader = iio.read("<video0>", fps=20)

    import visvis as vv  # type: ignore

    im = reader.get_next_data()
    f = vv.clf()
    f.title = reader.format.name
    t = vv.imshow(im, clim=(0, 255))

    while not f._destroyed:
        im = reader.get_next_data()
        if im.meta["new"]:
            t.SetData(im)
        vv.processEvents()


def test_reverse_read(tmpdir):
    # Ensure we can read a file in reverse without error.

    tmpf = tmpdir.join("test_vid.mp4")
    W = iio.get_writer(str(tmpf))
    for i in range(120):
        W.append_data(np.zeros((64, 64, 3), np.uint8))
    W.close()

    W = iio.get_reader(str(tmpf))
    for i in range(W.count_frames() - 1, 0, -1):
        print("reading", i)
        W.get_data(i)
    W.close()


def test_read_stream(test_images):
    """Test stream reading workaround"""

    video_blob = Path(test_images / "cockatoo.mp4").read_bytes()

    result = iio3.imread(video_blob, index=5, extension=".mp4")
    expected = iio3.imread("imageio:cockatoo.mp4", index=5)

    assert np.allclose(result, expected)


def test_write_stream(test_images, tmp_path):
    # regression test
    expected = iio3.imread(test_images / "newtonscradle.gif")
    iio3.imwrite(tmp_path / "test.mp4", expected, plugin="FFMPEG")

    # Note: No assertions here, because video compression is lossy and
    # imageio-python changes the shape of the array. Our PyAV plugin (which
    # should be preferred) does not have the latter limitaiton :)


def test_h264_reading(test_images, tmp_path):
    # regression test for
    # https://github.com/imageio/imageio/issues/900
    frames = iio3.imread(test_images / "cockatoo.mp4")
    iio3.imwrite(tmp_path / "cockatoo.h264", frames, plugin="FFMPEG")

    imageio.get_reader(tmp_path / "cockatoo.h264", "ffmpeg")