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()