File: ffmpeg.py

package info (click to toggle)
python-imageio 2.4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,824 kB
  • sloc: python: 18,306; makefile: 145
file content (1116 lines) | stat: -rw-r--r-- 43,124 bytes parent folder | download | duplicates (3)
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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
# -*- coding: utf-8 -*-
# imageio is distributed under the terms of the (new) BSD License.

""" Plugin that uses ffmpeg to read and write series of images to
a wide range of video formats.

Code inspired/based on code from moviepy: https://github.com/Zulko/moviepy/
by Zulko

"""

from __future__ import absolute_import, print_function, division

import sys
import os
import stat
import re
import time
import threading
import subprocess as sp
import logging

import numpy as np

from .. import formats
from ..core import (
    Format,
    get_remote_file,
    string_types,
    read_n_bytes,
    image_as_uint,
    get_platform,
    CannotReadFrameError,
    InternetNotAllowedError,
    NeedDownloadError,
)


ISWIN = sys.platform.startswith("win")

FNAME_PER_PLATFORM = {
    "osx32": "ffmpeg-osx-v3.2.4",
    "osx64": "ffmpeg-osx-v3.2.4",
    "win32": "ffmpeg-win32-v3.2.4.exe",
    "win64": "ffmpeg-win32-v3.2.4.exe",
    "linux32": "ffmpeg-linux32-v3.3.1",
    "linux64": "ffmpeg-linux64-v3.3.1",
}


def limit_lines(lines, N=32):
    """ When number of lines > 2*N, reduce to N.
    """
    if len(lines) > 2 * N:
        lines = [b"... showing only last few lines ..."] + lines[-N:]
    return lines


def download(directory=None, force_download=False):
    """ Download the ffmpeg exe to your computer.

    Parameters
    ----------
    directory : str | None
        The directory where the file will be cached if a download was
        required to obtain the file. By default, the appdata directory
        is used. This is also the first directory that is checked for
        a local version of the file.
    force_download : bool | str
        If True, the file will be downloaded even if a local copy exists
        (and this copy will be overwritten). Can also be a YYYY-MM-DD date
        to ensure a file is up-to-date (modified date of a file on disk,
        if present, is checked).
    """
    plat = get_platform()
    if not (plat and plat in FNAME_PER_PLATFORM):
        raise RuntimeError("FFMPEG exe isn't available for platform %s" % plat)
    fname = "ffmpeg/" + FNAME_PER_PLATFORM[plat]
    get_remote_file(fname=fname, directory=directory, force_download=force_download)


def get_exe():
    """ Get ffmpeg exe
    """
    # Try ffmpeg exe in order of priority.
    # 1. Try environment variable.
    exe = os.getenv("IMAGEIO_FFMPEG_EXE", None)
    if exe:
        return exe

    plat = get_platform()

    # 2. Try our own version from the imageio-binaries repository
    if plat and plat in FNAME_PER_PLATFORM:
        try:
            exe = get_remote_file("ffmpeg/" + FNAME_PER_PLATFORM[plat], auto=False)
            os.chmod(exe, os.stat(exe).st_mode | stat.S_IEXEC)  # executable
            return exe
        except (NeedDownloadError, InternetNotAllowedError):
            pass

    # 3. Try binary from conda package
    # (installed e.g. via `conda install ffmpeg -c conda-forge`)
    if plat.startswith("win"):
        exe = os.path.join(sys.prefix, "Library", "bin", "ffmpeg.exe")
    else:
        exe = os.path.join(sys.prefix, "bin", "ffmpeg")
    if exe and os.path.isfile(exe):
        try:
            with open(os.devnull, "w") as null:
                sp.check_call([exe, "-version"], stdout=null, stderr=sp.STDOUT)
                return exe
        except (OSError, ValueError, sp.CalledProcessError):
            pass

    # 4. Try system ffmpeg command
    exe = "ffmpeg"
    try:
        with open(os.devnull, "w") as null:
            sp.check_call([exe, "-version"], stdout=null, stderr=sp.STDOUT)
            return exe
    except (OSError, ValueError, sp.CalledProcessError):
        pass

    # Nothing was found so far
    raise NeedDownloadError(
        "Need ffmpeg exe. "
        "You can obtain it with either:\n"
        "  - install using conda: "
        "conda install ffmpeg -c conda-forge\n"
        "  - download using the command: "
        "imageio_download_bin ffmpeg\n"
        "  - download by calling (in Python): "
        "imageio.plugins.ffmpeg.download()\n"
    )


# Get camera format
if sys.platform.startswith("win"):
    CAM_FORMAT = "dshow"  # dshow or vfwcap
elif sys.platform.startswith("linux"):
    CAM_FORMAT = "video4linux2"
elif sys.platform.startswith("darwin"):
    CAM_FORMAT = "avfoundation"
else:  # pragma: no cover
    CAM_FORMAT = "unknown-cam-format"


class FfmpegFormat(Format):
    """ The ffmpeg format provides reading and writing for a wide range
    of movie formats such as .avi, .mpeg, .mp4, etc. And also to read
    streams from webcams and USB cameras. 
    
    To read from camera streams, supply "<video0>" as the filename,
    where the "0" can be replaced with any index of cameras known to
    the system.
    
    Note that for reading regular video files, the avbin plugin is more
    efficient.
    
    The ffmpeg plugin requires an ``ffmpeg`` binary. Imageio searches
    this binary in the following locations (order of priority):
    
    - The path stored in the ``IMAGEIO_FFMPEG_EXE`` environment
      variable, which can be set using e.g.
      ``os.environ['IMAGEIO_FFMPEG_EXE'] = '/path/to/my/ffmpeg'``
    - The binary downloaded from the "imageio-binaries" repository
      (see below) which is stored either in the "imageio/resources"
      directory or in the user directory.
    - A binary installed as an anaconda package (see below).
    - The system ``ffmpeg`` command.
    
    If the binary is not available on the system, it can be downloaded
    manually from <https://github.com/imageio/imageio-binaries> by
    either
    
    - the command line script ``imageio_download_bin ffmpeg``,
    - the Python method ``imageio.plugins.ffmpeg.download()``, or
    - anaconda: ``conda install ffmpeg -c conda-forge``.
    
    Parameters for reading
    ----------------------
    fps : scalar
        The number of frames per second to read the data at. Default None (i.e.
        read at the file's own fps). One can use this for files with a
        variable fps, or in cases where imageio is unable to correctly detect
        the fps.
    loop : bool
        If True, the video will rewind as soon as a frame is requested
        beyond the last frame. Otherwise, IndexError is raised. Default False.
    size : str | tuple
        The frame size (i.e. resolution) to read the images, e.g. 
        (100, 100) or "640x480". For camera streams, this allows setting
        the capture resolution. For normal video data, ffmpeg will
        rescale the data.
    dtype : str | type
        The dtype for the output arrays. Determines the bit-depth that
        is requested from ffmpeg. Supported dtypes: uint8, uint16.
        Default: uint8.
    pixelformat : str
        The pixel format for the camera to use (e.g. "yuyv422" or
        "gray"). The camera needs to support the format in order for
        this to take effect. Note that the images produced by this
        reader are always RGB.
    input_params : list
        List additional arguments to ffmpeg for input file options.
        (Can also be provided as ``ffmpeg_params`` for backwards compatibility)
        Example ffmpeg arguments to use aggressive error handling:
        ['-err_detect', 'aggressive']
    output_params : list
        List additional arguments to ffmpeg for output file options (i.e. the
        stream being read by imageio).
    print_info : bool
        Print information about the video file as reported by ffmpeg.
    
    Parameters for saving
    ---------------------
    fps : scalar
        The number of frames per second. Default 10.
    codec : str
        the video codec to use. Default 'libx264', which represents the
        widely available mpeg4. Except when saving .wmv files, then the
        defaults is 'msmpeg4' which is more commonly supported for windows
    quality : float | None
        Video output quality. Default is 5. Uses variable bit rate. Highest
        quality is 10, lowest is 0. Set to None to prevent variable bitrate
        flags to FFMPEG so you can manually specify them using output_params
        instead. Specifying a fixed bitrate using 'bitrate' disables this
        parameter.
    bitrate : int | None
        Set a constant bitrate for the video encoding. Default is None causing
        'quality' parameter to be used instead.  Better quality videos with
        smaller file sizes will result from using the 'quality'  variable
        bitrate parameter rather than specifiying a fixed bitrate with this
        parameter.
    pixelformat: str
        The output video pixel format. Default is 'yuv420p' which most widely
        supported by video players.
    input_params : list
        List additional arguments to ffmpeg for input file options (i.e. the
        stream that imageio provides).
    output_params : list
        List additional arguments to ffmpeg for output file options.
        (Can also be provided as ``ffmpeg_params`` for backwards compatibility)
        Example ffmpeg arguments to use only intra frames and set aspect ratio:
        ['-intra', '-aspect', '16:9']
    ffmpeg_log_level: str
        Sets ffmpeg output log level.  Default is "warning".
        Values can be "quiet", "panic", "fatal", "error", "warning", "info"
        "verbose", or "debug". Also prints the FFMPEG command being used by
        imageio if "info", "verbose", or "debug".
    macro_block_size: int
        Size constraint for video. Width and height, must be divisible by this
        number. If not divisible by this number imageio will tell ffmpeg to
        scale the image up to the next closest size
        divisible by this number. Most codecs are compatible with a macroblock
        size of 16 (default), some can go smaller (4, 8). To disable this
        automatic feature set it to None, however be warned many players can't
        decode videos that are odd in size and some codecs will produce poor
        results or fail. See https://en.wikipedia.org/wiki/Macroblock.
    """

    def _can_read(self, request):
        if request.mode[1] not in "I?":
            return False

        # Read from video stream?
        # Note that we could write the _video flag here, but a user might
        # select this format explicitly (and this code is not run)
        if request.filename in ["<video%i>" % i for i in range(10)]:
            return True

        # Read from file that we know?
        if request.extension in self.extensions:
            return True

    def _can_write(self, request):
        if request.mode[1] in (self.modes + "?"):
            if request.extension in self.extensions:
                return True

    # --

    class Reader(Format.Reader):

        _exe = None
        _frame_catcher = None

        @classmethod
        def _get_exe(cls):
            cls._exe = cls._exe or get_exe()
            return cls._exe

        def _get_cam_inputname(self, index):
            if sys.platform.startswith("linux"):
                return "/dev/" + self.request._video[1:-1]

            elif sys.platform.startswith("win"):
                # Ask ffmpeg for list of dshow device names
                cmd = [
                    self._exe,
                    "-list_devices",
                    "true",
                    "-f",
                    CAM_FORMAT,
                    "-i",
                    "dummy",
                ]
                # Set `shell=True` in sp.Popen to prevent popup of
                # a command line window in frozen applications.
                proc = sp.Popen(
                    cmd, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, shell=True
                )
                proc.stdout.readline()
                proc.terminate()
                infos = proc.stderr.read().decode("utf-8")
                # Return device name at index
                try:
                    name = parse_device_names(infos)[index]
                except IndexError:
                    raise IndexError("No ffdshow camera at index %i." % index)
                return "video=%s" % name

            elif sys.platform.startswith("darwin"):
                # Appears that newer ffmpeg builds don't support -list-devices
                # on OS X. But you can directly open the camera by index.
                name = str(index)
                return name

            else:  # pragma: no cover
                return "??"

        def _open(
            self,
            loop=False,
            size=None,
            dtype=None,
            pixelformat=None,
            print_info=False,
            ffmpeg_params=None,
            input_params=None,
            output_params=None,
            fps=None,
        ):
            # Get exe
            self._exe = self._get_exe()
            # Process input args
            self._arg_loop = bool(loop)
            if size is None:
                self._arg_size = None
            elif isinstance(size, tuple):
                self._arg_size = "%ix%i" % size
            elif isinstance(size, string_types) and "x" in size:
                self._arg_size = size
            else:
                raise ValueError('FFMPEG size must be tuple of "NxM"')
            if pixelformat is None:
                pass
            elif not isinstance(pixelformat, string_types):
                raise ValueError("FFMPEG pixelformat must be str")
            if dtype is None:
                self._dtype = np.dtype("uint8")
            else:
                self._dtype = np.dtype(dtype)
                allowed_dtypes = ["uint8", "uint16"]
                if self._dtype.name not in allowed_dtypes:
                    raise ValueError(
                        "dtype must be one of: {}".format(", ".join(allowed_dtypes))
                    )
            self._arg_pixelformat = pixelformat
            self._arg_input_params = input_params or []
            self._arg_output_params = output_params or []
            self._arg_input_params += ffmpeg_params or []  # backward compat
            # Write "_video"_arg
            self.request._video = None
            if self.request.filename in ["<video%i>" % i for i in range(10)]:
                self.request._video = self.request.filename
            # Get local filename
            if self.request._video:
                index = int(self.request._video[-2])
                self._filename = self._get_cam_inputname(index)
            else:
                self._filename = self.request.get_local_filename()
            # Determine pixel format and depth
            self._depth = 3
            if self._dtype.name == "uint8":
                self._pix_fmt = "rgb24"
                self._bytes_per_channel = 1
            else:
                self._pix_fmt = "rgb48le"
                self._bytes_per_channel = 2
            # Initialize parameters
            self._proc = None
            self._pos = -1
            self._meta = {"plugin": "ffmpeg", "nframes": float("inf")}
            self._lastread = None
            # Start ffmpeg subprocess and get meta information
            self._initialize()
            self._load_infos()

            # For cameras, create thread that keeps reading the images
            if self.request._video:
                w, h = self._meta["size"]
                framesize = self._depth * w * h
                self._frame_catcher = FrameCatcher(self._proc.stdout, framesize)

        def _close(self):
            self._terminate()  # Short timeout
            self._proc = None

        def _get_length(self):
            return self._meta["nframes"]

        def _get_data(self, index):
            """ Reads a frame at index. Note for coders: getting an
            arbitrary frame in the video with ffmpeg can be painfully
            slow if some decoding has to be done. This function tries
            to avoid fectching arbitrary frames whenever possible, by
            moving between adjacent frames. """
            # Modulo index (for looping)
            if self._meta["nframes"] and self._meta["nframes"] < float("inf"):
                if self._arg_loop:
                    index %= self._meta["nframes"]

            if index == self._pos:
                return self._lastread, dict(new=False)
            elif index < 0:
                raise IndexError("Frame index must be > 0")
            elif index >= self._meta["nframes"]:
                raise IndexError("Reached end of video")
            else:
                if (index < self._pos) or (index > self._pos + 100):
                    self._reinitialize(index)
                else:
                    self._skip_frames(index - self._pos - 1)
                result, is_new = self._read_frame()
                self._pos = index
                return result, dict(new=is_new)

        def _get_meta_data(self, index):
            return self._meta

        def _initialize(self):
            """ Opens the file, creates the pipe. """
            # Create input args
            if self.request._video:
                iargs = ["-f", CAM_FORMAT]
                if self._arg_pixelformat:
                    iargs.extend(["-pix_fmt", self._arg_pixelformat])
                if self._arg_size:
                    iargs.extend(["-s", self._arg_size])
            else:
                iargs = []
            # Output args, for writing to pipe
            oargs = [
                "-f",
                "image2pipe",
                "-pix_fmt",
                self._pix_fmt,
                "-vcodec",
                "rawvideo",
            ]
            oargs.extend(["-s", self._arg_size] if self._arg_size else [])
            if self.request.kwargs.get("fps", None):
                fps = float(self.request.kwargs["fps"])
                oargs += ["-r", "%.02f" % fps]
            # Create process
            cmd = [self._exe] + self._arg_input_params
            cmd += iargs + ["-i", self._filename]
            cmd += oargs + self._arg_output_params + ["-"]
            # For Windows, set `shell=True` in sp.Popen to prevent popup
            # of a command line window in frozen applications.
            self._proc = sp.Popen(
                cmd, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, shell=ISWIN
            )

            # Create thread that keeps reading from stderr
            self._stderr_catcher = StreamCatcher(self._proc.stderr)

        def _reinitialize(self, index=0):
            """ Restarts the reading, starts at an arbitrary location
            (!! SLOW !!) """
            if self.request._video:
                raise RuntimeError("No random access when streaming from cam.")
            self._close()
            if index == 0:
                self._initialize()
            else:
                starttime = index / self._meta["fps"]

                # Create input args -> start time
                # Need minimum 6 significant digits accurate frame seeking
                # and to support higher fps videos
                # Also appears this epsilon below is needed to ensure frame
                # accurate seeking in some cases
                epsilon = -1 / self._meta["fps"] * 0.1
                iargs = ["-ss", "%.06f" % (starttime + epsilon)]
                iargs += ["-i", self._filename]

                # Output args, for writing to pipe
                oargs = [
                    "-f",
                    "image2pipe",
                    "-pix_fmt",
                    self._pix_fmt,
                    "-vcodec",
                    "rawvideo",
                ]
                oargs.extend(["-s", self._arg_size] if self._arg_size else [])
                if self.request.kwargs.get("fps", None):
                    fps = float(self.request.kwargs["fps"])
                    oargs += ["-r", "%.02f" % fps]

                # Create process
                cmd = [self._exe] + self._arg_input_params + iargs
                cmd += oargs + self._arg_output_params + ["-"]
                # For Windows, set `shell=True` in sp.Popen to prevent popup
                # of a command line window in frozen applications.
                self._proc = sp.Popen(
                    cmd, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, shell=ISWIN
                )

                # Create thread that keeps reading from stderr
                self._stderr_catcher = StreamCatcher(self._proc.stderr)

        def _terminate(self):
            """ Terminate the sub process.
            """
            # Check
            if self._proc is None:  # pragma: no cover
                return  # no process
            if self._proc.poll() is not None:
                return  # process already dead
            # Terminate process
            # Using kill since self._proc.terminate() does not seem
            # to work for ffmpeg, leaves processes hanging
            if self.request._video:
                # When streaming from webcam, quit by sending 'q' to ffmpeg
                self._proc.communicate("q")
            else:
                self._proc.kill()

            # Tell threads to stop when they have a chance. They are probably
            # blocked on reading from their file, but let's play it safe.
            if self._stderr_catcher:
                self._stderr_catcher.stop_me()
            if self._frame_catcher:
                self._frame_catcher.stop_me()

        def _load_infos(self):
            """ reads the FFMPEG info on the file and sets size fps
            duration and nframes. """

            # Wait for the catcher to get the meta information
            etime = time.time() + 10.0
            while (not self._stderr_catcher.header) and time.time() < etime:
                time.sleep(0.01)

            # Check whether we have the information
            infos = self._stderr_catcher.header
            if not infos:
                self._terminate()
                if self.request._video:
                    ffmpeg_err = (
                        "FFMPEG STDERR OUTPUT:\n"
                        + self._stderr_catcher.get_text(.1)
                        + "\n"
                    )
                    if "darwin" in sys.platform:
                        if "Unknown input format: 'avfoundation'" in ffmpeg_err:
                            ffmpeg_err += "Try installing FFMPEG using " "home brew to get a version with " "support for cameras."
                    raise IndexError(
                        "No video4linux camera at %s.\n\n%s"
                        % (self.request._video, ffmpeg_err)
                    )
                else:
                    err2 = self._stderr_catcher.get_text(0.2)
                    fmt = "Could not load meta information\n=== stderr ===\n%s"
                    raise IOError(fmt % err2)

            if self.request.kwargs.get("print_info", False):
                # print the whole info text returned by FFMPEG
                print(infos)
                print("-" * 80)
            lines = infos.splitlines()
            if "No such file or directory" in lines[-1]:
                if self.request._video:
                    raise IOError("Could not open steam %s." % self._filename)
                else:  # pragma: no cover - this is checked by Request
                    raise IOError("%s not found! Wrong path?" % self._filename)

            # Go!
            self._meta.update(parse_ffmpeg_info(lines))

            # Update with fps with user-value?
            if self.request.kwargs.get("fps", None):
                self._meta["fps"] = float(self.request.kwargs["fps"])

            # Estimate nframes
            self._meta["nframes"] = np.inf
            if self._meta["fps"] > 0 and "duration" in self._meta:
                n = int(round(self._meta["duration"] * self._meta["fps"]))
                self._meta["nframes"] = n

        def _read_frame_data(self):
            # Init and check
            w, h = self._meta["size"]
            framesize = self._depth * w * h * self._bytes_per_channel
            assert self._proc is not None

            try:
                # Read framesize bytes
                if self._frame_catcher:  # pragma: no cover - camera thing
                    s, is_new = self._frame_catcher.get_frame()
                else:
                    s = read_n_bytes(self._proc.stdout, framesize)
                    is_new = True
                # Check
                if len(s) != framesize:
                    raise RuntimeError(
                        "Frame is %i bytes, but expected %i." % (len(s), framesize)
                    )
            except Exception as err:
                self._terminate()
                err1 = str(err)
                err2 = self._stderr_catcher.get_text(0.4)
                fmt = "Could not read frame %i:\n%s\n=== stderr ===\n%s"
                raise CannotReadFrameError(fmt % (self._pos, err1, err2))
            return s, is_new

        def _skip_frames(self, n=1):
            """ Reads and throws away n frames """
            w, h = self._meta["size"]
            for i in range(n):
                self._read_frame_data()
            self._pos += n

        def _read_frame(self):
            # Read and convert to numpy array
            w, h = self._meta["size"]
            # t0 = time.time()
            s, is_new = self._read_frame_data()
            result = np.frombuffer(s, dtype=self._dtype).copy()
            result = result.reshape((h, w, self._depth))
            # t1 = time.time()
            # print('etime', t1-t0)

            # Store and return
            self._lastread = result
            return result, is_new

    # --

    class Writer(Format.Writer):

        _exe = None

        @classmethod
        def _get_exe(cls):
            cls._exe = cls._exe or get_exe()
            return cls._exe

        def _open(
            self,
            fps=10,
            codec="libx264",
            bitrate=None,
            pixelformat="yuv420p",
            ffmpeg_params=None,
            input_params=None,
            output_params=None,
            ffmpeg_log_level="quiet",
            quality=5,
            macro_block_size=16,
        ):
            self._exe = self._get_exe()
            # Get local filename
            self._filename = self.request.get_local_filename()
            # Determine pixel format and depth
            self._pix_fmt = None
            # Initialize parameters
            self._proc = None
            self._size = None
            self._cmd = None

        def _close(self):
            if self._proc is None:  # pragma: no cover
                return  # no process
            if self._proc.poll() is not None:
                return  # process already dead
            if self._proc.stdin:
                self._proc.stdin.close()
            self._proc.wait()
            self._proc = None

        def _append_data(self, im, meta):

            # Get props of image
            size = im.shape[:2]
            depth = 1 if im.ndim == 2 else im.shape[2]

            # Ensure that image is in uint8
            im = image_as_uint(im, bitdepth=8)

            # Set size and initialize if not initialized yet
            if self._size is None:
                map = {1: "gray", 2: "gray8a", 3: "rgb24", 4: "rgba"}
                self._pix_fmt = map.get(depth, None)
                if self._pix_fmt is None:
                    raise ValueError("Image must have 1, 2, 3 or 4 channels")
                self._size = size
                self._depth = depth
                self._initialize()

            # Check size of image
            if size != self._size:
                raise ValueError("All images in a movie should have same size")
            if depth != self._depth:
                raise ValueError(
                    "All images in a movie should have same " "number of channels"
                )

            assert self._proc is not None  # Check status

            # Write
            try:
                self._proc.stdin.write(im.tostring())
            except IOError as e:
                # Show the command and stderr from pipe
                msg = (
                    "{0:}\n\nFFMPEG COMMAND:\n{1:}\n\nFFMPEG STDERR "
                    "OUTPUT:\n".format(e, self._cmd)
                )
                raise IOError(msg)

        def set_meta_data(self, meta):
            raise RuntimeError(
                "The ffmpeg format does not support setting " "meta data."
            )

        def _initialize(self):
            """ Creates the pipe to ffmpeg. Open the file to write to. """

            # Get parameters
            # Note that H264 is a widespread and very good codec, but if we
            # do not specify a bitrate, we easily get crap results.
            sizestr = "%dx%d" % (self._size[1], self._size[0])
            fps = self.request.kwargs.get("fps", 10)
            default_codec = "libx264"
            if self._filename.lower().endswith(".wmv"):
                # This is a safer default codec on windows to get videos that
                # will play in powerpoint and other apps. H264 is not always
                # available on windows.
                default_codec = "msmpeg4"
            codec = self.request.kwargs.get("codec", default_codec)
            bitrate = self.request.kwargs.get("bitrate", None)
            quality = self.request.kwargs.get("quality", 5)
            ffmpeg_log_level = self.request.kwargs.get("ffmpeg_log_level", "warning")
            input_params = self.request.kwargs.get("input_params") or []
            output_params = self.request.kwargs.get("output_params") or []
            output_params += self.request.kwargs.get("ffmpeg_params") or []
            # You may need to use -pix_fmt yuv420p for your output to work in
            # QuickTime and most other players. These players only supports
            # the YUV planar color space with 4:2:0 chroma subsampling for
            # H.264 video. Otherwise, depending on your source, ffmpeg may
            # output to a pixel format that may be incompatible with these
            # players. See
            # https://trac.ffmpeg.org/wiki/Encode/H.264#Encodingfordumbplayers
            pixelformat = self.request.kwargs.get("pixelformat", "yuv420p")

            # Get command
            cmd = [
                self._exe,
                "-y",
                "-f",
                "rawvideo",
                "-vcodec",
                "rawvideo",
                "-s",
                sizestr,
                "-pix_fmt",
                self._pix_fmt,
                "-r",
                "%.02f" % fps,
            ] + input_params
            cmd += ["-i", "-"]
            cmd += ["-an", "-vcodec", codec, "-pix_fmt", pixelformat]
            # Add fixed bitrate or variable bitrate compression flags
            if bitrate is not None:
                cmd += ["-b:v", str(bitrate)]
            elif quality is not None:  # If None, then we don't add anything
                if quality < 0 or quality > 10:
                    raise ValueError(
                        "ffpmeg writer quality parameter out of"
                        "range. Expected range 0 to 10."
                    )
                quality = 1 - quality / 10.0
                if codec == "libx264":
                    # crf ranges 0 to 51, 51 being worst.
                    quality = int(quality * 51)
                    cmd += ["-crf", str(quality)]  # for h264
                else:  # Many codecs accept q:v
                    # q:v range can vary, 1-31, 31 being worst
                    # But q:v does not always have the same range.
                    # May need a way to find range for any codec.
                    quality = int(quality * 30) + 1
                    cmd += ["-qscale:v", str(quality)]  # for others

            # Note, for most codecs, the image dimensions must be divisible by
            # 16 the default for the macro_block_size is 16. Check if image is
            # divisible, if not have ffmpeg upsize to nearest size and warn
            # user they should correct input image if this is not desired.
            macro_block_size = self.request.kwargs.get("macro_block_size", 16)
            if (
                macro_block_size is not None
                and macro_block_size > 1
                and (
                    self._size[1] % macro_block_size > 0
                    or self._size[0] % macro_block_size > 0
                )
            ):
                out_w = self._size[1]
                if self._size[1] % macro_block_size > 0:
                    out_w += macro_block_size - (self._size[1] % macro_block_size)
                out_h = self._size[0]
                if self._size[0] % macro_block_size > 0:
                    out_h += macro_block_size - (self._size[0] % macro_block_size)
                cmd += ["-vf", "scale={}:{}".format(out_w, out_h)]
                logging.warning(
                    "IMAGEIO FFMPEG_WRITER WARNING: input image is not"
                    " divisible by macro_block_size={}, resizing from {} "
                    "to {} to ensure video compatibility with most codecs "
                    "and players. To prevent resizing, make your input "
                    "image divisible by the macro_block_size or set the "
                    "macro_block_size to None (risking incompatibility). You "
                    "may also see a FFMPEG warning concerning "
                    "speedloss due to "
                    "data not being aligned.".format(
                        macro_block_size, self._size[:2], (out_h, out_w)
                    )
                )

            if ffmpeg_log_level:
                # Rather than redirect stderr to a pipe, just set minimal
                # output from ffmpeg by default. That way if there are warnings
                # the user will see them.
                cmd += ["-v", ffmpeg_log_level]
            cmd += output_params
            cmd.append(self._filename)
            self._cmd = " ".join(cmd)  # For showing command if needed
            if any(
                [
                    level in ffmpeg_log_level
                    for level in ("info", "verbose", "debug", "trace")
                ]
            ):
                print("RUNNING FFMPEG COMMAND:", self._cmd, file=sys.stderr)

            # Launch process
            # For Windows, set `shell=True` in sp.Popen to prevent popup
            # of a command line window in frozen applications.
            self._proc = sp.Popen(
                cmd, stdin=sp.PIPE, stdout=sp.PIPE, stderr=None, shell=ISWIN
            )
            # Warning, directing stderr to a pipe on windows will cause ffmpeg
            # to hang if the buffer is not periodically cleared using
            # StreamCatcher or other means.


def cvsecs(*args):
    """ converts a time to second. Either cvsecs(min, secs) or
    cvsecs(hours, mins, secs).
    """
    if len(args) == 1:
        return args[0]
    elif len(args) == 2:
        return 60 * args[0] + args[1]
    elif len(args) == 3:
        return 3600 * args[0] + 60 * args[1] + args[2]


class FrameCatcher(threading.Thread):
    """ Thread to keep reading the frame data from stdout. This is
    useful when streaming from a webcam. Otherwise, if the user code
    does not grab frames fast enough, the buffer will fill up, leading
    to lag, and ffmpeg can also stall (experienced on Linux). The
    get_frame() method always returns the last available image.
    """

    def __init__(self, file, framesize):
        self._file = file
        self._framesize = framesize
        self._frame = None
        self._frame_is_new = False
        self._bytes_read = 0
        self._lock = threading.RLock()
        threading.Thread.__init__(self)
        self.setDaemon(True)  # do not let this thread hold up Python shutdown
        self._should_stop = False
        self.start()

    def stop_me(self):
        self._should_stop = True

    def get_frame(self):
        # This runs in the main thread
        while self._frame is None:  # pragma: no cover - an init thing
            time.sleep(0.001)
        with self._lock:
            is_new = self._frame_is_new
            self._frame_is_new = False  # reset
            return self._frame, is_new

    def _read(self, n):
        # This runs in the worker thread
        try:
            return self._file.read(n)
        except ValueError:
            return b""

    def run(self):
        # This runs in the worker thread
        framesize = self._framesize

        while not self._should_stop:
            time.sleep(0)  # give control to other threads
            s = self._read(framesize)
            while len(s) < framesize:
                need = framesize - len(s)
                part = self._read(need)
                if not part:
                    break
                s += part
                self._bytes_read += len(part)
            # Stop?
            if not s:
                return
            # Store frame
            with self._lock:
                # Lock ensures that _frame and frame_is_new remain consistent
                self._frame = s
                self._frame_is_new = True
            # NOTE: could add a threading.Condition to facilitate blocking


class StreamCatcher(threading.Thread):
    """ Thread to keep reading from stderr so that the buffer does not
    fill up and stalls the ffmpeg process. On stderr a message is send
    on every few frames with some meta information. We only keep the
    last ones.
    """

    def __init__(self, file):
        self._file = file
        self._header = ""
        self._lines = []
        self._remainder = b""
        threading.Thread.__init__(self)
        self.setDaemon(True)  # do not let this thread hold up Python shutdown
        self._should_stop = False
        self.start()

    def stop_me(self):
        self._should_stop = True

    @property
    def header(self):
        """ Get header text. Empty string if the header is not yet parsed.
        """
        return self._header

    def get_text(self, timeout=0):
        """ Get the whole text printed to stderr so far. To preserve
        memory, only the last 50 to 100 frames are kept.

        If a timeout is givem, wait for this thread to finish. When
        something goes wrong, we stop ffmpeg and want a full report of
        stderr, but this thread might need a tiny bit more time.
        """

        # Wait?
        if timeout > 0:
            etime = time.time() + timeout
            while self.isAlive() and time.time() < etime:  # pragma: no cover
                time.sleep(0.01)
        # Return str
        lines = b"\n".join(self._lines)
        return self._header + "\n" + lines.decode("utf-8", "ignore")

    def run(self):

        # Create ref here so it still exists even if Py is shutting down
        limit_lines_local = limit_lines

        while not self._should_stop:
            time.sleep(0.001)
            # Read one line. Detect when closed, and exit
            try:
                line = self._file.read(20)
            except ValueError:  # pragma: no cover
                break
            if not line:
                break
            # Process to divide in lines
            line = line.replace(b"\r", b"\n").replace(b"\n\n", b"\n")
            lines = line.split(b"\n")
            lines[0] = self._remainder + lines[0]
            self._remainder = lines.pop(-1)
            # Process each line
            self._lines.extend(lines)
            if not self._header:
                if get_output_video_line(self._lines):
                    header = b"\n".join(self._lines)
                    self._header += header.decode("utf-8", "ignore")
            elif self._lines:
                self._lines = limit_lines_local(self._lines)


def parse_device_names(ffmpeg_output):
    """ Parse the output of the ffmpeg -list-devices command"""
    device_names = []
    in_video_devices = False
    for line in ffmpeg_output.splitlines():
        if line.startswith("[dshow"):
            logging.debug(line)
            line = line.split("]", 1)[1].strip()
            if in_video_devices and line.startswith('"'):
                device_names.append(line[1:-1])
            elif "video devices" in line:
                in_video_devices = True
            elif "devices" in line:
                # set False for subsequent "devices" sections
                in_video_devices = False
    return device_names


def parse_ffmpeg_info(text):
    meta = {}

    if isinstance(text, list):
        lines = text
    else:
        lines = text.splitlines()

    # Get version
    ver = lines[0].split("version", 1)[-1].split("Copyright")[0]
    meta["ffmpeg_version"] = ver.strip() + " " + lines[1].strip()

    # get the output line that speaks about video
    videolines = [
        l for l in lines if l.lstrip().startswith("Stream ") and " Video: " in l
    ]
    line = videolines[0]

    # get the frame rate.
    # matches can be empty, see #171, assume nframes = inf
    # the regexp omits values of "1k tbr" which seems a specific edge-case #262
    # it seems that tbr is generally to be preferred #262
    matches = re.findall(" ([0-9]+\.?[0-9]*) (tbr|fps)", line)
    fps = 0
    matches.sort(key=lambda x: x[1] == "tbr", reverse=True)
    if matches:
        fps = float(matches[0][0].strip())
    meta["fps"] = fps

    # get the size of the original stream, of the form 460x320 (w x h)
    match = re.search(" [0-9]*x[0-9]*(,| )", line)
    parts = line[match.start() : match.end() - 1].split("x")
    meta["source_size"] = tuple(map(int, parts))

    # get the size of what we receive, of the form 460x320 (w x h)
    line = videolines[-1]  # Pipe output
    match = re.search(" [0-9]*x[0-9]*(,| )", line)
    parts = line[match.start() : match.end() - 1].split("x")
    meta["size"] = tuple(map(int, parts))

    # Check the two sizes
    if meta["source_size"] != meta["size"]:
        logging.warning(
            "Warning: the frame size for reading %s is "
            "different from the source frame size %s."
            % (meta["size"], meta["source_size"])
        )

    # get duration (in seconds)
    line = [l for l in lines if "Duration: " in l][0]
    match = re.search(" [0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9]", line)
    if match is not None:
        hms = map(float, line[match.start() + 1 : match.end()].split(":"))
        meta["duration"] = cvsecs(*hms)

    return meta


def get_output_video_line(lines):
    """Get the line that defines the video stream that ffmpeg outputs,
    and which we read.
    """
    in_output = False
    for line in lines:
        sline = line.lstrip()
        if sline.startswith(b"Output "):
            in_output = True
        elif in_output:
            if sline.startswith(b"Stream ") and b" Video:" in sline:
                return line


# Register. You register an *instance* of a Format class.
format = FfmpegFormat(
    "ffmpeg",
    "Many video formats and cameras (via ffmpeg)",
    ".mov .avi .mpg .mpeg .mp4 .mkv .wmv",
    "I",
)
formats.add_format(format)