How to Record Your Desktop Video and Audio Using Avconv Tool

Steps to Record Your Desktop Video and Audio Using “ Avconv” Tool in Ubuntu

Avconv tool is a very fast audio and video converter which grabs from a live audio or video source. In this article, we will learn how to record desktop video and audio by using “ Avconv” tool in Ubuntu.

Libav is a collection of cross-platform libraries and tools, which are used to work with streams, multimedia files and protocols. Libav includes several sub-tools such as:

  • Libavfilter: a filter API for several Libav tools.
  • Avprobe: connects to the multimedia file stream and also returns several useful information and statistics related to it.
  • Avconv: a multimedia converter and a video & audio recorder from various sources.
  • Avplay: video and audio player.

To Install Avconv Tool

avconv is from the ' libav-tools' package, which can be installed from the official repositories for all Debian-based distributions using the following command.

user1@linuxhelp:~$ sudo apt-get update
user1@linuxhelp:~$ sudo apt-get install libav-tools
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  ffmpeg i965-va-driver libaacs0 libass5 libavcodec-ffmpeg56
  libavdevice-ffmpeg56 libavfilter-ffmpeg5 libavformat-ffmpeg56
.
.
libvdpau1 libx264-148 libx265-79 libxvidcore4 libzmq5 libzvbi-common
  libzvbi0 mesa-vdpau-drivers va-driver-all vdpau-driver-all vdpau-va-driver
0 upgraded, 50 newly installed, 0 to remove and 0 not upgraded.
Need to get 27.7 MB of archives.
After this operation, 96.8 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
.
. 
Setting up vdpau-va-driver:amd64 (0.7.4-5) ...
Setting up va-driver-all:amd64 (1.7.0-1) ...
Setting up vdpau-driver-all:amd64 (1.1.1-3ubuntu1) ...
Setting up libav-tools (7:2.8.6-1ubuntu2) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...

Now the libav-tools has been installed successfully.

To install yasm package

Run the following command to install yasm package.

user1@linuxhelp:~$ sudo apt-get install yasm
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  yasm
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 351 kB of archives.
After this operation, 1,727 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu xenial/universe amd64 yasm amd64 1.3.0-2 [351 kB]
Fetched 351 kB in 4s (78.9 kB/s)
Selecting previously unselected package yasm.
(Reading database ... 172589 files and directories currently installed.)
Preparing to unpack .../yasm_1.3.0-2_amd64.deb ...
Unpacking yasm (1.3.0-2) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up yasm (1.3.0-2) ...


It is essential to get the latest version of ‘ avconv‘ tool from the official git repository.

user1@linuxhelp:~$ git clone git://git.libav.org/libav.git
Cloning into ' libav' ...
remote: Counting objects: 242852, done.
remote: Compressing objects: 100% (55456/55456), done.
remote: Total 242852 (delta 191214), reused 237836 (delta 186984)
Receiving objects: 100% (242852/242852), 52.26 MiB | 233.00 KiB/s, done.
Resolving deltas: 100% (191214/191214), done.
Checking connectivity... done.


After cloning the libav package from the official git repository, move into the libav directory and run the configure and make script

user1@linuxhelp:~$ cd libav
user1@linuxhelp:~/libav $ ./configure
install prefix            /usr/local
source path               .
C compiler                gcc
C library                 glibc
ARCH                      x86 (generic)
big-endian                no
runtime cpu detection     no
.
.
.
Enabled bsfs:
aac_adtstoasc             chomp                     dump_extradata           
h264_mp4toannexb          hevc_mp4toannexb          imx_dump_header          
mjpeg2jpeg                mjpega_dump_header        mov2textsub              
noise                     remove_extradata          text2movsub              
Enabled indevs:
dv1394                    fbdev                     oss                      
v4l2                                                                         
Enabled outdevs:
oss      

                                                             
user1@linuxhelp:~/libav ~$ sudo make
CC    libavdevice/alldevices.o
CC    libavdevice/avdevice.o
CC    libavdevice/dv1394.o
CC    libavdevice/fbdev.o
CC    libavdevice/oss.o
CC    libavdevice/oss_dec.o
CC    libavdevice/oss_enc.o
CC    libavdevice/v4l2.o
AR    libavdevice/libavdevice.a
CC    libavfilter/af_aformat.o
CC    libavfilter/af_amix.o
.
.
.
In file included from ./libavformat/avformat.h:258:0,
                 from doc/print_options.c:29:
./libavcodec/avcodec.h:3023:9: note: declared here
     int side_data_only_packets 
         ^
HOSTLD    doc/print_options
GENTEXI    doc/avoptions_format.texi
GENTEXI    doc/avoptions_codec.texi
POD    doc/avconv.pod
Unescaped left brace in regex is deprecated, passed through in regex  marked by

user1@linuxhelp:~/libav $ sudo make install
INSTALL    doc/avconv.1
INSTALL    doc/avprobe.1
INSTALL    install-progs-yes
INSTALL    avconv
INSTALL    avprobe
INSTALL    presets/libx264-slow.avpreset
INSTALL    presets/libx264-lossless_slower.avpreset
INSTALL    presets/libx264-ultrafast_firstpass.avpreset
INSTALL    presets/libx264-faster.avpreset
INSTALL    presets/libx264-lossless_max.avpreset
INSTALL    presets/libvpx-1080p.avpreset
INSTALL    presets/libx264-medium_firstpass.avpreset
INSTALL    presets/libx264-baseline.avpreset
.
.
.
INSTALL    libavutil/samplefmt.h
INSTALL    libavutil/sha.h
INSTALL    libavutil/stereo3d.h
INSTALL    libavutil/time.h
INSTALL    libavutil/version.h
INSTALL    libavutil/xtea.h
INSTALL    libavutil/lzo.h
INSTALL    libavutil/avconfig.h
INSTALL    libavutil/libavutil.pc


Run the ' ./configure --help' to list all the available options for the configuration file and then you need to install codecs and libraries that you require.

If you have used compile-from-source way, you need to use “ sudo avconv” to run the tool.

To Start Video Recording of Desktop

Run the following command to record your desktop video.

user1@linuxhelp:~$ avconv -f x11grab -r 24 -s 1920x1080 -i :0.0 -vcodec libx264 -threads 4 /home/user1/Desktop/video_only.avi


Once the enter key is pressed the recording will start automatically.

Press ‘ q’ key to stop the recording

Explanation of the above command

  • avconv -f x11grab - default command used for capturing video from the X server
  • -r 24 - is the frame rate and you can change it if you like.
  • -s 1920× 1080 - is the system’ s screen resolution, which you can change.
  • -i :0.0 - is the recording start point and you need to alter this.
  • -vcodec libx264 - is the video codec, which is used to record the desktop.
  • -threads 4 - is the number of threads, which can be changed.
  • /home/user1/Desktop/video - is the destination path where you can save the file.
  • .avi - is the video format and you are free to change it to, ' mov' , ' mp4' , ' flv' , ' wmv' and ' mkv' .

Run the file by your preferred multimedia player or run it by using “ avplay” multimedia player which comes along with the Libav package.

user1@linuxhelp:~$ avplay Desktop/video.avi


To Start Audio Recording of Desktop

Before starting to record the audio,check whether it is listed and make your preferred input device as default one.



Run the below command to list the complete input sources for the audio and choose your input device

user1@linuxhelp:~$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: AudioPCI [Ensoniq AudioPCI], device 0: ES1371/1 [ES1371 DAC2/ADC]
  Subdevices: 0/1
  Subdevice #0: subdevice #0
card 1: SPUSB20 [Practica SP-USB20], device 0: USB Audio [USB Audio]
  Subdevices: 0/1
  Subdevice #0: subdevice #0


Here you can see the input source for audio and my preferred device number is ' 1' . You need to replace the “ 1” number with the number of the input device that you want, whenever you list the available audio input sources. Run the below command to record audio file.

user1@linuxhelp:~$ avconv -f alsa -i hw:1 Desktop/audiofile.wav


Press ‘ q’ key to stop recording

-i hw:1 takes the audio input source from the “ hw:1” device.
-f alsa (Advanced Linux Sound Architecture)captures the sound from the alsa device.

Replace .wav with a supported audio format by Libav, and then play the audiofile.wav using a multimedia player.

To Start Video & Audio Recording of Desktop

Execute the following command to Start Video & Audio Recording of Desktop.

user1@linuxhelp:~$ avconv -f alsa -i hw:1 -f x11grab -r 24 -s 1920x1080 -i :0.0 -vcodec libx264 -threads 4 Desktop/videofile.avi



Press ‘ q’ key to stop recording

Comment
nuriman
Jul 18 2017
Can you please help me out with these 2 errors that im getting? pi@raspberrypi:~/libav $ avconv -f x11grab -r 24 -s 1920x1080 -i :0.0 -vcodec libx264 -threads 4 /home/pi/video_only.avi avconv version 11.9-6:11.9-1~deb8u1 rpi1, Copyright (c) 2000-2017 the Libav developers built on Apr 26 2017 06:57:28 with gcc 4.9.2 (Raspbian 4.9.2-10) [x11grab @ 0x4711e0] device: :0.0 -> display: :0.0 x: 0 y: 0 width: 1920 height: 1080 [x11grab @ 0x4711e0] shared memory extension found X Error of failed request: BadMatch (invalid parameter attributes) Major opcode of failed request: 130 (MIT-SHM) Minor opcode of failed request: 4 (X_ShmGetImage) Serial number of failed request: 11 Current serial number in output stream: 11 pi@raspberrypi:~/libav $ arecord -l **** List of CAPTURE Hardware Devices **** card 1: U0xac80x3420 [USB Device 0xac8:0x3420], device 0: USB Audio [USB Audio] Subdevices: 1/1 Subdevice #0: subdevice #0 pi@raspberrypi:~/libav $ avconv -f alsa -i hw:1 home/pi/audiofile.wav avconv version 11.9-6:11.9-1~deb8u1 rpi1, Copyright (c) 2000-2017 the Libav developers built on Apr 26 2017 06:57:28 with gcc 4.9.2 (Raspbian 4.9.2-10) [alsa @ 0xae200] cannot set channel count to 2 (Invalid argument) hw:1: Input/output error
Add a comment
FAQ
Q
What API is developed in Avconv for filtering purpose?
A
Libavfilter is used as a filter API on Avconv.
Q
How to list the complete input sources for the audio on Avconv?
A
In Avconv with the help of "arecord -l" you can achieve this.
Q
Will Avconv capture video in a timely manner or we need to execute any command for it?
A
The command "avconv -f x11grab - default" command used for capturing video from the X server.
Q
The audio recorded sound is not audible after recording from Avconv?
A
Make sure you have enabled external audio devices.
Q
What is the default format that comes with the recorded version of Aconv?
A
The default format is ".avi" which is the video format and you are free to change it to 'mp4', 'flv', 'wmv' and 'mkv' on Aconv.