Linux中使用Asciinema进行录制和回放终端会话(2)

我们首先使用 rec 选项来录制终端会话。下面的命令将会开始录制终端会话,之后,你将会有一个选项来丢弃已录制记录或者把它上传到 asciinema.org 网站以便将来参考。

$ asciinema rec 

运行上面的命令以后,你会注意到, Asciinema 已经开始录制终端会话了,你可以按下 CTRL+D 快捷键或执行 exit 命令来停止录制。如果你使用的是 Debian/Ubuntu/Mint Linux 系统,你可以像下面这样尝试进行第一次 asciinema 录制:

$ su 

Password

# apt install sl 

# exit 

$ sl 

一旦输入最后一个 exit 命令以后,将会询问你:

$ exit 

~ Asciicast recording finished. 

~ Press <Enter> to upload, <Ctrl-C> to cancel. 

https://asciinema.org/a/7lw94ys68gsgr1yzdtzwijxm4 

如果你不想上传你的私密命令行技巧到 asciinema.org 网站,那么有一个选项可以把 Asciinema 记录以 JSON 格式保存为本地文件。比如,下面的 asciinema 记录将被存为 /tmp/my_rec.json:

$ asciinema rec /tmp/my_rec.json 

另一个非常有用的 asciinema 特性是时间微调。如果你的键盘输入速度很慢,或者你在进行多任务,输入命令和执行命令之间的时间会比较长。Asciinema 会记录你的实时按键时间,这意味着每一个停顿都将反映在最终视频的长度上。可以使用 -w 选项来缩短按键的时间间隔。比如,下面的命令将按键的时间间隔缩短为 0.2 秒:

$ asciinema rec -w 0.2 

回放已录制终端会话

有两种方式可以来回放已录制会话。第一种方式是直接从 asciinema.org 网站上播放终端会话。这意味着,你之前已经把录制会话上传到了 asciinema.org 网站,并且需要提供有效链接:

$ asciinema play https://asciinema.org/a/7lw94ys68gsgr1yzdtzwijxm4 

另外,你也可以使用本地存储的 JSON 文件:

$ asciinema play /tmp/my_rec.json 

如果要使用 wget 命令来下载之前的上传记录,只需在链接的后面加上 .json:

$ wget -q -O steam_locomotive.json https://asciinema.org/a/7lw94ys68gsgr1yzdtzwijxm4.json 

$ asciinema play steam_locomotive.json 

将视频嵌入 HTML

最后,asciinema 还带有一个独立的 JavaScript 播放器。这意味者你可以很容易的在你的网站上分享终端会话记录。下面,使用一段简单的 index.html 代码来说明这个方法。首先,下载所有必要的东西:

$ cd /tmp/ 

$ mkdir steam_locomotive 

$ cd steam_locomotive/ 

$ wget -q -O steam_locomotive.json https://asciinema.org/a/7lw94ys68gsgr1yzdtzwijxm4.json 

$ wget -q https://github.com/asciinema/asciinema-player/releases/download/v2.4.0/asciinema-player.css 

$ wget -q https://github.com/asciinema/asciinema-player/releases/download/v2.4.0/asciinema-player.js 

之后,创建一个新的包含下面这些内容的 /tmp/steam_locomotive/index.html 文件:

<html> 

<head> 

  <link rel="stylesheet" type="text/css" href="./asciinema-player.css" /> 

</head> 

<body> 

  <asciinema-player src="./steam_locomotive.json" cols="80" rows="24"></asciinema-player> 

  <script src="./asciinema-player.js"></script> 

</body> 

</html> 

完成以后,打开你的网页浏览器,按下 CTRL+O 来打开新创建的 /tmp/steam_locomotive/index.html 文件。

结论

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/13861.html