Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a video frame by frame using ffmpeg

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()

And here is the 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

What could be causing this problem, and how can I fix it?

like image 237
Chris Avatar asked Mar 18 '26 23:03

Chris


1 Answers

The problem is with the specification of video codec. Change the string "mpeg" to "mpeg4".

like image 168
Toothpick Anemone Avatar answered Mar 20 '26 18:03

Toothpick Anemone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!