def convertpool(self): if len(self.todo) > 0: if self.type in [".h264", ".mp4", ".avi"]: pool = Pool(min(self.pools, len(self.todo))) try: pool.map(self.conv_single, self.todo) pool.close() lineprint("Done converting all videofiles!", label="pirecorder") except KeyboardInterrupt: lineprint("User terminated converting pool..", label="pirecorder") self.terminated = True pool.terminate() return except Exception as e: excep = "Got exception: %r, terminating pool" % (e, ) lineprint(excep, label="pirecorder") pool.terminate() finally: pool.join() if self.delete: for filein in self.todo: os.remove(filein) lineprint("Deleted all original videofiles..", label="pirecorder") elif self.type in [".jpg", ".jpeg", ".png"]: vidname = commonpref(self.todo) lineprint("Start converting " + str(len(self.todo)) + " images", label="pirecorder") frame_array = [] for filename in self.todo: frame = cv2.imread(filename) frame_array.append(frame) #os.rename(filename, self.outdir+"/"+filename) h, w, _ = frame_array[0].shape if self.outdir != "": vidname = self.outdir + "/" + os.path.basename(vidname) vidout = videowriter(vidname, w, h, self.imgfps, self.resizeval) for i in range(len(frame_array)): vidout.write(frame_array[i]) vidout.release() lineprint("Finished converting " + os.path.basename(vidname), label="pirecorder") else: lineprint("No video or image files found..", label="pirecorder")
def conv_single(self, filein): try: filebase = os.path.basename(filein) fileout = filein if self.outdir == "" else self.outdir + "/" + filebase lineprint("Start converting " + filebase, label="pirecorder") if self.withframe: vid = cv2.VideoCapture(filein) fps, width, height, _ = get_vid_params(vid) if self.fps is None: self.fps = fps vidout = videowriter(fileout, width, height, self.fps, self.resizeval) while True: flag, frame = vid.read() if flag: if self.resizeval != 1: frame = imgresize(frame, self.resizeval) frame_nr = int(vid.get(cv2.CAP_PROP_POS_FRAMES)) draw_text(frame, str(frame_nr), (10, 10), 0.9, col="white", shadow=True) vidout.write(frame) if not flag: break else: if self.resizeval != 1: comm = "' -vf 'scale=iw*" + str(self.resizeval) + ":-2' '" else: comm = "' -vcodec copy '" bashcomm = "ffmpeg" if self.fps is not None: bashcomm = bashcomm + " -r " + str(self.fps) bashcomm = bashcomm + " -i '" + filein + comm + fileout[:-len( self.type)] + ".mp4'" bashcomm = bashcomm + " -y -nostats -loglevel 0" output = subprocess.check_output(['bash', '-c', bashcomm]) lineprint("Finished converting " + filebase, label="pirecorder") except KeyboardInterrupt: raise KeyboardInterruptError()