Recording Your Linux Desktop with FFMPEG

One of the most frequent questions I get asked on my channel is "What program do use to record your videos?" And this is a very valid question. I apologize that it has taken me this long to provide a full tutorial/solution to recording your screen (screencasting) under Linux, so in this post I will endeavour to share my experiences with screencasting in hopes that you too can use these simple tools to make videos of your own!

Basically, I use a command line tool, so for those who don't like the command line, this might be breaking a bit of new ground, however the method is extremely simple, and was the very first time I had ever used the command line apart from apt-getting. Now the tool that is required to get the job done is of course ffmpeg, which can be found in nearly every distro's repositories. You can of course get the latest source code from SourceForge but will just stick with the package based install for now. Now the encoder that I always use to encode the videos in real time as I'm recording is "x264". Basically, this is a high quality H.264 encoder that when combined with ffmpeg can record very nice videos with smooth frame rates in real time even on modest hardware!

The big advantage to this is that you're not left waiting around for the video to encode like the popular "recordmydesktop" and the like. So, the two packages you need so far are ffmpeg and x264. On Ubuntu based distributions, you will also want to make sure that the 'libavdevice52 and the libavcodec52' packages are installed for Ubuntu 11.04 and earlier, and the 'libavdevice53 and libavcodec53' for 11.10 and later. For some weird reason, ffmpeg won't recognize the x264 encoder until you've installed those packages - at least on Ubuntu.

So now onto the commands themselves...it is a simple case of copy/pasting these commands into a terminal and adapting them, if necessary, to your needs. Before I go on, I need to mention a fellow YouTuber, gotbletu who posted both a video and a pastebin without which I would never have been able to start YouTubing a year ago, so this post is really a mere update on that tutorial. So these are the commands you need for various situations that I will explain below.

http://paste2.org/p/1203300

The main one that I use is:

ffmpeg -f alsa -ac 2 -i hw:0,0 -f x11grab -r 30 -s $(xwininfo -root | grep 'geometry'| awk '{print $2;}') -i :0.0 -acodec pcm_s16le -vcodec libx264 -vpre lossless_ultrafast -threads 0 -y output.mkv

so this is the one that I'll pull apart.

Now then, what does all this mean? ffmpeg -f is the base ffmpeg command, and alsa -ac 2 is basically the sound card info, but after the -i, we have the sound device which is the microphone or whatever you want to record sound from. By default in most modern Ubuntu based distributions and in a good deal of others, pulse audio is the default input manager/mixer therefore, so if you want to use the pulsesudio mixer to manage your sound input, then you'll need to change the 'hw:0,0' to 'pulse'. You can also change the maximum frame rate by changing the '-r' value. Finally, you can change all the info in brackets $(xwininfo -root | grep 'geometry'| awk '{print $2;}') to your native screen resolution if you so wish eg. -s 1440x900. That last step is critical for success on distributions post Ubuntu 11.10.

Once you have experimented with customizing the command to your needs, it's a good idea to copy/paste the magic formula into a text file and saving for future reference. With a successful command entry, hit enter, and ffmpeg will read out various capture related information such as bitrate etc. before continuously saving all of the captured video to a video file entitled 'output.mkv' in your home folder.

And that is all there is to it! Now for those who prefer a nice user interface, there is an app for Ubuntu specifically known as Kazam which uses the exact same ffmpeg backend to record your video. It has a few other nice perks, but it usually takes a while for the developers to update their PPA when a new Ubuntu release comes out.

So with all that said, happy screencasting, and feel free to leave any questions in the comments below!

Cheers.

Comments

  1. I'm on LinuxMint 12
    Terminal says that the encoder libx264 is unknown.
    I have it, Libvdevice53 and Libvcodec53 installed...

    ReplyDelete
  2. @The death You could try installing libavcodecs53-extra and see if that helps.

    ReplyDelete
  3. it solved that problem, but now it says:
    [matroska @ 0xb48be0] Application provided invalid, non monotonically increasing dts to muxer in stream 1: 1 >= 1

    ...in red. And then:
    av_interleaved_write_frame(): Invalid argument

    ...in ordinary black text

    ReplyDelete
  4. @The death!

    Ok, sorry for the late reply...I've only had my tablet for the last week so I wasn't able to troubleshoot that problem. Anyway, here's a command that seems to work for me under Mint 12:

    ffmpeg -f alsa -ac 1 -i pulse -f x11grab -r 30 -s 1440x900 -i :0.0 -vcodec libx264 -vpre lossless_ultrafast -threads 0 -y output.mkv

    Obviously, you'll need to change the 1440x900 to your resolution first, but that should work.

    Good luck!

    ReplyDelete
  5. Thanks for posting about screen recording.
    This is great stuff Brother as it will work on most of the distros and DEs without relying on special softwares.

    ReplyDelete
  6. Thanks for the info, good stuff..

    Also you can create 'bash aliases', this allows you to avoid copy/paste long commands into terminal. Gotbletu explains it better than I can,go to his channel and search 'bash aliases'.

    ReplyDelete
  7. Could you tell me what blog settings you have set up. I can't see any themes for my blog design elements that will let me have my posts as a sidebar, and it looks really nice.

    I love the vid reveiws as well. Have just downloaded Kororaa to try out.

    ReplyDelete
    Replies
    1. I've just used the 'dynamic' layouts that blogger provide, and added my own background. It doesn't have as much customization abilities as the 'traditional' layout, but I think it looks nice.

      Delete
  8. If you just want to record your desktop or a window you can use CaptureMe. It uses FFMPEG but it is GUI based application. http://screen-captureme.blogspot.com

    ReplyDelete

Post a Comment

Popular Posts