在linux上使用usb摄像头进行录像的尝试

2024-07-06

最后的结果:录音用了 arecord,录像用了ffmpeg.


开始的时候录像是用opencv的,对opencv这个库掌握的不行,录制出来视频的时间和实际的时间不一致,

开始用了 

VideoWriter.FourCC(@"XVID"), 20, new Size(640, 480), true);

结果实际上10分钟的时间,但是视频出来却有12分钟对的时间,然后在stackoverflow上求助,告诉我


    frame rates are an illusion. video works with "presentation timestamps" -- OpenCV is the wrong library for this. 

    – Christoph Rackwitz

     Commented2 days ago 

  • You need to ensure that you actually capture 20 frames per second (at consistent intervals). It's not enough to just say so (as in specifying it as a parameter of VideoWriter), which is what you've done so far. 

    – Dan Mašek

     Commented2 days ago

  • webcams may not accept 20 fps. maybe. maybe not. for some reason, lots of people think "20" is a good value... must be some blog post or youtube video somewhere. you should work with 15, 25, 30, which are common values. -- whatever you do, DO NOT SLEEP to attempt to throttle the camera. people try that. it's always wrong. 

    – Christoph Rackwitz

     Commented2 days ago 


20这个参数不对,然后我试了25,开始时是对的。然后快到下班时,找了两个视频文件,发现一个9分14秒,一个9分29秒。

最后网上搜了一下,好像普遍有这个问题,也没有方便的解决方法,最后决定用ffmpeg。

ffmpeg 有个 -t 的参数,后面跟秒,就是录制几秒。10分钟就是600秒

 ffmpeg -f video4linux2 -video_size 640x480 -t 600 -i /dev/video0 -vcodec libx264 -pix_fmt yuv420p 1.h264

用这个命令,c#用 process类调用,封装了一下,多少秒的话按照启动时间到下一个10分钟的时间,屏幕尺寸开始用1280x720,然后帧数变少了,应该时usb摄像头不够高级,切换到640x480就流畅多了。


声音在c#及linux下根本找不到可以用的库,NAudio,还有一个库,都是只支持Windows录制,最后直接用了arecord进行录音,还是用c#用process类调用,有个-d 的参数,后面还是跟秒数,比如

arecord -d5 -Dhw:1,0 -r48000 -fS16_LE -traw -t wav test.wav


最后c#一个类了启动了三个线程循环,一个录制视频,一个录制音频,再一个检查磁盘空间,超过限定的阈值就清理最早的文件。视频和音频都是10分钟一个文件。


买的是一个usb摄像头,带麦克风的。接在一个小主机的usb口,再插上一个usb移动硬盘进行存储。


联系信息

QQ:1827566828
Email: 1827566828@qq.com
Web: https://www.yynet.wang

留言