python利用opencv保存、播放视频
代码已上传至:https://gitee.com/tqbx/python-opencv/tree/master/Getting_started_videos
目标
学习读取视频,播放视频,保存视频。
学习从相机中捕捉帧并展示。
学习cv2.VideoCapture(),cv2.VideoWriter()的使用
从相机中捕捉视频
通过自带摄像头捕捉视频,并将其转化为灰度视频显示出来。
基本步骤如下:
1.首先创建一个VideoCapture对象,它的参数包含两种:
- 设备索引,指定摄像机的编号。
- 视频文件的名称。
2.逐帧捕捉。
3.释放捕捉物。
importnumpyasnp importcv2ascv cap=cv.VideoCapture(0) ifnotcap.isOpened(): print("Cannotopencamera") exit() whileTrue: #Captureframe-by-frame ret,frame=cap.read() #ifframeisreadcorrectlyretisTrue ifnotret: print("Can'treceiveframe(streamend?).Exiting...") break #Ouroperationsontheframecomehere gray=cv.cvtColor(frame,cv.COLOR_BGR2GRAY) #Displaytheresultingframe cv.imshow('frame',gray) ifcv.waitKey(1)==ord('q'): break #Wheneverythingdone,releasethecapture cap.release() cv.destroyAllWindows()
其他:
- cap.read()返回布尔值,如果frame读取正确,为True,可以通过这个值判断视频是否已经结束。
- 有时,cap可能会初始化捕获失败,可以通过cap.isOpened()来检查其是否被初始化,如果为True那是最好,如果不是,可以使用cap.open()来尝试打开它。
- 当然,你可以使用cap.get(propId)的方式获取视频的一些属性,如帧的宽度,帧的高度,帧速等。propId是0-18的数字,每个数字代表一个属性,对应关系见底部附录。
- 既然可以获取,当然也可以尝试设置,假设想要设置帧的宽度和高度为320和240:cap.set(3,320),cap.set(4,240)。
从文件中播放视频
代码和从相机中捕获视频基本相同,不同之处在于传入VideoCapture的参数,此时传入视频文件的名称。
在显示每一帧的时候,可以使用cv2.waitKey()设置适当的时间,如果值很小,视频将会很快。正常情况下,25ms就ok。
importnumpyasnp importcv2 cap=cv2.VideoCapture('vtest.avi') while(cap.isOpened()): ret,frame=cap.read() gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) cv2.imshow('frame',gray) ifcv2.waitKey(1)&0xFF==ord('q'): break cap.release() cv2.destroyAllWindows()
保存视频
1.创建一个VideoWriter对象,指定如下参数:
- 输出的文件名,如output.avi。
- FourCCcode。
- 每秒的帧数fps。
- 帧的size。
2.FourCCcode传递有两种方式:
- fourcc=cv2.VideoWriter_fourcc(*'XVID')
- fourcc=cv2.VideoWriter_fourcc('X','V','I','D')
3.FourCC是一个用于指定视频编解码器的4字节代码。
- InFedora:DIVX,XVID,MJPG,X264,WMV1,WMV2.(XVIDismorepreferable.MJPGresultsinhighsizevideo.X264givesverysmallsizevideo)
- InWindows:DIVX(Moretobetestedandadded)
- InOSX:(Idon'thaveaccesstoOSX.Cansomeonefillthis?)
importnumpyasnp importcv2 cap=cv2.VideoCapture(0) #DefinethecodecandcreateVideoWriterobject fourcc=cv2.VideoWriter_fourcc(*'XVID') out=cv2.VideoWriter('output.avi',fourcc,20.0,(640,480)) while(cap.isOpened()): ret,frame=cap.read() ifret==True: frame=cv2.flip(frame,0) #writetheflippedframe out.write(frame) cv2.imshow('frame',frame) ifcv2.waitKey(1)&0xFF==ord('q'): break else: break #Releaseeverythingifjobisfinished cap.release() out.release() cv2.destroyAllWindows()
附录
- CV_CAP_PROP_POS_MSECCurrentpositionofthevideofileinmillisecondsorvideocapturetimestamp.
- CV_CAP_PROP_POS_FRAMES0-basedindexoftheframetobedecoded/capturednext.
- CV_CAP_PROP_POS_AVI_RATIORelativepositionofthevideofile:0-startofthefilm,1-endofthefilm.
- CV_CAP_PROP_FRAME_WIDTHWidthoftheframesinthevideostream.
- CV_CAP_PROP_FRAME_HEIGHTHeightoftheframesinthevideostream.
- CV_CAP_PROP_FPSFramerate.
- CV_CAP_PROP_FOURCC4-charactercodeofcodec.
- CV_CAP_PROP_FRAME_COUNTNumberofframesinthevideofile.
- CV_CAP_PROP_FORMATFormatoftheMatobjectsreturnedbyretrieve().
- CV_CAP_PROP_MODEBackend-specificvalueindicatingthecurrentcapturemode.
- CV_CAP_PROP_BRIGHTNESSBrightnessoftheimage(onlyforcameras).
- CV_CAP_PROP_CONTRASTContrastoftheimage(onlyforcameras).
- CV_CAP_PROP_SATURATIONSaturationoftheimage(onlyforcameras).
- CV_CAP_PROP_HUEHueoftheimage(onlyforcameras).
- CV_CAP_PROP_GAINGainoftheimage(onlyforcameras).
- CV_CAP_PROP_EXPOSUREExposure(onlyforcameras).
- CV_CAP_PROP_CONVERT_RGBBooleanflagsindicatingwhetherimagesshouldbeconvertedtoRGB.
- CV_CAP_PROP_WHITE_BALANCE_UTheUvalueofthewhitebalancesetting(note:onlysupportedbyDC1394v2.xbackendcurrently)
- CV_CAP_PROP_WHITE_BALANCE_VTheVvalueofthewhitebalancesetting(note:onlysupportedbyDC1394v2.xbackendcurrently)
- CV_CAP_PROP_RECTIFICATIONRectificationflagforstereocameras(note:onlysupportedbyDC1394v2.xbackendcurrently)
- CV_CAP_PROP_ISO_SPEEDTheISOspeedofthecamera(note:onlysupportedbyDC1394v2.xbackendcurrently)
- CV_CAP_PROP_BUFFERSIZEAmountofframesstoredininternalbuffermemory(note:onlysupportedbyDC1394v2.xbackendcurrently)
参考阅读
GettingStartedwithVideos
作者:天乔巴夏丶
出处:https://www.cnblogs.com/summerday152/
本文已收录至Gitee:https://gitee.com/tqbx/JavaBlog
若有兴趣,可以来参观本人的个人小站:https://www.hyhwky.com
以上就是python利用opencv保存、播放视频的详细内容,更多关于pythonopencv的资料请关注毛票票其它相关文章!