Python ffmpeg子进程:管道破裂

问题描述 投票:0回答:1

以下脚本使用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!')
python python-3.x ffmpeg subprocess
1个回答
0
投票

'-vcodec', 'rawvideo'之后有一个缺少逗号 !!!

花了一个小时让我注意...

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

pipe.stdin.close()
pipe.wait()
© www.soinside.com 2019 - 2024. All rights reserved.