Pik*_*ack 9 python subprocess ffmpeg python-3.x
以下脚本使用 OpenCV 读取视频,对每个帧应用转换,并尝试使用 ffmpeg 写入它。我的问题是,我无法让 ffmpeg 使用该subprocess模块。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!')
Run Code Online (Sandbox Code Playgroud)
后面少了一个逗号'-vcodec', 'rawvideo'!!!
我花了大约一个小时才注意到...
您还应该在stdin以下操作之前关闭并等待print('Done!'):
pipe.stdin.close()
pipe.wait()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11663 次 |
| 最近记录: |