2

Hello everyone I'm good at a project to develop using the PyAudio library, also using the webcam to capture video, at the time I managed to record the video and audio in two separate files, but I have the problem when the The user pauses the recording and then resumes the process, the video is stored without any problem but the audio is only recorded until the pause signal is given.

While the user did not put the video both the audio file and video match, my problem is how to pause the audio recording and then resume it I hope someone can help me attached the code of my work

import cv2
import numpy as np
from datetime import datetime
import gtk
import keyboard

import pyaudio
import wave
import sys

flagrecord=True
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100


def show_webcam(flagrecord):

    cam = cv2.VideoCapture(0)
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    frame_width = int(cam.get(3))
    frame_height = int(cam.get(4))
    FONT = cv2.FONT_HERSHEY_PLAIN
    filename = datetime.now().strftime("%Y-%m-%d_%H.%M.%S") + ".avi"
    filenamea = datetime.now().strftime("%Y-%m-%d_%H.%M.%S") 

    p = pyaudio.PyAudio()
    stream = p.open(format = FORMAT,
                    channels = CHANNELS,
                    rate = RATE,
                    input = True,
                    frames_per_buffer = chunk)
    all = []

    out = cv2.VideoWriter(filename,fourcc, 30, (frame_width,frame_height))

    while True:

        ret_val, img = cam.read()
        title = datetime.now().strftime("%Y-%m-%d*%H:%M:%S")

        if flagrecord: 

            img = cv2.flip(img,1)
            cv2.putText(img, "REC", (40,40), FONT, 3 , (0,0,255), 2)
            cv2.circle(img, (20,20), 10 , (0,0,255), -1)
            cv2.rectangle(img, (30,430),(600,480),(0,0,0), -1)
            cv2.putText(img, title, (40,470), FONT, 3 , (255,255,255), 2)
            cv2.imshow('Grabacion de Audiencias', img)
            out.write(img)

            stream.start_stream()
            data = stream.read(chunk)
            all.append(data)

        else:

            img = cv2.flip(img,1)
            cv2.putText(img, "PAUSE", (40,40), FONT, 3 , (255,0,0), 2)
            cv2.circle(img, (20,20), 10 , (255,0,0), -1)
            cv2.rectangle(img, (50,430),(570,480),(0,0,0), -1)
            cv2.putText(img, "Audiencias En Pausa", (60,470), FONT, 3 , (255,255,255), 2)
            cv2.imshow('Grabacion de Audiencias', img)
            stream.stop_stream()

        if cv2.waitKey(1) == 27: 
            break  
        if keyboard.is_pressed('p'):
            flagrecord=False
        if keyboard.is_pressed('c'):
            flagrecord=True 
        if keyboard.is_pressed('q'):
            break     

    cam.release()        
    out.release()        
    cv2.destroyAllWindows()

    data = ''.join(all)
    wf = wave.open(filenamea, 'wb')
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(p.get_sample_size(FORMAT))
    wf.setframerate(RATE)
    wf.writeframes(data)
    wf.close()


def main():
    show_webcam(mirror=True)


if __name__ == '__main__':
    main()
mikey
  • 46
  • 5

1 Answers1

1

Hello everyone I managed to pause the audio and continue with the video I have a delay of 2 seconds but I think it is a matter of improving the frames annex the code of the solution

Complete Code

import cv2
import numpy as np
from datetime import datetime
import gtk
import keyboard

import pyaudio
import wave
import sys

flagrecord=True
#chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
ropen=True
chunk = int(RATE/20) 


def show_webcam(flagrecord):

    cam = cv2.VideoCapture(0)
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    frame_width = int(cam.get(3))
    frame_height = int(cam.get(4))
    FONT = cv2.FONT_HERSHEY_PLAIN
    filename = datetime.now().strftime("%Y-%m-%d_%H.%M.%S") + ".avi"
    filenamea = datetime.now().strftime("%Y-%m-%d_%H.%M.%S") 


    p = pyaudio.PyAudio()
    stream = p.open(format = FORMAT,
                    channels = CHANNELS,
                    rate = RATE,
                    input = True,
                    frames_per_buffer = chunk)

    out = cv2.VideoWriter(filename,fourcc, 20, (frame_width,frame_height))

    all = []
    aux = []
    stream.start_stream()

    flagaudio=False

    while True:

        ret_val, img = cam.read()
        title = datetime.now().strftime("%Y-%m-%d*%H:%M:%S")

        if flagrecord: 

            img = cv2.flip(img,1)
            cv2.putText(img, "REC", (40,40), FONT, 3 , (0,0,255), 3)
            cv2.circle(img, (20,20), 10 , (0,0,255), -1)
            cv2.rectangle(img, (30,430),(600,480),(0,0,0), -1)
            cv2.putText(img, title, (40,470), FONT, 3 , (255,255,255), 2)
            cv2.imshow('Grabacion de Audiencias', img)
            data = stream.read(chunk)
            aux.append(data)
            out.write(img)

        else:

            img = cv2.flip(img,1)
            cv2.putText(img, "PAUSE", (40,40), FONT, 3 , (255,0,0), 3)
            cv2.circle(img, (20,20), 10 , (255,0,0), -1)
            cv2.rectangle(img, (50,430),(570,480),(0,0,0), -1)
            cv2.putText(img, "Audiencias En Pausa", (60,470), FONT, 3 , (255,0,0), 2)
            cv2.imshow('Grabacion de Audiencias', img)

            if flagaudio:
               all+=aux
               del  aux[:]
               data= 0
               stream.stop_stream()
            else:
               pass


        q=cv2.waitKey(1)
        if q == 27:
            break  
        if q == ord('p'):
            flagrecord=False
            flagaudio = True
        if q == ord('c'):
            flagrecord=True
            flagaudio=False
            stream.start_stream()
        if q == ord('q'):
            break  

    cam.release()        
    out.release()        
    cv2.destroyAllWindows()

    stream.close()
    p.terminate()
    all+=aux
    data = ''.join(all)
    wf = wave.open(filenamea, 'wb')
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(p.get_sample_size(FORMAT))
    wf.setframerate(RATE)
    wf.writeframes(data)
    wf.close()




def main():
    show_webcam(mirror=True)


if __name__ == '__main__':
    main()

First step

flagrecord=True
#chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
ropen=True
chunk = int(RATE/20) 

The variable chunk has to be divided RATE / 20 where 20 is the number of frames, since we are going to record continuously and there is no limit for time

I found this solution in this post

Continuesly streaming audio signal real time infinitely, Python

Second step

all = []
aux = []
stream.start_stream()
flagaudio=False

we create a lita aux start stream.start_stream () and create the variable flagaudio = False that starts false so that it does not enter within the first cycle

Third step

if flagrecord: 

            img = cv2.flip(img,1)
            cv2.putText(img, "REC", (40,40), FONT, 3 , (0,0,255), 3)
            cv2.circle(img, (20,20), 10 , (0,0,255), -1)
            cv2.rectangle(img, (30,430),(600,480),(0,0,0), -1)
            cv2.putText(img, title, (40,470), FONT, 3 , (255,255,255), 2)
            cv2.imshow('Grabacion de Audiencias', img)
            data = stream.read(chunk)
            aux.append(data)
            out.write(img)

        else:

            img = cv2.flip(img,1)
            cv2.putText(img, "PAUSE", (40,40), FONT, 3 , (255,0,0), 3)
            cv2.circle(img, (20,20), 10 , (255,0,0), -1)
            cv2.rectangle(img, (50,430),(570,480),(0,0,0), -1)
            cv2.putText(img, "Audiencias En Pausa", (60,470), FONT, 3 , (255,0,0), 2)
            cv2.imshow('Grabacion de Audiencias', img)

            if flagaudio:
               all+=aux
               del  aux[:]
               data= 0
               stream.stop_stream()
            else:
               pass

Within the flagrecord if the video is paused when it goes to false, the condition is placed where it asks if the flagaudio variable is true if this is the list list all takes the values ​​of aux, the list is deleted so that in the next cycle it takes the new values ​​of audio for security it is said that the data variable takes the value of 0 and with stream.stop_stream () we stop the recording of the audio

Fourth step

q=cv2.waitKey(1)
        if q == 27:
            break  
        if q == ord('p'):
            flagrecord=False
            flagaudio = True
        if q == ord('c'):
            flagrecord=True
            flagaudio=False
            stream.start_stream()
        if q == ord('q'):
            break  

With the use of q = cv2.waitKey (1) we control the pause order (letter p), continuous recording (letter c) audio and video and exit (letter q), in each order we indicate the variables flag that we create so that they allow the entrance or not to one of the control conditionals

To end

cam.release()        
    out.release()        
    cv2.destroyAllWindows()

    stream.close()
    p.terminate()
    all+=aux
    data = ''.join(all)
    wf = wave.open(filenamea, 'wb')
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(p.get_sample_size(FORMAT))
    wf.setframerate(RATE)
    wf.writeframes(data)
    wf.close()

We finished our recording process both video and audio and closed the webcam.

At this moment only I am presented with a three-second audio loss to be exact but the main thing that was pausing and continuing the recording works correctly, I think that my failure is in the control of the frame and the cycle.

I hope my solution will be of help and the explanation that has given

mikey
  • 46
  • 5