Project Name: Zulko+moviepy
Language: Python
Statement Type: API

Source Code:
def ffmpeg_write_image(filename, image, logfile=False, pixel_format=None):
    """Writes an image (HxWx3 or HxWx4 numpy array) to a file, using ffmpeg.

    Parameters
    ----------

    filename : str
        Path to the output file.

    image : np.ndarray
        Numpy array with the image data.

    logfile : bool, optional
        Writes the ffmpeg output inside a logging file (``True``) or not
        (``False``).

    pixel_format : str, optional
        Pixel format for ffmpeg. If not defined, it will be discovered checking
        if the image data contains an alpha channel (``"rgba"``) or not
        (``"rgb24"``).
    """
    if image.dtype != "uint8":
        image = image.astype("uint8")
    if not pixel_format:
        pixel_format = "rgba" if (image.shape[2] == 4) else "rgb24"

    cmd = [
        FFMPEG_BINARY,
        "-y",
        "-s",
        "%dx%d" % (image.shape[:2][::-1]),
        "-f",
        "rawvideo",
        "-pix_fmt",
        pixel_format,
        "-i",
        "-",
        filename,
    ]

    if logfile:
        log_file = open(filename + ".log", "w+")
    else:
        log_file = sp.PIPE

    popen_params = cross_platform_popen_params(
        {"stdout": sp.DEVNULL, "stderr": log_file, "stdin": sp.PIPE}
    )

    proc = sp.Popen(cmd, **popen_params)
    out, err = proc.communicate(image.tobytes())

    if proc.returncode:
        error = (
            f"{err}\n\nMoviePy error: FFMPEG encountered the following error while "
            f"writing file {filename} with command {cmd}:\n\n {err.decode()}"
        )

        raise IOError(error)

    del proc

ffmpeg_write_image(filename='/tmp/moviepy_ffmpeg_write_image.png', image=array([[[  0., 255.,   0.],        [ 51., 204.,   0.],        [102., 153.,   0.],        [153., 102.,   0.],        [204.,  51.,   0.]]]), logfile=False, pixel_format=None)

Selected Statement: proc = sp.Popen(cmd, **popen_params)

Function Input:
filename: '/tmp/moviepy_ffmpeg_write_image.png'
image: array([[[  0., 255.,   0.],        [ 51., 204.,   0.],        [102., 153.,   0.],        [153., 102.,   0.],        [204.,  51.,   0.]]])
logfile: False
pixel_format: None

Variable Values Before Statement:
cmd: /local/rcs/jinjun/miniforge3/envs/Zulko+moviepy/lib/python3.9/site-packages/imageio_ffmpeg/binaries/ffmpeg-linux64-v4.2.2
-y
-s
5x1
-f
rawvideo
-pix_fmt
rgb24
-i
-
/tmp/moviepy_ffmpeg_write_image.png

Value After Statement Execution: <Popen: returncode: None args: ['/local/rcs/jinjun/miniforge3/envs/Zulko+mov...>

Variable States During Runtime (line, value):
filename: 1
'/tmp/moviepy_ffmpeg_write_image.png'
image: 1
array([[[  0., 255.,   0.],        [ 51., 204.,   0.],        [102., 153.,   0.],        [153., 102.,   0.],        [204.,  51.,   0.]]])
23
array([[[  0, 255,   0],        [ 51, 204,   0],        [102, 153,   0],        [153, 101,   0],        [204,  50,   0]]], dtype=uint8)
logfile: 1
False
pixel_format: 1
None
25
'rgb24'
cmd: 27
/local/rcs/jinjun/miniforge3/envs/Zulko+moviepy/lib/python3.9/site-packages/imageio_ffmpeg/binaries/ffmpeg-linux64-v4.2.2
-y
-s
5x1
-f
rawvideo
-pix_fmt
rgb24
-i
-
/tmp/moviepy_ffmpeg_write_image.png
log_file: 44
-1
popen_params: 46
stdout: -3
stderr: -1
stdin: -1
proc: 50
<Popen: returncode: None args: ['/local/rcs/jinjun/miniforge3/envs/Zulko+mov...>
51
<Popen: returncode: 0 args: ['/local/rcs/jinjun/miniforge3/envs/Zulko+moviep...>
out: 51
None
err: 51
b"ffmpeg version 4.2.2-static https://johnvansickle.com/ffmpeg/  Copyright (c) 2000-2019 the FFmpeg developers\n  built with gcc 8 (Debian 8.3.0-6)\n  configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gmp --enable-libgme --enable-gray --enable-libaom --enable-libfribidi --enable-libass --enable-libvmaf --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librubberband --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libvorbis --enable-libopus --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libdav1d --enable-libxvid --enable-libzvbi --enable-libzimg\n  libavutil      56. 31.100 / 56. 31.100\n  libavcodec     58. 54.100 / 58. 54.100\n  libavformat    58. 29.100 / 58. 29.100\n  libavdevice    58.  8.100 / 58.  8.100\n  libavfilter     7. 57.100 /  7. 57.100\n  libswscale      5.  5.100 /  5.  5.100\n  libswresample   3.  5.100 /  3.  5.100\n  libpostproc    55.  5.100 / 55.  5.100\nInput #0, rawvideo, from 'pipe:':\n  Duration: N/A, start: 0.000000, bitrate: 3 kb/s\n    Stream #0:0: Video: rawvideo (RGB[24] / 0x18424752), rgb24, 5x1, 3 kb/s, 25 tbr, 25 tbn, 25 tbc\nStream mapping:\n  Stream #0:0 -> #0:0 (rawvideo (native) -> png (native))\nOutput #0, image2, to '/tmp/moviepy_ffmpeg_write_image.png':\n  Metadata:\n    encoder         : Lavf58.29.100\n    Stream #0:0: Video: png, rgb24, 5x1, q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc\n    Metadata:\n      encoder         : Lavc58.54.100 png\nframe=    1 fps=0.0 q=-0.0 Lsize=N/A time=00:00:00.04 bitrate=N/A speed=1.64x    \nvideo:0kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown\n"