vcopy.py 2.8 KB
Newer Older
Guido van Rossum's avatar
Guido van Rossum committed
1 2 3 4 5 6
# Copy a video file, interactively, frame-by-frame.

import sys
import getopt
from gl import *
from DEVICE import *
7
import VFile
8 9
import string
import imageop
Guido van Rossum's avatar
Guido van Rossum committed
10 11 12 13 14

def report(time, iframe):
	print 'Frame', iframe, ': t =', time

def usage():
15 16 17 18 19
	sys.stderr.write('usage: vcopy [-t type] [-m treshold] [-a] infile outfile\n')
	sys.stderr.write('-t Convert to other type\n')
	sys.stderr.write('-a Automatic\n')
	sys.stderr.write('-m Convert grey to mono with treshold\n')
	sys.stderr.write('-d Convert grey to mono with dithering\n')
Guido van Rossum's avatar
Guido van Rossum committed
20 21 22 23 24 25 26 27
	sys.exit(2)

def help():
	print 'Command summary:'
	print 'n   get next image from input'
	print 'w   write current image to output'

def main():
28
	foreground()
29
	opts, args = getopt.getopt(sys.argv[1:], 't:am:d')
Guido van Rossum's avatar
Guido van Rossum committed
30 31 32
	if len(args) <> 2:
		usage()
	[ifile, ofile] = args
33 34 35 36 37 38 39 40 41
	print 'open film ', ifile
	ifilm = VFile.VinFile().init(ifile)
	print 'open output ', ofile
	ofilm = VFile.VoutFile().init(ofile)
	
	ofilm.setinfo(ifilm.getinfo())

	use_grabber = 0
	continuous = 0
42 43
	tomono = 0
	tomonodither = 0
44 45 46 47 48 49
	for o, a in opts:
		if o == '-t':
			ofilm.format = a
			use_grabber = 1
		if o == '-a':
			continuous = 1
50 51 52 53 54 55 56 57 58 59 60 61 62 63
		if o == '-m':
			if ifilm.format <> 'grey':
				print '-m only supported for greyscale'
				sys.exit(1)
			tomono = 1
			treshold = string.atoi(a)
			ofilm.format = 'mono'
		if o == '-d':
			if ifilm.format <> 'grey':
				print '-m only supported for greyscale'
				sys.exit(1)
			tomonodither = 1
			ofilm.format = 'mono'
			
64
	ofilm.writeheader()
Guido van Rossum's avatar
Guido van Rossum committed
65
	#
66 67
	prefsize(ifilm.width, ifilm.height)
	w = winopen(ifile)
Guido van Rossum's avatar
Guido van Rossum committed
68 69 70 71
	qdevice(KEYBD)
	qdevice(ESCKEY)
	qdevice(WINQUIT)
	qdevice(WINSHUT)
72
	print 'qdevice calls done'
Guido van Rossum's avatar
Guido van Rossum committed
73 74 75
	#
	help()
	#
76 77
	time, data, cdata = ifilm.getnextframe()
	ifilm.showframe(data, cdata)
Guido van Rossum's avatar
Guido van Rossum committed
78 79 80 81
	iframe = 1
	report(time, iframe)
	#
	while 1:
82 83 84 85
		if continuous:
			dev = KEYBD
		else:
			dev, val = qread()
Guido van Rossum's avatar
Guido van Rossum committed
86 87
		if dev in (ESCKEY, WINQUIT, WINSHUT):
			break
Guido van Rossum's avatar
Guido van Rossum committed
88
		if dev == REDRAW:
Guido van Rossum's avatar
Guido van Rossum committed
89
			reshapeviewport()
Guido van Rossum's avatar
Guido van Rossum committed
90
		elif dev == KEYBD:
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
			if continuous:
				c = '0'
			else:
				c = chr(val)
			#XXX Debug
			if c == 'R':
				c3i(255,0,0)
				clear()
			if c == 'G':
				c3i(0,255,0)
				clear()
			if c == 'B':
				c3i(0,0,255)
				clear()
			if c == 'w' or continuous:
				if use_grabber:
					data, cdata = ofilm.grabframe()
108 109 110 111 112 113 114
				if tomono:
					data = imageop.grey2mono(data, \
						  ifilm.width, ifilm.height, \
						  treshold)
				if tomonodither:
					data = imageop.dither2mono(data, \
						  ifilm.width, ifilm.height)
115 116 117
				ofilm.writeframe(time, data, cdata)
				print 'Frame', iframe, 'written.'
			if c == 'n' or continuous:
Guido van Rossum's avatar
Guido van Rossum committed
118
				try:
119 120
					time,data,cdata = ifilm.getnextframe()
					ifilm.showframe(data, cdata)
Guido van Rossum's avatar
Guido van Rossum committed
121 122 123 124
					iframe = iframe+1
					report(time, iframe)
				except EOFError:
					print 'EOF'
125 126
					if continuous:
						break
Guido van Rossum's avatar
Guido van Rossum committed
127
					ringbell()
Guido van Rossum's avatar
Guido van Rossum committed
128
		elif dev == INPUTCHANGE:
Guido van Rossum's avatar
Guido van Rossum committed
129 130 131
			pass
		else:
			print '(dev, val) =', (dev, val)
132
	ofilm.close()
Guido van Rossum's avatar
Guido van Rossum committed
133 134

main()