How to capture screen using ffmpeg on linux

By | August 30, 2015

capture screen

So many times you take screenshots of your desktop for different purposes but sometimes a screenshot only is not enough. You may need to record your desktop screen for different purposes like –

  • Creating a video tutorials.
  • Recording a training sessions.
  • Unattended monitoring of your desktop.
  • Monitoring machines remotely.
  • Broadcasting your desktop screen on LAN.

In this guide I’ll show you how to capture screen using ffmpeg on linux with sound and voice over (from Microphone) using free software – ffmpeg .

Ffmpeg is a feature rich video manipulation command line tool. It is open source and freely available to download. Ffmpeg is available for windows/Linux/Mac.

This tutorial is written and tested on Fedora 19 LXDE. These steps should work for other RedHat based distribution also but if you face any problem feel free to ask me in comment section.

How to install ffmpeg

Pre-compiled builds of ffmpeg are available to download form ffmpeg site. To download Click here.

For Redhat/CentOS & Fedora Yum repository is available on rpmfusion and on Ubuntu you can install using apt-get install ffmpeg command.

ffmpeg builds available on ffmpeg official site, rpmfusion or Ubuntu repositories may not include x11grab encoder which is required for capturing Linux desktop. If ffmpeg is not compiled with x11grab and alsa, you will get error- “Unknown input format: x11grab/alsa”.

If you are getting error – “Unknown input format : x11grab” , uninstall old version of ffmpeg and compile ffmpeg from source code.

How to compile ffmpeg from source code

Install dependencies

Open terminal and run following commands as superuser or root.

yum install autoconf automake cmake freetype-devel gcc gcc-c++ git libtool make mercurial nasm pkgconfig zlib-devel

Create a new directory where you can download & compile required packages

mkdir ~/ffmpeg_sources

 

Yasm

Yasm is required for libx264 encoder.

cd ~/ffmpeg_sources
git clone --depth 1 git://github.com/yasm/yasm.git
cd yasm 
autoreconf -fiv
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" 
make 
make install 
make distclean

 

libx264

libx264 is opensource library for encoding H.264/Mpeg-4 AVC format.

cd ~/ffmpeg_sources
git clone --depth 1 git://git.videolan.org/x264
cd x264
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-static
make
make install
make distclean

 

libfdk_aac

libfdk_aac is used to encode aac audio.

cd ~/ffmpeg_sources git clone --depth 1 git://git.code.sf.net/p/opencore-amr/fdk-aac
cd fdk-aac 
autoreconf -fiv
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install
make distclean

 

libmp3lame
cd ~/ffmpeg_sources
curl -L -O http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
tar xzvf lame-3.99.5.tar.gz 
cd lame-3.99.5
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --disable-shared --enable-nasm 
make
make install
make distclean

 

x11grab
yum install libX11-devel

 

ALSA
yum install alsa-lib-devel

 

ffmpeg

cd ~/ffmpeg_sources
git clone --depth 1 git://source.ffmpeg.org/ffmpeg
cd ffmpeg
PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure --prefix="$HOME/ffmpeg_build" --extra-cflags="-I$HOME/ffmpeg_build/include" --extra-ldflags="-L$HOME/ffmpeg_build/lib" --bindir="$HOME/bin" --pkg-config-flags="--static" --enable-gpl --enable-nonfree --enable-libfdk_aac --enable-libfreetype --enable-libmp3lame --enable-libx264 --enable x11grab --enable alsa
make
make install
make distclean
hash -r

How to capture screen using ffmpeg

ffmpeg basic command line syntax –

ffmpeg [ input format & souce ] [output format & souce]

Record desktop without audio.

ffmpeg -f x11grab -i :0.0 -s 1024x768 -f mp4 record.mp4

-s : Screen resolution

To auto select screen resolution

ffmpeg -f x11grab -i :0.0 -r 25 -s `xdpyinfo | grep 'dimensions:'|awk '{print $2}'` -f mp4 record.mp4

 

Record desktop with audio

To capture video with audio, run following command in terminal.

ffmpeg -f x11grab -i :0.0 -r 25 -s `xdpyinfo | grep 'dimensions:'|awk '{print $2}'` -f alsa -ac 2 -i pulse -f mp4 record.mp4

Minimize terminal and Open Volume control. To open volume control from terminal you can run

pavucontrol

Choose required source of audio which you want to capture.

Select audio source in linux for ffmpeg

Select audio source in linux for ffmpeg

Record desktop with voice over (audio from Headphone)

ffmpeg -f x11grab -i :0.0 -r 25 -s `xdpyinfo | grep 'dimensions:'|awk '{print $2}'` -f alsa -ac 2 -i pulse -f mp4 record.mp4

Run command in terminal and Open Volume Control, Select “Input Devices” and select required input.

Monitor desktop from remote system on LAN

Suppose we want to monitor machine ‘A‘ from  machine ‘B‘. Both machines are in LAN. Machine B has ip address : 192.168.0.10
On machine A, Run following command in background.

ffmpeg -f x11grab -i :0.0 -r 25 -s `xdpyinfo | grep 'dimensions:'|awk '{print $2}'` -f alsa -ac 2 -i pulse -f mpegts udp://192.168.0.10:5000&

 

On machine B you can play video in VLC media player.
Open VLC, Go to Media=>Network =>type – udp://@:5000

Broadcast your screen over LAN (Local Area Network)

It is assumed that all systems are connected via LAN and Multicast streaming is not blocked in Network.

On machine which screen you want to broadcast, run command in terminal

ffmpeg -f x11grab -i :0.0 -r 25 -s `xdpyinfo | grep 'dimensions:'|awk '{print $2}'` -f alsa -ac 2 -i pulse -f mpegts udp://@224.2.2.2:10000

To Play stream on remote machines
Open VLC, Go to Media =>Network => Type “udp://@224.2.2.2:10000” without quotes.

Do you have any suggestions or questions related to this story. Please feel free to ask in comment section.

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz