recterm - A Nice Way to Record Videos with Audio on Linux Terminal

We generally record the terminal with a video recorder but the output video is generally blurry and not high quality. ASCIINEMA is a lightweight tool that record and share terminal session with high quality video. The drawbacks of asciinema is that the video does not include the audio. The recterm is a tool that can record the terminal into a mp4 file. It does not need any video player like vlc, mplayer for recording into an mp4 with audio. This article covers installation of Recterm to record the terminal into a mp4 with audio using asciinema, asciinema2gif and ffmpeg.

Install asciinema

ASCIINEMA is a lightweight tool to record the terminal activities. Once the terminal recording is complete, it provides a URL to share the recorded session. The viewers can simply pause the video, highlight the command and copy/paste it into their terminal. To install asciinema, execute the following command from the terminal.

 # sudo curl -sL https://asciinema.org/install | sh
 Downloading asciinema v1.2.0 for linux-amd64...
 ########################################100.0%
 Installing to /usr/local/bin/asciinema...
 Success.

Install linuxbrew

Linuxbrew is a fork of Homebrew the Mac OS package manager. It can be installed in the home directory and does not require root access. Linuxbrew has few dependencies that we will install at first. We need linuxbrew to install asciinema2gif.

 # sudo apt-get install build-essential curl git python-setuptools ruby

Now install linuxbrew

 # ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install)"

==> This script will install:
 /home/ubuntu/.linuxbrew/bin/brew
 /home/ubuntu/.linuxbrew/Library/...
 /home/ubuntu/.linuxbrew/share/doc/homebrew
 /home/ubuntu/.linuxbrew/share/man/man1/brew.1
 /home/ubuntu/.linuxbrew/share/zsh/site-functions/_brew
 /home/ubuntu/.linuxbrew/etc/bash_completion.d/brew
 /home/ubuntu/.cache/Homebrew/
 .................................................
 .................................................

Add the linuxbrew binary path to PATH, man pages to MANPATH and info path to INFOPATH environmental variable in ~/.bash_profile.

 # echo 'export PATH="/home/ubuntu/.linuxbrew/bin:$PATH"' >>~/.bash_profile
 # echo 'export MANPATH="/home/ubuntu/.linuxbrew/share/man:$MANPATH"' >>~/.bash_profile
 # echo 'export INFOPATH="/home/ubuntu/.linuxbrew/share/info:$INFOPATH"' >>~/.bash_profile

Load the new environment.

 # source ~/.bash_profile

Linuxbrew recommends to install gcc. We need it because while installing asciinema2gif, brew will compile the dependencies for it.

 # brew install gcc

Check the installation of brew by executing following command from the terminal.

 # brew install hello

Install asciinema2gif

Install asciinema2gif using linuxbrew. While installing asciinema2gif, brew will also install all the dependencies and these are gifsicle, xz, pkg-config, libtool, bzip2, jpeg, zlib, libpng, libtiff, freetype, imagemagick, gpatch, openssl, icu4c, libxml2, phantomjs.

 #  brew install asciinema2gif
 ==> Tapping homebrew/dupes
 Cloning into '/home/ubuntu/.linuxbrew/Library/Taps/homebrew/homebrew-dupes'...
 remote: Counting objects: 42, done.
 remote: Compressing objects: 100% (42/42), done.
 remote: Total 42 (delta 0), reused 3 (delta 0), pack-reused 0
 Unpacking objects: 100% (42/42), done.
 .....................................................
 .....................................................

Install ffmpeg/sox

SOX is needed to start the audio recording separately while we start recording the terminal. FFMPEG is needed for converting all gif into an mp4 files and to combine the audio (WAV) with final MP4 file. Install bc which is an arbitrary precision numeric processing language. This is needed for re-sizing mp4 and to correctly combine mp4 and wav.

 # sudo apt-get install sox ffmpeg bc

Install recterm

To install recterm, simply clone the github repository and change the permission of the scripts.

 # git clone https://github.com/rascoro1/recterm.git
 # cd recterm/
 # chmod +x recterm.sh 
 # chmod +x to_gif.sh

Now open asciinema2gif script in vi editor and edit the line number 116. You can find the location of it by using 'which asciinema2gif' from the terminal. This is needed because asciinema2gif creates gif/png files in the temporary directory that is configured in line no 14 i.e

 readonly tempdir="$(mktemp -d -t asciinema2gif.XXXX)"

Edit the following file and change the location of png/gif files. The file path may be different if you are using different version of asciinema2gif.

 # vim /home/ubuntu/.linuxbrew/Cellar/asciinema2gif/0.5/libexec/bin/asciinema2gif

Therefore to find the gif/png files in proper directory by the convert command, change the directory location of gif/png files to /tmp/asciinema2gif*/frames/*.png gif so that the final line looks like this-

 convert -delay 5 -loop 0 /tmp/asciinema2gif*/frames/*.png gif:- | gifsicle --colors=256 --delay=6 --optimize=3 --output='asciicast.gif'

At the end of the asciinema2gif script, add the following line to remove the temporary directory.

 rm -rf /tmp/asciinema2gif*/

While converting to mp4 if the output contains 'gifsicle:<stdin>: empty files' that means convert command unable to find the png/gif files or there is an insufficient memory to convert png/gif to mp4. There must be at least 2GB RAM if you are trying to record a large file otherwise convert command will exit with 'killed' status.

Now change to the recterm directory and execute it.

 # ./recterm.sh new_term
 ~ Asciicast recording started.
 ~ Hit Ctrl-D or type "exit" to finish.

There will be a folder by the name new_term from where recterm script has been executed and the final mp4 file can be found here.

Conclusion

Recterm is a lightweight terminal recording tool along with audio. It takes advantages of asciinema, asciinema2gif and ffmpeg to perform the task. If you have any issues regarding installation, post it in recterm issue section in github. Thanks for reading the article.

About Dwijadas Dey

Dwijadas Dey is working with GNU/Linux, Open source systems since 2005. Having avid follower of GNU/Linux, He believes in sharing and spreading the open source ideas to the targeted audience. Apart from freelancing he also writes for community. His current interest includes information and network security.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.

5 Comments