linux 终端录制

 刚 get 了一个在 linux 下好玩的东西,可以录制一个终端操作,还能转化为 gif 图像格式,很赞!

首先安装一下软件

1
2
3
$ sudo apt-get install ttyrec
$ sudo apt-get install imagemagick
$ sudo apt-get install python-opster

然后将以下代码存为 tty2gif.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python

import os
import sys
import struct
import time
from opster import command, dispatch

header = struct.Struct('iii')

@command()
def replay(filename, factor=('f', 1, 'Speedup factor')):
main(filename, 'replay', factor=factor)

@command()
def typing(filename):
main(filename, 'typing')

@command()
def inspect(filename):
main(filename, 'inspect')

def main(filename, action, factor=1):
winid = int(os.getenv('WINDOWID'))

script = open(filename)
basetime = None
prevtime = None
stepnum = 0
args = []
while True:
data = script.read(header.size)
if not data:
break
sec, usec, n = header.unpack(data)
curtime = sec + usec/1000000.0
if basetime is None:
basetime = prevtime = curtime
delay = curtime-prevtime
prevtime = curtime
data = script.read(n)
if action == 'inspect':
print '%8.4f %4d %s' % (delay, n, `data[:40]`)
continue
if action == 'replay':
time.sleep(delay/factor)
elif action == 'typing':
delay = 0
if n == 1:
delay = 0.2
elif data.startswith('\r\n'):
delay = 0.5
if delay:
os.system('import -window %s step%03d.gif' % (winid, stepnum))
#time.sleep(delay)
#print '-delay %d step%03d.gif' % (delay*100, stepnum),
stepnum+=1

sys.stdout.write(data)
sys.stdout.flush()


if __name__ == '__main__':
dispatch()

关于使用

1
2
3
4
$ ttyrec    ---开始录制
$ exit ---结束录制
$ ttyplay ttyreord ---回放录制的内容
$ python tty2gif.py typing ttyrecord && convert -delay 10 -loop 0 *.gif example.gif ---制作动态gif图像

0%