Python ffmpeg subprocess: Broken pipe



以下脚本使用 OpenCV 读取视频,对每个帧应用转换并尝试使用 ffmpeg 编写它。我的问题是,我没有使用subprocess模块获得 ffmpeg。我总是在我尝试写入 stdin的行中BrokenPipeError: [Errno 32] Broken pipe错误。为什么会这样,我做错了什么?

# Open input video with OpenCV
video_in = cv.VideoCapture(src_video_path)
frame_width = int(video_in.get(cv.CAP_PROP_FRAME_WIDTH))
frame_height = int(video_in.get(cv.CAP_PROP_FRAME_HEIGHT))
fps = video_in.get(cv.CAP_PROP_FPS)
frame_count = int(video_in.get(cv.CAP_PROP_FRAME_COUNT))
bitrate = bitrate * 4096 * 2160 / (frame_width * frame_height)
# Process video in ffmpeg pipe
# See http://zulko.github.io/blog/2013/09/27/read-and-write-video-frames-in-python-using-ffmpeg/
command = ['ffmpeg',
'-loglevel', 'error',
'-y',
# Input
'-f', 'rawvideo',
'-vcodec', 'rawvideo'
'-pix_fmt', 'bgr24',
'-s', str(frame_width) + 'x' + str(frame_height),
'-r', str(fps),
# Output
'-i', '-',
'-an',
'-vcodec', 'h264',
'-r', str(fps),
'-b:v', str(bitrate) + 'M',
'-pix_fmt', 'bgr24',
dst_video_path
]
pipe = sp.Popen(command, stdin=sp.PIPE)
for i_frame in range(frame_count):
ret, frame = video_in.read()
if ret:
warped_frame = cv.warpPerspective(frame, homography, (frame_width, frame_height))
pipe.stdin.write(warped_frame.astype(np.uint8).tobytes())
else:
print('Stopped early.')
break
print('Done!')

'-vcodec', 'rawvideo'!!!后面缺少逗号

我花了大约一个小时才注意到...

您还应该关闭stdin并等待print('Done!')

pipe.stdin.close()
pipe.wait()

最新更新