Pipe raw OpenCV images to FFmpeg Pipe raw OpenCV images to FFmpeg python python

Pipe raw OpenCV images to FFmpeg


Took a bunch of fiddling but I figured it out using the FFmpeg rawvideo demuxer:

python capture.py | ffmpeg -f rawvideo -pixel_format bgr24 -video_size 640x480 -framerate 30 -i - foo.avi

Since there is no header in raw video specifying the assumed video parameters, the user must specify them in order to be able to decode the data correctly:

  • -framerate Set input video frame rate. Default value is 25.
  • -pixel_format Set the input video pixel format. Default value is yuv420p.
  • -video_size Set the input video size. There is no default, so this value must be specified explicitly.

And here's a little something extra for the power users. Same thing but using VLC to stream the live output to the web, Flash format:

python capture.py | cvlc --demux=rawvideo --rawvid-fps=30 --rawvid-width=320 --rawvid-height=240  --rawvid-chroma=RV24 - --sout "#transcode{vcodec=h264,vb=200,fps=30,width=320,height=240}:std{access=http{mime=video/x-flv},mux=ffmpeg{mux=flv},dst=:8081/stream.flv}"

Edit:Create a webm stream using ffmpeg and ffserver

python capture.py | ffmpeg -f rawvideo -pixel_format rgb24 -video_size 640x480 -framerate 25 -i - http://localhost:8090/feed1.ffm


I'm Kind of late, But my powerful VidGear Python Library automates the process of pipelining OpenCV frames into FFmpeg on any platform. Here's a basic python example:

# import librariesfrom vidgear.gears import WriteGearimport cv2output_params = {"-vcodec":"libx264", "-crf": 0, "-preset": "fast"} #define (Codec,CRF,preset) FFmpeg tweak parameters for writerstream = cv2.VideoCapture(0) #Open live webcam video stream on first index(i.e. 0) devicewriter = WriteGear(output_filename = 'Output.mp4', compression_mode = True, logging = True, **output_params) #Define writer with output filename 'Output.mp4' # infinite loopwhile True:    (grabbed, frame) = stream.read()    # read frames    # check if frame empty    if not is grabbed:        #if True break the infinite loop        break    # {do something with frame here}        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)    # write a modified frame to writer        writer.write(gray)         # Show output window    cv2.imshow("Output Frame", frame)    key = cv2.waitKey(1) & 0xFF    # check for 'q' key-press    if key == ord("q"):        #if 'q' key-pressed break out        breakcv2.destroyAllWindows()# close output windowstream.release()# safely close video streamwriter.close()# safely close writer

Source: https://github.com/abhiTronix/vidgear/wiki/Compression-Mode:-FFmpeg#2-writegear-apicompression-mode-with-opencv-directly

You can check out VidGear Docs for more advanced applications and features.

Hope that helps!


Not sure if this is Mac OS-specific, or python3-specific, but I needed to cast the frame to a string in order for this to work for me, like so:

sys.stdout.write(str(frame.tostring()))