如何在 Linux 上录制你的终端操作
终端 录制
录制一个终端操作可能是一个帮助他人学习 Linux 、展示一系列正确命令行操作的和分享知识的通俗易懂方法。不管是出于什么目的，从终端复制粘贴文本需要重复很多次，而录制视频的过程也是相当麻烦，有时候还不能录制。在这次的文章中，我们将简单的了解一下以 gif 格式记录和分享终端会话的方法。


### 预先要求


如果你只是希望能记录你的终端会话，并且能在终端进行回放或者和他人分享，那么你只需要一个叫做：ttyrec 的软件。Ubuntu 用户可以通过运行这行代码进行安装：



```
sudo apt-get install ttyrec

```

如果你想将生成的视频转换成一个 gif 文件，这样能够和那些不使用终端的人分享，就可以发布到网站上去，或者你只是想做一个 gif 方便使用而不想写命令。那么你需要安装额外的两个软件包。第一个就是 imagemagick , 你可以通过以下的命令安装：



```
sudo apt-get install imagemagick

```

第二个软件包就是：tty2gif.py，访问其[项目网站](https://bitbucket.org/antocuni/tty2gif/raw/61d5596c916512ce5f60fcc34f02c686981e6ac6/tty2gif.py)下载。这个软件包需要安装如下依赖:



```
sudo apt-get install python-opster

```

### 录制


开始录制终端操作，你需要的仅仅是键入 `ttyprec` ，然后回车。这个命令将会在后台运行一个实时的记录工具。我们可以通过键入`exit`或者`ctrl+d`来停止。ttyrec 默认会在主目录下创建一个`ttyrecord`的文件。


![](/data/attachment/album/201607/14/090540y88686ia4eeet6ag.jpg)


![](/data/attachment/album/201607/14/090541hy85xqj33jdeyzg2.jpg)


![](/data/attachment/album/201607/14/090541ph617831ma8dk1zt.jpg)


### 回放


回放这个文件非常简单。你只需要打开终端并且使用 `ttyplay` 命令打开 `ttyrecord` 文件即可。(在这个例子里，我们使用 ttyrecord 作为文件名，当然，你也可以改成你用的文件名)


![](/data/attachment/album/201607/14/090541j0nj7fm0jfgif1mn.jpg)


然后就可以开始播放这个文件。这个视频记录了所有的操作，包括你的删除，修改。这看起来像一个拥有自我意识的终端，但是这个命令执行的过程并不是只是为了给系统看，而是为了更好的展现给人。


注意一点，播放这个记录是完全可控的，你可以通过点击 `+` 或者 `-` 进行加速减速，或者 `0`和 `1` 暂停和恢复播放。


### 导出成 GIF


为了方便，我们通常会将视频记录转换为 gif 格式，并且，这个非常容易做到。以下是方法：


将之前下载的 tty2gif.py 这个文件拷贝到 ttyprecord 文件（或者你命名的那个视频文件）相同的目录，然后在这个目录下打开终端，输入命令：



```
python tty2gif.py typing ttyrecord

```

如果出现了错误，检查一下你是否有安装 python-opster 包。如果还是有错误，使用如下命令进行排除。



```
sudo apt-get install xdotool
export WINDOWID=$(xdotool getwindowfocus)

```

然后重复这个命令 `python tty2gif.py` 并且你将会看到在 ttyrecord 目录下多了一些 gif 文件。


![](/data/attachment/album/201607/14/090542web5cac1ymncdnzs.jpg)


接下来的一步就是整合所有的 gif 文件，将他打包成一个 gif 文件。我们通过使用 imagemagick 工具。输入下列命令:



```
convert -delay 25 -loop 0 *.gif example.gif

```

![](/data/attachment/album/201607/14/090542pnpxnn8i9i1bs166.jpg)


你可以使用任意的文件名，我用的是 example.gif。 并且，你可以改变这个延时和循环时间。 Enjoy。




---


via: <https://www.howtoforge.com/tutorial/how-to-record-your-terminal-session-on-linux/>


作者：[Bill Toulas](https://twitter.com/howtoforgecom) 译者：[MikeCoder](https://github.com/MikeCoder) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# How to record your terminal session on Linux

Recording a terminal session may be important in helping someone learn a process, sharing information in an understandable way, and also presenting a series of commands in a proper manner. Whatever the purpose, there are many times when copy-pasting text from the terminal won't be very helpful while capturing a video of the process is quite far-fetched and may not be always possible. In this quick guide, we will take a look at the easiest way to record and share a terminal session in .gif format.

## Prerequisites

If you just want to record your terminal sessions and be able to play the recording in your terminal, or share them with people who will use a terminal for playback, then the only tool that you'll need is called “ttyrec”. Ubuntu users may install it by inserting the following command on a terminal:

sudo apt-get install ttyrec

If you want to produce a .gif file from the recording and be able to share it with people who don't use the terminal, publish it on websites, or simply keep a .gif handy for when you'll need it instead of written commands, you will have to install two additional packages. The first one is “imagemagick” which you can install with:

sudo apt-get install imagemagick

and the second one is “tty2gif” which can be downloaded from here. The latter has a dependency that can be satisfied with:

sudo apt-get install python-opster

## Capturing

To start capturing the terminal session, all you need to do is simply start with “ttyrec” + enter. This will launch the real-time recording tool which will run in the background until we enter “exit” or we press “Ctrl+D”. By default, ttyrec creates a file named “ttyrecord” on the destination of the terminal session which by default is “Home”.

## Playing

Playing the file is as simple as opening a terminal on the destination of the “ttyrecord” file and using the “ttyplay” command followed by the name of the recording (in our case it's ttyrecord but you may change this into whatever you want).

This will result in the playback of the recorded session, in real-time, and with typing corrections included (all actions are recorded). This will look like a completely normal automated terminal session, but the commands and their apparent execution are obviously not really applied to the system, as they are only reproduced as a recording.

It is also important to note that the playback of the terminal session recording is completely controllable. You may double the playback speed by hitting the “+” button, slow it down with the “-” button, pause it with “0”, and resume it in normal speed with “1”.

## Converting into a .gif

For reasons of convenience, many of us would like to convert the recorded session into a .gif file, and that is very easy to do. Here's how:

First, untar the downloaded “tty2gif.tar.bz2” by opening a terminal in the download location and entering the following command:

tar xvfj tty2gif.tar.bz2

Next, copy the resulting “tty2gif.py file onto the destination of the “ttyrecord” file (or whatever the name you've specified is), and then open a terminal on that destination and type the command:

python tty2gif.py typing ttyrecord

If you are getting errors in this step, check that you have installed the “python-opster” package. If errors persist, give the following two commands consecutively:

sudo apt-get install xdotool

export WINDOWID=$(xdotool getwindowfocus)

then repeat the “python tty2gif.py typing ttyrecord ” and you should now see a number of gif files that were created on the location of the “ttyrecord”

The next step is to unify all these gifs that correspond to individual terminal session actions into one final .gif file using the imagemagick utility. To do this, open a terminal on the destination and insert the following command:

convert -delay 25 -loop 0 *.gif example.gif

You may name the resulting file as you like (I used “example.gif”), and you may change the delay and loop settings as needed. Here is the resulting file of this quick tutorial: