python 实现指定时间段录制视频

tech2024-04-02  71

python 实现指定时间段录制视频

一、pyaudio:二、cv2:指定时间录制:

一、pyaudio:

windows下使用 python -m pip install pyaudio 安装 mac 下使用 HomeBrew安装: brew install portaudio, pip install pyaudio linux 下使用 sudo apt-get install python-pyaudio python3-pyaudio 下面展示一些 代码片段。

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

二、cv2:

windows下使用 python -m pip install opencv-python 安装 mac 下使用 pip install opencv-python linux 下使用 pip install opencv-python

下面展示一些 代码。

class VideoRecorder(): "Video class based on openCV"#sizex=640, sizey=480, camindex=0, fps=30 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.frameSize = (int(self.video_cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(self.video_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) 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 class VideoRecorder(): "Video class based on openCV"#sizex=640, sizey=480, camindex=0, fps=30 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.frameSize = (int(self.video_cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(self.video_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) 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

指定时间录制:

下面展示一些 代码。

parser = argparse.ArgumentParser() parser.add_argument('--starttime', '-s', type=int, default='020000') parser.add_argument('--endtime', '-e', type=int, default='050000') args = parser.parse_args() starttime = args.starttime endtime = args.endtime while time.strftime('%H%M%S', time.localtime(time.time()))!=str(starttime): if int(time.strftime('%H', time.localtime(time.time())))<=int(str(starttime)[:2]): data_definit = int(str(starttime)[:2])*3600+int(str(starttime)[2:4])*60+int(str(starttime)[4:6]) data_current = int(time.strftime('%H', time.localtime(time.time())))*3600+int(time.strftime('%M', time.localtime(time.time())))*60+\ int(time.strftime('%S', time.localtime(time.time()))) print('距离启动录像还有{}秒'.format(data_definit-data_current)) time.sleep(1) else: data_definit = int(str(starttime)[:2])*3600+int(str(starttime)[2:4])*60+int(str(starttime)[4:6])+24*3600 data_current = int(time.strftime('%H', time.localtime(time.time())))*3600+int(time.strftime('%M', time.localtime(time.time())))*60+\ int(time.strftime('%S', time.localtime(time.time()))) print('距离启动录像还有{}秒'.format(data_definit-data_current)) time.sleep(1) start_AVrecording() while time.strftime('%H%M%S', time.localtime(time.time()))!=str(endtime): if int(time.strftime('%H', time.localtime(time.time())))<= int(str(endtime)[:2]): data_definit = int(str(endtime)[:2])*3600+int(str(endtime)[2:4])*60+int(str(endtime)[4:6]) data_current = int(time.strftime('%H', time.localtime(time.time())))*3600+int(time.strftime('%M', time.localtime(time.time())))*60+\ int(time.strftime('%S', time.localtime(time.time()))) print('距离结束录像还有{}秒'.format(data_definit-data_current)) time.sleep(1) else: data_definit = int(str(endtime)[:2])*3600+int(str(endtime)[2:4])*60+int(str(endtime)[4:6])+24*3600 data_current = int(time.strftime('%H', time.localtime(time.time())))*3600+int(time.strftime('%M', time.localtime(time.time())))*60+\ int(time.strftime('%S', time.localtime(time.time()))) print('距离结束录像还有{}秒'.format(data_definit-data_current)) time.sleep(1) stop_AVrecording() file_manager() parser = argparse.ArgumentParser() parser.add_argument('--starttime', '-s', type=int, default='020000') parser.add_argument('--endtime', '-e', type=int, default='050000') args = parser.parse_args() starttime = args.starttime endtime = args.endtime while time.strftime('%H%M%S', time.localtime(time.time()))!=str(starttime): if int(time.strftime('%H', time.localtime(time.time())))<=int(str(starttime)[:2]): data_definit = int(str(starttime)[:2])*3600+int(str(starttime)[2:4])*60+int(str(starttime)[4:6]) data_current = int(time.strftime('%H', time.localtime(time.time())))*3600+int(time.strftime('%M', time.localtime(time.time())))*60+\ int(time.strftime('%S', time.localtime(time.time()))) print('距离启动录像还有{}秒'.format(data_definit-data_current)) time.sleep(1) else: data_definit = int(str(starttime)[:2])*3600+int(str(starttime)[2:4])*60+int(str(starttime)[4:6])+24*3600 data_current = int(time.strftime('%H', time.localtime(time.time())))*3600+int(time.strftime('%M', time.localtime(time.time())))*60+\ int(time.strftime('%S', time.localtime(time.time()))) print('距离启动录像还有{}秒'.format(data_definit-data_current)) time.sleep(1) start_AVrecording() while time.strftime('%H%M%S', time.localtime(time.time()))!=str(endtime): if int(time.strftime('%H', time.localtime(time.time())))<= int(str(endtime)[:2]): data_definit = int(str(endtime)[:2])*3600+int(str(endtime)[2:4])*60+int(str(endtime)[4:6]) data_current = int(time.strftime('%H', time.localtime(time.time())))*3600+int(time.strftime('%M', time.localtime(time.time())))*60+\ int(time.strftime('%S', time.localtime(time.time()))) print('距离结束录像还有{}秒'.format(data_definit-data_current)) time.sleep(1) else: data_definit = int(str(endtime)[:2])*3600+int(str(endtime)[2:4])*60+int(str(endtime)[4:6])+24*3600 data_current = int(time.strftime('%H', time.localtime(time.time())))*3600+int(time.strftime('%M', time.localtime(time.time())))*60+\ int(time.strftime('%S', time.localtime(time.time()))) print('距离结束录像还有{}秒'.format(data_definit-data_current)) time.sleep(1) stop_AVrecording() file_manager()

最后附上代码链接: link.有需要的就来拿吧。

最新回复(0)