本文介绍了Python ffmpeg子进程:管道损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下脚本使用OpenCV读取视频,对每个帧进行转换,然后尝试使用ffmpeg编写视频.我的问题是,我无法在 subprocess
模块上使用ffmpeg.我总是在尝试写入stdin的行中收到错误 BrokenPipeError:[Errno 32]管道损坏
.为什么会这样,我在做什么错了?
The following script reads a video with OpenCV, applies a transformation to each frame and attempts to write it with ffmpeg. My problem is, that I don't get ffmpeg working with the subprocess
module. I always get the error BrokenPipeError: [Errno 32] Broken pipe
in the line where I try to write to stdin. Why is that, what am I doing wrong?
# 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'
后有一个缺少逗号 !!!
花了我一个小时来注意...
Took me about an hour to notice...
您还应该关闭 stdin
并等待 print('Done!')
:
pipe.stdin.close()
pipe.wait()
这篇关于Python ffmpeg子进程:管道损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!