简体   繁体   中英

Pipe video frame to OpenCV image and then to FFmpeg

There is a similar question here: Getting 'av_interleaved_write_frame(): Broken pipe' error

But what should I do if I want to write the data?

I put pipe_out.stdin.write(image.tostring()) in the while loop, like this

FFMPEG_BIN = "/home/media/Downloads/ffmpeg"
import subprocess as sp
import sys
width = 360
height = 240
command_in = [ FFMPEG_BIN,
            '-i', '/home/media/Videos/mytestvideo/zhou.avi',
            '-f', 'image2pipe',
            '-pix_fmt', 'bgr24',
            '-vcodec', 'rawvideo', '-']
pipe_in = sp.Popen(command_in, stdout = sp.PIPE, bufsize = 10**8)

command_out = [ FFMPEG_BIN,
        '-y', # (optional) overwrite output file if it exists
        '-f', 'rawvideo',
        '-vcodec','rawvideo',
        '-s', '360x240', # size of one frame
        '-pix_fmt', 'bgr24',
        '-r', '28', # frames per second
        '-i', '-', # The imput comes from a pipe
        '-an', # Tells FFMPEG not to expect any audio
        #'-vcodec', 'mpeg',
        'my_output_videofile.mp4' ]

pipe_out = sp.Popen( command_out, stdin=sp.PIPE, stderr=sp.PIPE)

import numpy
import cv2
import pylab
# read width*height*3 bytes (= 1 frame)
while True:
    raw_image = pipe_in.stdout.read(width*height*3)
    image =  numpy.fromstring(raw_image, dtype='uint8')
    image = image.reshape((height,width,3))
    pipe_in.communicate()
    pipe_out.stdin.write(image.tostring())
    pipe_out.communicate()
    pipe_in.stdout.flush()
    #cv2.imshow('image',image)
    #cv2.waitKey(0)
    # throw away the data in the pipe's buffer.


'''
pipe_in.stdin.close()
pipe_in.stderr.close()
pipe_in.wait()
pipe_out.stdout.close()
pipe_out.stderr.close()
pipe_out.wait()
'''
#pipe_out.stdin.write(image.tostring())

However, the output video has only 1 frame(the first frame of input video)

Any ideas?

Thanks!

@Pureheart, try something like this:

import numpy
import cv2
import pylab
# read width*height*3 bytes (= 1 frame)
while True:
    t_end = time.time() + 15
    while time.time() < t_end:
        raw_image = pipe_in.stdout.read(width*height*3)
        image =  numpy.fromstring(raw_image, dtype='uint8')
        image = image.reshape((height,width,3))
        pipe_in.communicate()
        pipe_out.stdin.write(image.tostring())
        pipe_out.communicate()
        pipe_in.stdout.flush()

    proc.stdin.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM