繁体   English   中英

使用 python 从罗技网络摄像头录制音频和视频的好方法

[英]good and simple way to record audio and video from logitech webcam with python

我正在寻找一个简单的解决方案来使用 python 从我的 Logitech 网络摄像头录制音频和视频。
我尝试使用 ffmpeg 但我无法让它正常工作。
另外,我在 windows 上使用它,所以该解决方案应该适用于 windows。

使用ffmpeg

使用 dshow (DirectShow) 输入列出设备:

ffmpeg -list_devices true -f dshow -i dummy

捕获视频和音频的示例命令:

ffmpeg -f dshow -i video="Camera name here":audio="Microphone name here" -vf format=yuv420p output.mp4

有关更多信息和示例,请参阅dshow 文档FFmpeg Wiki:DirectShow

如上所述,JRodrigoF 有一个解决方案,它使用 openCV 录制视频和 pyaudio 录制音频。 我在一个项目上使用了一段时间; 但是,我注意到有时线程会挂起,这会导致程序崩溃。 另一个问题是 openCV 无法以可靠的速率捕获视频帧,并且 ffmpeg 在重新编码时会扭曲视频。

我想出了一个新的解决方案,它记录得更可靠、质量更高。 但是,它仅适用于 Windows,因为它使用 pywinauto 和内置的 Windows 相机应用程序。 脚本的最后一位通过检查视频名称的时间戳来进行一些错误检查,以确认视频成功录制。

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