python - Python ffmpeg 子进程:断管
问题描述
以下脚本使用 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!')
解决方案
后面有一个逗号'-vcodec', 'rawvideo'
!!!
我花了大约一个小时才注意到...
您还应该关闭stdin
并等待print('Done!')
:
pipe.stdin.close()
pipe.wait()
推荐阅读
- swift - 使用 pod 时重复 info.plist 文件
- android - 反应原生:上传 apk 后,Play 商店有权限问题(CAMERA,RECORD_AUDIO,...)
- php - 具有多个阵列的在线服务器检测
- php - 在 Woocommerce 中自定义我的帐户地址字段
- c# - 如何备份迁移文件夹
- android - 使用工作管理器获取远程异常
- vue.js - Buefy 自动完成搜索 在右侧添加清除图标?
- django - 如果已经创建了对象,则 Django CreateView 重定向到 UpdateView
- javascript - Node JS对后端API的多个并发请求
- java - 如何检索 ArrayList (Java) 的索引?