⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vb.py

📁 Python语言编译器
💻 PY
📖 第 1 页 / 共 2 页
字号:
			self.setarrow()			fl.show_message('Capture error:', str(msg), '')			return		if info <> info2: print info, '<>', info2		self.save_burst(info2, data, bitvec)		self.setarrow()	def calcnframes(self, memsize):		gl.winset(self.window)		x, y = gl.getsize()		pixels = x*y		pixels = pixels/2	# XXX always assume fields		if self.mono or self.grey:			n = memsize/pixels		else:			n = memsize/(4*pixels)		return max(1, n)	def save_burst(self, info, data, bitvec):		(vformat, x, y, nframes, rate) = info		self.open_if_closed()		fieldsize = x*y/2		nskipped = 0		realframeno = 0		tpf = 1000 / 50.0     # XXX		for frameno in range(0, nframes*2):			if frameno <> 0 and \				  bitvec[frameno] == bitvec[frameno-1]:				nskipped = nskipped + 1				continue			#			# Save field.			# XXX Works only for fields and top-to-bottom			#			start = frameno*fieldsize			field = data[start:start+fieldsize]			realframeno = realframeno + 1			fn = int(realframeno*tpf)			if not self.write_frame(fn, field):				break	def cont_capture(self):		saved_label = self.b_capture.label		self.b_capture.label = 'Stop\n' + saved_label		self.open_if_closed()		self.init_cont()		fps = 59.64		# Fields per second		# XXX (fps of Indigo monitor, not of PAL or NTSC!)		tpf = 1000.0 / fps	# Time per field in msec		self.capturing = 1		self.start_audio()		while 1:			try:				void = fl.check_forms()			except StopCapture:				break			try:				cd, id = self.video.GetCaptureData()			except sv.error:				sgi.nap(1)				continue			id = id + 2*self.rate			data = cd.InterleaveFields(1)			cd.UnlockCaptureData()			t = id*tpf			if not self.write_frame(t, data):				break		self.stop_audio()		self.capturing = 0		self.end_cont()		if self.aout:			# If recording audio, can't capture multiple sequences			self.reset()		self.b_capture.label = saved_label	def single_capture(self, stepfunc, timecode):		self.open_if_closed()		self.init_cont()		while 1:			try:				cd, id = self.video.GetCaptureData()				break			except sv.error:				pass			sgi.nap(1)			if stepfunc:		# This might step the video				d=stepfunc()	# to the next frame		if not self.use_24:			data = cd.InterleaveFields(1)		else:			x, y = self.vout.getsize()			if self.use_compress:				if self.rgb24_size == 1:					data = cd.YUVtoYUV422DC(0)				elif self.rgb24_size == 2:					data = cd.YUVtoYUV422DC_quarter(1)					x = x/2					y = y/2				elif self.rgb24_size == 3:					data = cd.YUVtoYUV422DC_sixteenth(1)					x = x/4					y = y/4			else:				data = cd.YUVtoRGB(1)				if self.maxx*self.maxy*4 <> len(data):					print 'maxx,maxy,exp,got=', self.maxx,					print self.maxy,self.maxx*self.maxy*4,					print len(data)					fl.showmessage('Wrong sized data')					return 0				if self.rgb24_size <> 1:					data = imageop.scale(data, 4, \						  self.maxx, self.maxy, x, y)			if self.use_jpeg:				import jpeg				data = jpeg.compress(data, x, y, 4)			if self.use_compress:				data = self.compressor.Compress(1, data)		cd.UnlockCaptureData()		self.end_cont()		if timecode == None:			timecode = (self.nframes+1) * (1000/25)		return self.write_frame(timecode, data)	def vcr_capture(self):		if not self.vcr:			try:				print 'Connecting to VCR ...'				self.vcr = VCR.VCR()				print 'Waiting for VCR to come online ...'				self.vcr.initvcr()				print 'Preparing VCR ...'				if not (self.vcr.fmmode('dnr') and \					  self.vcr.dmcontrol('digital slow')):					self.vcr_error('digital slow failed')					return				print 'VCR OK.'			except VCR.error, msg:				self.vcr = None				self.vcr_error(msg)				return		if not self.vcr.still():			self.vcr_error('still failed')			return		self.open_if_closed()		rate = self.getint(self.in_rate_vcr, 1)		rate = max(rate, 1)		vcrspeed = self.c_vcrspeed.get_choice()		vcrspeed = VcrSpeeds[vcrspeed]		if vcrspeed == 0:			stepfunc = self.vcr.step		else:			stepfunc = None		self.speed_factor = rate		addr = start_addr = self.vcr.sense()		if not self.single_capture(None, 0):			return		print 'captured %02d:%02d:%02d:%02d' % self.vcr.addr2tc(addr)		count = self.getint(self.in_nframes_vcr, 1) - 1		if count <= 0:			while rate > 0:				if not self.vcr.step():					self.vcr_error('step failed')				here = self.vcr.sense()				if here > addr:					rate = rate - (here - addr)				addr = here			return		if not self.vcr.fwdshuttle(vcrspeed):			self.vcr_error('fwd shuttle failed')			return		cycle = 0		while count > 0:			try:				here = self.vcr.sense()			except VCR.error, msg:				self.vcr_error(msg)				break			if here <> addr:				if here <> addr+1:					print 'Missed', here-addr-1,					print 'frame' + 's'*(here-addr-1 <> 1)				cycle = (cycle+1) % rate				if cycle == 0:					tc = (here-start_addr)*40					if not self.single_capture(stepfunc, \						  tc):						break					print 'captured %02d:%02d:%02d:%02d' \						  % self.vcr.addr2tc(here)					count = count -1				addr = here		if self.vcr and not self.vcr.still():			self.vcr_error('still failed')	def vcr_error(self, msg):		self.reset()		fl.show_message('VCR error:', str(msg), '')	# Init/end continuous capture mode	def init_cont(self):		qsize = 1		if self.vmode == VM_CONT:			self.rate = self.getint(self.in_rate, 2)		else:			self.rate = 2		x, y = self.vout.getsize()		if self.use_24:			info = (SV.YUV411_FRAMES, x, y, qsize, self.rate)		else:			info = (SV.RGB8_FRAMES, x, y, qsize, self.rate)		info2 = self.video.InitContinuousCapture(info)		if info2 <> info:			# XXX This is really only debug info			print 'Info mismatch: requested', info, 'got', info2	def end_cont(self):		self.video.EndContinuousCapture()	# Misc stuff	def settitle(self):		gl.winset(self.window)		x, y = gl.getsize()		title = 'Vb ' + self.vfile + ' (%dx%d)' % (x, y)		gl.wintitle(title)	def get_vformat(self):		i = self.c_vformat.get_choice()		label = VideoFormatLabels[i-1]		format = VideoFormats[i-1]		if format == 'compress' and cl == None:			fl.show_message('Sorry, no compression library support')			format = ''			label = 'Video off'		self.vformat = format		if self.vformat == '':			self.form.freeze_form()			self.g_video.hide_object()			self.g_cont.hide_object()			self.g_burst.hide_object()			self.g_single.hide_object()			self.form.unfreeze_form()		else:			self.g_video.show_object()			if self.vmode == VM_CONT:				self.g_cont.show_object()			elif self.vmode == VM_BURST:				self.g_burst.show_object()			elif self.vmode == VM_SINGLE:				self.g_single.show_object()		#		self.rgb = (format[:3] == 'rgb' or format == 'compress')		self.mono = (format == 'mono')		self.grey = (format[:4] == 'grey')		self.use_24 = (format in ('rgb', 'jpeg', 'compress'))		if self.use_24:			self.g_rgb24.show_object()		else:			self.g_rgb24.hide_object()		self.use_jpeg = (format == 'jpeg')		self.mono_use_thresh = (label == 'mono thresh')		self.use_compress = (format == 'compress')		if self.use_compress:			self.g_compress.show_object()		else:			self.g_compress.hide_object()		s = format[4:]		if self.grey and s:			self.greybits = string.atoi(s)		else:			self.greybits = 8		if label == 'grey2 dith':			self.greybits = -2		#		convertor = None		if self.grey:			if self.greybits == 2:				convertor = imageop.grey2grey2			elif self.greybits == 4:				convertor = imageop.grey2grey4			elif self.greybits == -2:				convertor = imageop.dither2grey2		self.convertor = convertor		self.optfullsizewindow()	def get_aformat(self):		self.reset()		self.aformat = self.c_aformat.get_choice()		if self.aformat == A_OFF:			self.g_audio.hide_object()		else:			self.g_audio.show_object()	def init_compressor(self, w, h):		self.compressor = None		scheme = cl.QuerySchemeFromName(cl.VIDEO, self.comp_scheme)		self.compressor = cl.OpenCompressor(scheme)		parambuf = [cl.IMAGE_WIDTH, w, \			  cl.IMAGE_HEIGHT, h, \			  cl.ORIGINAL_FORMAT, cl.YUV422DC]		self.compressor.SetParams(parambuf)		return self.compressor.Compress(0, '')	def open_if_closed(self):		if not self.vout:			self.open_video()		if not self.aout:			self.open_audio()	# File I/O handling	def open_video(self):		self.close_video()		gl.winset(self.window)		x, y = gl.getsize()		if self.use_24:			if self.rgb24_size == 2:				x, y = x/2, y/2			elif self.rgb24_size == 3:				x, y = x/4, y/4		vout = VFile.VoutFile(self.vfile)		vout.setformat(self.vformat)		if self.vformat == 'compress':			cheader = self.init_compressor(x, y)			vout.setcompressheader(cheader)		vout.setsize(x, y)		if self.vmode == VM_BURST:			vout.setpf((1, -2))		vout.writeheader()		self.vout = vout		self.nframes = 0		self.speed_factor = 1		self.t_nframes.label = `self.nframes`	def write_frame(self, t, data):		t = t * self.speed_factor		if not self.vout:			gl.ringbell()			return 0		if self.convertor:			data = self.convertor(data, len(data), 1)		elif self.mono:			if self.mono_use_thresh:				data = imageop.grey2mono(data, \					  len(data), 1,\					  self.mono_thresh)			else:				data = imageop.dither2mono(data, \					  len(data), 1)		try:			self.vout.writeframe(int(t), data, None)		except IOError, msg:			self.reset()			if msg == (0, 'Error 0'):				msg = 'disk full??'			fl.show_message('IOError', str(msg), '')			return 0		self.nframes = self.nframes + 1		self.t_nframes.label = `self.nframes`		return 1	def close_video(self):		if not self.vout:			return		self.nframes = 0		self.t_nframes.label = ''		try:			self.vout.close()		except IOError, msg:			if msg == (0, 'Error 0'):				msg = 'disk full??'			fl.show_message('IOError', str(msg), '')		self.vout = None		self.compressor = None	# Watch cursor handling	def setwatch(self):		gl.winset(self.form.window)		gl.setcursor(WATCH, 0, 0)		gl.winset(self.window)		gl.setcursor(WATCH, 0, 0)	def setarrow(self):		gl.winset(self.form.window)		gl.setcursor(ARROW, 0, 0)		gl.winset(self.window)		gl.setcursor(ARROW, 0, 0)	# Numeric field handling	def getint(self, field, default):		try:			value = string.atoi(field.get_input())		except string.atoi_error:			value = default		field.set_input(`value`)		return value	def getfloat(self, field, default):		try:			value = float(eval(field.get_input()))		except:			value = float(default)		field.set_input(`value`)		return value	# Audio stuff	def open_audio(self):		if self.aformat == A_OFF:			return		import aifc		import al		import AL		import thread		self.close_audio()		params = [AL.INPUT_RATE, 0]		al.getparams(AL.DEFAULT_DEVICE, params)		rate = params[1]		self.aout = aifc.open(self.afile, 'w')		if self.aformat in (A_16_STEREO, A_8_STEREO):			nch = AL.STEREO		else:			nch = AL.MONO		if self.aformat in (A_16_STEREO, A_16_MONO):			width = AL.SAMPLE_16		else:			width = AL.SAMPLE_8		self.aout.setnchannels(nch)		self.aout.setsampwidth(width)		self.aout.setframerate(rate)		c = al.newconfig()		c.setqueuesize(8000)		c.setchannels(nch)		c.setwidth(width)		self.aport = al.openport('Vb audio record', 'r', c)		self.audio_stop = 0		self.audio_ok = 0		self.audio_busy = 1		thread.start_new_thread(self.record_audio, ())	def start_audio(self):		if self.aformat == A_OFF:			return		self.audio_ok = 1	def record_audio(self, *args):		# This function runs in a separate thread		# Currently no semaphores are used		while not self.audio_stop:			data = self.aport.readsamps(4000)			if self.audio_ok:				self.aout.writeframes(data)			data = None		self.audio_busy = 0	def stop_audio(self):		self.audio_ok = 0	def close_audio(self):		if self.aout:			self.audio_ok = 0			self.audio_stop = 1			while self.audio_busy:				time.sleep(0.1)			self.aout.close()			self.aout = None		if self.aport:			self.aport.closeport()			self.aport = Nonetry:	main()except KeyboardInterrupt:	print '[Interrupt]'	sys.exit(1)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -