python - Pipe raw OpenCV images to FFmpeg -
here's straightforward example of reading off web cam using opencv's python bindings:
'''capture.py''' import cv, sys cap = cv.capturefromcam(0) # 0 /dev/video0 while true : if not cv.grabframe(cap) : break frame = cv.retrieveframe(cap) sys.stdout.write( frame.tostring() )
now want pipe output ffmpeg in:
$ python capture.py | ffmpeg -f image2pipe -pix_fmt bgr8 -i - -s 640x480 foo.avi
sadly, can't ffmpeg magic incantation quite right , fails with
libavutil 50.15. 1 / 50.15. 1 libavcodec 52.72. 2 / 52.72. 2 libavformat 52.64. 2 / 52.64. 2 libavdevice 52. 2. 0 / 52. 2. 0 libavfilter 1.19. 0 / 1.19. 0 libswscale 0.11. 0 / 0.11. 0 libpostproc 51. 2. 0 / 51. 2. 0 output #0, avi, 'out.avi': stream #0.0: video: flv, yuv420p, 640x480, q=2-31, 19660 kb/s, 90k tbn, 30 tbc [image2pipe @ 0x1508640]max_analyze_duration reached [image2pipe @ 0x1508640]estimating duration bitrate, may inaccurate input #0, image2pipe, 'pipe:': duration: n/a, bitrate: n/a stream #0.0: video: 0x0000, bgr8, 25 fps, 25 tbr, 25 tbn, 25 tbc swscaler: 0x0 -> 640x480 invalid scaling dimension
- the captured frames 640x480.
- i'm pretty sure pixel order opencv image type (iplimage) gbr, 1 byte per channel. @ least, that's seems coming off camera.
i'm no ffmpeg guru. has done successfully?
took bunch of fiddling figured out using ffmpeg rawvideo demuxer:
python capture.py | ffmpeg -f rawvideo -pixel_format bgr24 -video_size 640x480 -framerate 30 -i - foo.avi
since there no header in raw video specifying assumed video parameters, user must specify them in order able decode data correctly:
-framerate
set input video frame rate. default value 25.-pixel_format
set input video pixel format. default value yuv420p.-video_size
set input video size. there no default, value must specified explicitly.
and here's little power users. same thing using vlc stream live output 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 webm stream using ffmpeg , ffserver
python capture.py | ffmpeg -f rawvideo -pixel_format rgb24 -video_size 640x480 -framerate 25 -i - http://localhost:8090/feed1.ffm
Comments
Post a Comment