简体   繁体   English

Python网络摄像头记录

[英]Python webcam record

I want to create a webcam streaming app that records webcam stream for, say about 30 seconds, and save it as myFile.wmv .我想创建一个记录网络摄像头流的网络摄像头流应用程序,比如大约 30 秒,并将其保存为myFile.wmv Now To get live camera feed I know this code :-现在要获得实时摄像头馈送,我知道此代码:-

import cv2
import numpy as np
c = cv2.VideoCapture(0)

while(1):
    _,f = c.read()
    cv2.imshow('e2',f)
    if cv2.waitKey(5)==27:
        break
cv2.destroyAllWindows()

But I have no idea off how to record for a given number of seconds and save it as a file in its current directory,但我不知道如何记录给定的秒数并将其保存为当前目录中的文件,

Please someone point me to the right direction请有人指出我正确的方向

Thanks谢谢

ABOUT TIME关于时间

Why do not use the python time function ?为什么不使用python时间函数 In particular time.time() Look at this answer about time in python特别是time.time()看看这个关于 python 时间的答案

NB OpenCV should have (or had) its own timer but I can not tell you for sure if it works in current versions. NB OpenCV 应该有(或有)自己的计时器,但我不能确定它是否适用于当前版本。

ABOUT RECORDING/SAVING关于录音/保存

Look at this other answer看看这个另一个答案

OpenCV allows you to record video, but not audio. OpenCV 允许您录制视频,但不能录制音频。 There is this script I came across from JRodrigoF that uses openCV to record video and pyaudio to record audio.我从 JRodrigoF 遇到了这个脚本,它使用 openCV 录制视频,使用 pyaudio 录制音频。 I used it for a while on a similar project;我在一个类似的项目中使用了一段时间; however, I noticed that sometimes the threads would hang and it would cause the program to crash.但是,我注意到有时线程会挂起并导致程序崩溃。 Another issue is that openCV does not capture video frames at a reliable rate and ffmpeg would distort the video when re-encoding.另一个问题是 openCV 无法以可靠的速率捕获视频帧,并且 ffmpeg 在重新编码时会使视频失真。

https://github.com/JRodrigoF/AVrecordeR https://github.com/JRodrigoF/AVrecordeR

I came up with a new solution that records much more reliably and with much higher quality.我想出了一个新的解决方案,它的记录更可靠,质量更高。 It presently only works for Windows because it uses pywinauto and the built-in Windows Camera app.它目前仅适用于 Windows,因为它使用pywinauto和内置的 Windows 相机应用程序。 The last bit of the script does some error-checking to confirm the video successfully recorded by checking the timestamp of the name of the video.脚本的最后一点会进行一些错误检查,通过检查视频名称的时间戳来确认视频已成功录制。

https://gist.github.com/mjdargen/956cc968864f38bfc4e20c9798c7d670 https://gist.github.com/mjdargen/956cc968864f38bfc4e20c9798c7d670

import pywinauto
import time
import subprocess
import os
import datetime

def win_record(duration):
    subprocess.run('start microsoft.windows.camera:', shell=True)  # open camera app

    # focus window by getting handle using title and class name
    # subprocess call opens camera and gets focus, but this provides alternate way
    # t, c = 'Camera', 'ApplicationFrameWindow'
    # handle = pywinauto.findwindows.find_windows(title=t, class_name=c)[0]
    # # get app and window
    # app = pywinauto.application.Application().connect(handle=handle)
    # window = app.window(handle=handle)
    # window.set_focus()  # set focus
    time.sleep(2)  # have to sleep

    # take control of camera window to take video
    desktop = pywinauto.Desktop(backend="uia")
    cam = desktop['Camera']
    # cam.print_control_identifiers()
    # make sure in video mode
    if cam.child_window(title="Switch to Video mode", auto_id="CaptureButton_1", control_type="Button").exists():
        cam.child_window(title="Switch to Video mode", auto_id="CaptureButton_1", control_type="Button").click()
    time.sleep(1)
    # start then stop video
    cam.child_window(title="Take Video", auto_id="CaptureButton_1", control_type="Button").click()
    time.sleep(duration+2)
    cam.child_window(title="Stop taking Video", auto_id="CaptureButton_1", control_type="Button").click()

    # retrieve vids from camera roll and sort
    dir = 'C:/Users/michael.dargenio/Pictures/Camera Roll'
    all_contents = list(os.listdir(dir))
    vids = [f for f in all_contents if "_Pro.mp4" in f]
    vids.sort()
    vid = vids[-1]
    # compute time difference
    vid_time = vid.replace('WIN_', '').replace('_Pro.mp4', '')
    vid_time = datetime.datetime.strptime(vid_time, '%Y%m%d_%H_%M_%S')
    now = datetime.datetime.now()
    diff = now - vid_time
    # time different greater than 2 minutes, assume something wrong & quit
    if diff.seconds > 120:
        quit()
    
    subprocess.run('Taskkill /IM WindowsCamera.exe /F', shell=True)  # close camera app
    print('Recorded successfully!')


win_record(2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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