【问题标题】:Python ffmpeg subprocess: Broken pipePython ffmpeg 子进程:断管
【发布时间】:2020-05-19 12:00:09
【问题描述】:

以下脚本使用 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!')

【问题讨论】:

    标签: python python-3.x ffmpeg subprocess


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2016-09-29
      • 2017-04-19
      • 2017-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-30
      • 2012-07-06
      相关资源
      最近更新 更多