[Python] Writing a video frame by frame using ffmpeg

Discussão em 'Python' iniciado por Stack, Maio 29, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to write a video frame by frame using ffmpeg as explained here: http://zulko.github.io/blog/2013/09/27/read-and-write-video-frames-in-python-using-ffmpeg/

    However, I am always getting an OSError: [Errno 22] Invalid argument. I am on Windows 7 and using Python 3.4. Here is the code:

    import subprocess as sp
    import numpy as np
    import time

    ffmpeg_bin = r'C:\path\to\ffmpeg\ffmpeg.exe'

    command = [ffmpeg_bin,
    '-y', # (optional) overwrite output file if it exists
    '-f', 'rawvideo',
    '-vcodec','rawvideo',
    '-s', '420x360', # size of one frame
    '-pix_fmt', 'rgb24',
    '-r', '24', # frames per second
    '-i', '-', # The imput comes from a pipe
    '-an', # Tells FFMPEG not to expect any audio
    '-vcodec', 'mpeg',
    'my_output_videofile.mp4' ]

    proc = sp.Popen(command, stdin=sp.PIPE, stderr=sp.PIPE)

    a = np.zeros((360, 420, 3), dtype=np.uint8)

    for ii in range(5*24):
    print(ii)
    proc.stdin.write(a.tostring())
    time.sleep(1/24)

    proc.stdin.close()
    proc.stderr.close()
    proc.wait()


    Any help is greatly appreciated.

    EDIT: As requested here the detailed output of the program

    0
    1
    Traceback (most recent call last):
    File ".\write_dummy_video.py", line 25, in <module>
    proc.stdin.write(a.tostring())
    OSError: [Errno 22] Invalid argument


    Interestingly, if I comment line number 26 (i.e. time.sleep(1/24)) the error message changes slightly but the loop still only runs twice. Here is the error output:

    0
    1
    Traceback (most recent call last):
    File ".\write_dummy_video.py", line 25, in <module>
    proc.stdin.write(a.tostring())
    BrokenPipeError: [Errno 32] Broken pipe

    Continue reading...

Compartilhe esta Página