【问题标题】:How to capture a video (AND audio) in python, from a camera (or webcam)如何从相机(或网络摄像头)在 python 中捕获视频(和音频)
【发布时间】:2012-12-17 21:48:52
【问题描述】:

我正在寻找一个解决方案,无论是在 linux 中还是在 windows 中,让我能够

  • 同时从我的网络摄像头和麦克风录制视频(+音频)。
  • 将其保存为 file.AVI(或 mpg 或其他)
  • 在录制视频时在屏幕上显示视频

在我的情况下,压缩不是问题,我实际上更喜欢捕获 RAW 并稍后压缩它。

到目前为止,我已经使用 VB 中的一个 ActiveX 组件完成了它,它可以处理所有事情,我想继续使用 python(VB 解决方案不稳定、不可靠)。

到目前为止,我已经看到了仅捕获 VIDEO 或单个帧的代码...

我已经看过了

  • OpenCV - 在那里找不到音频捕获
  • PyGame - 无同步音频捕获 (AFAIK)
  • VideoCapture - 仅提供单帧。
  • SimpleCV - 无音频
  • VLC - 将 VideoLAN 程序绑定到 wxPthon - 希望它可以做到(仍在研究这个选项)
  • kivy - 刚听说过,没能在 Windows SO FAR 下运行。

问题 - 是否有适用于 python 的视频和音频捕获库?

或者 - 如果有的话,还有哪些其他选项?

【问题讨论】:

    标签: python video-capture


    【解决方案1】:

    回答:没有。python 中没有单一的库/解决方案可以同时进行视频/音频录制。您必须分别实现并以智能方式合并音频和视频信号,以最终生成视频/音频文件。

    我为您提出的问题找到了解决方案。我的代码解决了您的三个问题:

    • 同时录制来自网络摄像头和麦克风的视频和音频。
    • 它将最终的视频/音频文件保存为 .AVI
    • 取消注释第 76、77 和 78 行将使视频在录制时显示在屏幕上。

    我的解决方案使用pyaudio 进行音频录制,opencv 用于视频录制,ffmpeg 用于混合这两个信号。为了能够同时记录两者,我使用多线程。一个线程录制视频,第二个线程录制音频。我已经将我的代码上传到了 github,并且在这里也包含了它的所有重要部分。

    https://github.com/JRodrigoF/AVrecordeR

    注意:opencv 无法控制网络摄像头进行录制的 fps。它只能在文件的编码中指定所需的最终 fps,但网络摄像头通常会根据规格和光照条件(我发现)表现不同。所以fps要控制在代码层面。

    import cv2
    import pyaudio
    import wave
    import threading
    import time
    import subprocess
    import os
    
    class VideoRecorder():  
    
        # Video class based on openCV 
        def __init__(self):
    
            self.open = True
            self.device_index = 0
            self.fps = 6               # fps should be the minimum constant rate at which the camera can
            self.fourcc = "MJPG"       # capture images (with no decrease in speed over time; testing is required)
            self.frameSize = (640,480) # video formats and sizes also depend and vary according to the camera used
            self.video_filename = "temp_video.avi"
            self.video_cap = cv2.VideoCapture(self.device_index)
            self.video_writer = cv2.VideoWriter_fourcc(*self.fourcc)
            self.video_out = cv2.VideoWriter(self.video_filename, self.video_writer, self.fps, self.frameSize)
            self.frame_counts = 1
            self.start_time = time.time()
    
    
        # Video starts being recorded 
        def record(self):
    
    #       counter = 1
            timer_start = time.time()
            timer_current = 0
    
    
            while(self.open==True):
                ret, video_frame = self.video_cap.read()
                if (ret==True):
    
                        self.video_out.write(video_frame)
    #                   print str(counter) + " " + str(self.frame_counts) + " frames written " + str(timer_current)
                        self.frame_counts += 1
    #                   counter += 1
    #                   timer_current = time.time() - timer_start
                        time.sleep(0.16)
    #                   gray = cv2.cvtColor(video_frame, cv2.COLOR_BGR2GRAY)
    #                   cv2.imshow('video_frame', gray)
    #                   cv2.waitKey(1)
                else:
                    break
    
                    # 0.16 delay -> 6 fps
                    # 
    
    
        # Finishes the video recording therefore the thread too
        def stop(self):
    
            if self.open==True:
    
                self.open=False
                self.video_out.release()
                self.video_cap.release()
                cv2.destroyAllWindows()
    
            else: 
                pass
    
    
        # Launches the video recording function using a thread          
        def start(self):
            video_thread = threading.Thread(target=self.record)
            video_thread.start()
    
    
    
    
    
    class AudioRecorder():
    
    
        # Audio class based on pyAudio and Wave
        def __init__(self):
    
            self.open = True
            self.rate = 44100
            self.frames_per_buffer = 1024
            self.channels = 2
            self.format = pyaudio.paInt16
            self.audio_filename = "temp_audio.wav"
            self.audio = pyaudio.PyAudio()
            self.stream = self.audio.open(format=self.format,
                                          channels=self.channels,
                                          rate=self.rate,
                                          input=True,
                                          frames_per_buffer = self.frames_per_buffer)
            self.audio_frames = []
    
    
        # Audio starts being recorded
        def record(self):
    
            self.stream.start_stream()
            while(self.open == True):
                data = self.stream.read(self.frames_per_buffer) 
                self.audio_frames.append(data)
                if self.open==False:
                    break
    
    
        # Finishes the audio recording therefore the thread too    
        def stop(self):
    
            if self.open==True:
                self.open = False
                self.stream.stop_stream()
                self.stream.close()
                self.audio.terminate()
    
                waveFile = wave.open(self.audio_filename, 'wb')
                waveFile.setnchannels(self.channels)
                waveFile.setsampwidth(self.audio.get_sample_size(self.format))
                waveFile.setframerate(self.rate)
                waveFile.writeframes(b''.join(self.audio_frames))
                waveFile.close()
    
            pass
    
        # Launches the audio recording function using a thread
        def start(self):
            audio_thread = threading.Thread(target=self.record)
            audio_thread.start()
    
    
    
    
    
    def start_AVrecording(filename):
    
        global video_thread
        global audio_thread
    
        video_thread = VideoRecorder()
        audio_thread = AudioRecorder()
    
        audio_thread.start()
        video_thread.start()
    
        return filename
    
    
    
    
    def start_video_recording(filename):
    
        global video_thread
    
        video_thread = VideoRecorder()
        video_thread.start()
    
        return filename
    
    
    def start_audio_recording(filename):
    
        global audio_thread
    
        audio_thread = AudioRecorder()
        audio_thread.start()
    
        return filename
    
    
    
    
    def stop_AVrecording(filename):
    
        audio_thread.stop() 
        frame_counts = video_thread.frame_counts
        elapsed_time = time.time() - video_thread.start_time
        recorded_fps = frame_counts / elapsed_time
        print "total frames " + str(frame_counts)
        print "elapsed time " + str(elapsed_time)
        print "recorded fps " + str(recorded_fps)
        video_thread.stop() 
    
        # Makes sure the threads have finished
        while threading.active_count() > 1:
            time.sleep(1)
    
    
    #    Merging audio and video signal
    
        if abs(recorded_fps - 6) >= 0.01:    # If the fps rate was higher/lower than expected, re-encode it to the expected
    
            print "Re-encoding"
            cmd = "ffmpeg -r " + str(recorded_fps) + " -i temp_video.avi -pix_fmt yuv420p -r 6 temp_video2.avi"
            subprocess.call(cmd, shell=True)
    
            print "Muxing"
            cmd = "ffmpeg -ac 2 -channel_layout stereo -i temp_audio.wav -i temp_video2.avi -pix_fmt yuv420p " + filename + ".avi"
            subprocess.call(cmd, shell=True)
    
        else:
    
            print "Normal recording\nMuxing"
            cmd = "ffmpeg -ac 2 -channel_layout stereo -i temp_audio.wav -i temp_video.avi -pix_fmt yuv420p " + filename + ".avi"
            subprocess.call(cmd, shell=True)
    
            print ".."
    
    
    
    
    # Required and wanted processing of final files
    def file_manager(filename):
    
        local_path = os.getcwd()
    
        if os.path.exists(str(local_path) + "/temp_audio.wav"):
            os.remove(str(local_path) + "/temp_audio.wav")
    
        if os.path.exists(str(local_path) + "/temp_video.avi"):
            os.remove(str(local_path) + "/temp_video.avi")
    
        if os.path.exists(str(local_path) + "/temp_video2.avi"):
            os.remove(str(local_path) + "/temp_video2.avi")
    
        if os.path.exists(str(local_path) + "/" + filename + ".avi"):
            os.remove(str(local_path) + "/" + filename + ".avi")
    

    【讨论】:

    • 你知道这是否可以在 Python 3.x(最好是 3.4)下工作吗?
    • 如何支持mac os?
    • 我不知道这里使用的库是否也适用于python 3。
    • 不知道如何在 Mac 中向下滚动。对不起。这里也很重要。这个解决方案是一个非常严格的解决方案,涵盖了原始问题的问题和需求。有关使用 python 录制视频/音频的真正好的解决方案,请查看 ffmpeg。真正的魔力和最佳解决方案就在其中。
    【解决方案2】:

    对于上面提出的问题:是的,代码也应该在 Python3 下工作。我对其进行了一些调整,现在适用于 python2 和 python3(在 windows7 上使用 2.7 和 3.6 进行了测试,尽管您需要安装 ffmpeg 或至少在同一目录中安装可执行的 ffmpeg.exe,您可以在此处获取它:@ 987654321@)。当然,您还需要安装所有其他库 cv2、numpy、pyaudio,如下所示:

    pip install opencv-python numpy pyaudio
    

    您现在可以直接运行代码:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # VideoRecorder.py
    
    from __future__ import print_function, division
    import numpy as np
    import cv2
    import pyaudio
    import wave
    import threading
    import time
    import subprocess
    import os
    
    class VideoRecorder():  
        "Video class based on openCV"
        def __init__(self, name="temp_video.avi", fourcc="MJPG", sizex=640, sizey=480, camindex=0, fps=30):
            self.open = True
            self.device_index = camindex
            self.fps = fps                  # fps should be the minimum constant rate at which the camera can
            self.fourcc = fourcc            # capture images (with no decrease in speed over time; testing is required)
            self.frameSize = (sizex, sizey) # video formats and sizes also depend and vary according to the camera used
            self.video_filename = name
            self.video_cap = cv2.VideoCapture(self.device_index)
            self.video_writer = cv2.VideoWriter_fourcc(*self.fourcc)
            self.video_out = cv2.VideoWriter(self.video_filename, self.video_writer, self.fps, self.frameSize)
            self.frame_counts = 1
            self.start_time = time.time()
    
        def record(self):
            "Video starts being recorded"
            # counter = 1
            timer_start = time.time()
            timer_current = 0
            while self.open:
                ret, video_frame = self.video_cap.read()
                if ret:
                    self.video_out.write(video_frame)
                    # print(str(counter) + " " + str(self.frame_counts) + " frames written " + str(timer_current))
                    self.frame_counts += 1
                    # counter += 1
                    # timer_current = time.time() - timer_start
                    time.sleep(1/self.fps)
                    # gray = cv2.cvtColor(video_frame, cv2.COLOR_BGR2GRAY)
                    # cv2.imshow('video_frame', gray)
                    # cv2.waitKey(1)
                else:
                    break
    
        def stop(self):
            "Finishes the video recording therefore the thread too"
            if self.open:
                self.open=False
                self.video_out.release()
                self.video_cap.release()
                cv2.destroyAllWindows()
    
        def start(self):
            "Launches the video recording function using a thread"
            video_thread = threading.Thread(target=self.record)
            video_thread.start()
    
    class AudioRecorder():
        "Audio class based on pyAudio and Wave"
        def __init__(self, filename="temp_audio.wav", rate=44100, fpb=1024, channels=2):
            self.open = True
            self.rate = rate
            self.frames_per_buffer = fpb
            self.channels = channels
            self.format = pyaudio.paInt16
            self.audio_filename = filename
            self.audio = pyaudio.PyAudio()
            self.stream = self.audio.open(format=self.format,
                                          channels=self.channels,
                                          rate=self.rate,
                                          input=True,
                                          frames_per_buffer = self.frames_per_buffer)
            self.audio_frames = []
    
        def record(self):
            "Audio starts being recorded"
            self.stream.start_stream()
            while self.open:
                data = self.stream.read(self.frames_per_buffer) 
                self.audio_frames.append(data)
                if not self.open:
                    break
    
        def stop(self):
            "Finishes the audio recording therefore the thread too"
            if self.open:
                self.open = False
                self.stream.stop_stream()
                self.stream.close()
                self.audio.terminate()
                waveFile = wave.open(self.audio_filename, 'wb')
                waveFile.setnchannels(self.channels)
                waveFile.setsampwidth(self.audio.get_sample_size(self.format))
                waveFile.setframerate(self.rate)
                waveFile.writeframes(b''.join(self.audio_frames))
                waveFile.close()
    
        def start(self):
            "Launches the audio recording function using a thread"
            audio_thread = threading.Thread(target=self.record)
            audio_thread.start()
    
    def start_AVrecording(filename="test"):
        global video_thread
        global audio_thread
        video_thread = VideoRecorder()
        audio_thread = AudioRecorder()
        audio_thread.start()
        video_thread.start()
        return filename
    
    def start_video_recording(filename="test"):
        global video_thread
        video_thread = VideoRecorder()
        video_thread.start()
        return filename
    
    def start_audio_recording(filename="test"):
        global audio_thread
        audio_thread = AudioRecorder()
        audio_thread.start()
        return filename
    
    def stop_AVrecording(filename="test"):
        audio_thread.stop() 
        frame_counts = video_thread.frame_counts
        elapsed_time = time.time() - video_thread.start_time
        recorded_fps = frame_counts / elapsed_time
        print("total frames " + str(frame_counts))
        print("elapsed time " + str(elapsed_time))
        print("recorded fps " + str(recorded_fps))
        video_thread.stop() 
    
        # Makes sure the threads have finished
        while threading.active_count() > 1:
            time.sleep(1)
    
        # Merging audio and video signal
        if abs(recorded_fps - 6) >= 0.01:    # If the fps rate was higher/lower than expected, re-encode it to the expected
            print("Re-encoding")
            cmd = "ffmpeg -r " + str(recorded_fps) + " -i temp_video.avi -pix_fmt yuv420p -r 6 temp_video2.avi"
            subprocess.call(cmd, shell=True)
            print("Muxing")
            cmd = "ffmpeg -y -ac 2 -channel_layout stereo -i temp_audio.wav -i temp_video2.avi -pix_fmt yuv420p " + filename + ".avi"
            subprocess.call(cmd, shell=True)
        else:
            print("Normal recording\nMuxing")
            cmd = "ffmpeg -y -ac 2 -channel_layout stereo -i temp_audio.wav -i temp_video.avi -pix_fmt yuv420p " + filename + ".avi"
            subprocess.call(cmd, shell=True)
            print("..")
    
    def file_manager(filename="test"):
        "Required and wanted processing of final files"
        local_path = os.getcwd()
        if os.path.exists(str(local_path) + "/temp_audio.wav"):
            os.remove(str(local_path) + "/temp_audio.wav")
        if os.path.exists(str(local_path) + "/temp_video.avi"):
            os.remove(str(local_path) + "/temp_video.avi")
        if os.path.exists(str(local_path) + "/temp_video2.avi"):
            os.remove(str(local_path) + "/temp_video2.avi")
        # if os.path.exists(str(local_path) + "/" + filename + ".avi"):
        #     os.remove(str(local_path) + "/" + filename + ".avi")
    
    if __name__ == '__main__':
        start_AVrecording()
        time.sleep(5)
        stop_AVrecording()
        file_manager()
    

    【讨论】:

    • 对于 ubuntu,您还需要安装一些软件包; sudo apt-get install libasound-dev portaudio19-dev libportaudio2 libportaudiocpp0
    【解决方案3】:

    我会推荐 ffmpeg。有一个 python 包装器。

    http://code.google.com/p/pyffmpeg/

    【讨论】:

    • 我没有看到如何使用 ffmpeg 来显示正在录制的视频。 (例如,可以将 vlc 嵌入到 wxpython 中,或者作为独立的无边框窗口)
    【解决方案4】:

    我一直在寻找一个好的答案,我认为是GStreamer...

    python 绑定的文档非常简单,其中大部分似乎都围绕着旧的 0.10 版本的 GStreamer 而不是新的 1.X 版本,但 GStreamer 是一个非常强大的跨平台多媒体框架,可以流式传输、复用、转码和显示几乎所有内容。

    【讨论】:

      【解决方案5】:

      我在一个项目中使用了 JRodrigoF 的脚本一段时间。但是,我注意到有时线程会挂起,这会导致程序崩溃。另一个问题是 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/m/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]  # get last vid
          # 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)
      

      【讨论】:

        【解决方案6】:

        你可以做离线html,js代码来做视频和录音。使用 python lib python webview 打开该页面。它应该可以正常工作。

        【讨论】:

        【解决方案7】:

        我在使用 JRodrigoF 的解决方案时随机收到“[Errno -9999] 意外主机错误”,并发现这是由于竞争条件造成的,其中音频流可以在最后一次在 record() 中读取之前关闭录音机类。

        我稍作修改,以便在 while 循环之后完成所有关闭过程,并添加了一个函数 list_audio_devices(),它显示了可供选择的音频设备列表。我还添加了音频设备索引作为参数来选择音频设备。

        #!/usr/bin/env python
        # -*- coding: utf-8 -*-
        # VideoRecorder.py
        
        from __future__ import print_function, division
        import numpy as np
        import cv2
        import pyaudio
        import wave
        import threading
        import time
        import subprocess
        import os
        
        class VideoRecorder():
            "Video class based on openCV"
            def __init__(self, name="temp_video.avi", fourcc="MJPG", sizex=640, sizey=480, camindex=0, fps=30):
                self.open = True
                self.device_index = camindex
                self.fps = fps                  # fps should be the minimum constant rate at which the camera can
                self.fourcc = fourcc            # capture images (with no decrease in speed over time; testing is required)
                self.frameSize = (sizex, sizey) # video formats and sizes also depend and vary according to the camera used
                self.video_filename = name
                self.video_cap = cv2.VideoCapture(self.device_index)
                self.video_writer = cv2.VideoWriter_fourcc(*self.fourcc)
                self.video_out = cv2.VideoWriter(self.video_filename, self.video_writer, self.fps, self.frameSize)
                self.frame_counts = 1
                self.start_time = time.time()
        
            def record(self):
                "Video starts being recorded"
                # counter = 1
                timer_start = time.time()
                timer_current = 0
                while self.open:
                    ret, video_frame = self.video_cap.read()
                    if ret:
                        self.video_out.write(video_frame)
                        # print(str(counter) + " " + str(self.frame_counts) + " frames written " + str(timer_current))
                        self.frame_counts += 1
                        # counter += 1
                        # timer_current = time.time() - timer_start
                        time.sleep(1/self.fps)
                        # gray = cv2.cvtColor(video_frame, cv2.COLOR_BGR2GRAY)
                        # cv2.imshow('video_frame', gray)
                        # cv2.waitKey(1)
                    else:
                        break
        
            def stop(self):
                "Finishes the video recording therefore the thread too"
                if self.open:
                    self.open=False
                    self.video_out.release()
                    self.video_cap.release()
                    cv2.destroyAllWindows()
        
            def start(self):
                "Launches the video recording function using a thread"
                video_thread = threading.Thread(target=self.record)
                video_thread.start()
        
        class AudioRecorder():
            "Audio class based on pyAudio and Wave"
            def __init__(self, filename="temp_audio.wav", rate=44100, fpb=2**12, channels=1, audio_index=0):
                self.open = True
                self.rate = rate
                self.frames_per_buffer = fpb
                self.channels = channels
                self.format = pyaudio.paInt16
                self.audio_filename = filename
                self.audio = pyaudio.PyAudio()
                self.stream = self.audio.open(format=self.format,
                                              channels=self.channels,
                                              rate=self.rate,
                                              input=True,
                                              input_device_index=audio_index,
                                              frames_per_buffer = self.frames_per_buffer)
                self.audio_frames = []
        
            def record(self):
                "Audio starts being recorded"
                self.stream.start_stream()
                t_start = time.time_ns()
                while self.open:
                    try:
                        data = self.stream.read(self.frames_per_buffer)
                        self.audio_frames.append(data)
                    except Exception as e:
                        print('\n' + '*'*80)
                        print('PyAudio read exception at %.1fms\n' % ((time.time_ns() - t_start)/10**6))
                        print(e)
                        print('*'*80 + '\n')
                    time.sleep(0.01)
                self.stream.stop_stream()
                self.stream.close()
                self.audio.terminate()
                waveFile = wave.open(self.audio_filename, 'wb')
                waveFile.setnchannels(self.channels)
                waveFile.setsampwidth(self.audio.get_sample_size(self.format))
                waveFile.setframerate(self.rate)
                waveFile.writeframes(b''.join(self.audio_frames))
                waveFile.close()
        
            def stop(self):
                "Finishes the audio recording therefore the thread too"
                if self.open:
                    self.open = False
        
            def start(self):
                "Launches the audio recording function using a thread"
                audio_thread = threading.Thread(target=self.record)
                audio_thread.start()
        
        def start_AVrecording(filename="test", audio_index=0, sample_rate=44100):
            global video_thread
            global audio_thread
            video_thread = VideoRecorder()
            audio_thread = AudioRecorder(audio_index=audio_index, rate=sample_rate)
            audio_thread.start()
            video_thread.start()
            return filename
        
        def start_video_recording(filename="test"):
            global video_thread
            video_thread = VideoRecorder()
            video_thread.start()
            return filename
        
        def start_audio_recording(filename="test", audio_index=0, sample_rate=44100):
            global audio_thread
            audio_thread = AudioRecorder(audio_index=audio_index, rate=sample_rate)
            audio_thread.start()
            return filename
        
        def stop_AVrecording(filename="test"):
            audio_thread.stop()
            frame_counts = video_thread.frame_counts
            elapsed_time = time.time() - video_thread.start_time
            recorded_fps = frame_counts / elapsed_time
            print("total frames " + str(frame_counts))
            print("elapsed time " + str(elapsed_time))
            print("recorded fps " + str(recorded_fps))
            video_thread.stop()
        
            # Makes sure the threads have finished
            while threading.active_count() > 1:
                time.sleep(1)
        
            # Merging audio and video signal
            if abs(recorded_fps - 6) >= 0.01:    # If the fps rate was higher/lower than expected, re-encode it to the expected
                print("Re-encoding")
                cmd = "ffmpeg -r " + str(recorded_fps) + " -i temp_video.avi -pix_fmt yuv420p -r 6 temp_video2.avi"
                subprocess.call(cmd, shell=True)
                print("Muxing")
                cmd = "ffmpeg -y -ac 2 -channel_layout stereo -i temp_audio.wav -i temp_video2.avi -pix_fmt yuv420p " + filename + ".avi"
                subprocess.call(cmd, shell=True)
            else:
                print("Normal recording\nMuxing")
                cmd = "ffmpeg -y -ac 2 -channel_layout stereo -i temp_audio.wav -i temp_video.avi -pix_fmt yuv420p " + filename + ".avi"
                subprocess.call(cmd, shell=True)
                print("..")
        
        def file_manager(filename="test"):
            "Required and wanted processing of final files"
            local_path = os.getcwd()
            if os.path.exists(str(local_path) + "/temp_audio.wav"):
                os.remove(str(local_path) + "/temp_audio.wav")
            if os.path.exists(str(local_path) + "/temp_video.avi"):
                os.remove(str(local_path) + "/temp_video.avi")
            if os.path.exists(str(local_path) + "/temp_video2.avi"):
                os.remove(str(local_path) + "/temp_video2.avi")
            # if os.path.exists(str(local_path) + "/" + filename + ".avi"):
            #     os.remove(str(local_path) + "/" + filename + ".avi")
        
        def list_audio_devices(name_filter=None):
            pa = pyaudio.PyAudio()
            device_index = None
            sample_rate = None
            for x in range(pa.get_device_count()):
                info = pa.get_device_info_by_index(x)
                print(pa.get_device_info_by_index(x))
                if name_filter is not None and name_filter in info['name']:
                    device_index = info['index']
                    sample_rate = int(info['defaultSampleRate'])
                    break
            return device_index, sample_rate
        
        if __name__ == '__main__':
            start_AVrecording()
            time.sleep(5)
            stop_AVrecording()
            file_manager()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多