How to make a screencast using ffmpeg

For those who don't know, a screencast it is a digital recording of computer screen output, sometimes containing audio narration.

En Linux, there are several tools to make screen casting. However, those who prefer to do so consuming minimal resources they can't stop reading this article.


Using 2 tools (ffmpeg to capture the screen and xwininfo to obtain the coordinates and dimensions of the window to be recorded) I managed to make screencasts using minimal resources and obtaining optimal results.

The script to start the screencast

#! / Bin / bash

# Get the coordinates and size of the selected window
# This excludes window decoration.
  unset xywh
  eval $ (xwininfo -frame |
    sed -n -e "s / ^ + Absolute upper-left X: + ([0-9] +). * / x = 1 / p"
           -e "s / ^ + Absolute upper-left Y: + ([0-9] +). * / y = 1 / p"
           -e "s / ^ + Width: + ([0-9] +). * / w = 1 / p"
           -e "s / ^ + Height: + ([0-9] +). * / h = 1 / p")
$ w = $ w + $ w% 2 # that the width is a multiple of 2, otherwise ffmpeg complains
WIN_XY = $ x "," $ y # format XY coordinates
WIN_GEO = $ w "x" $ h # format window size
# notify-send will display a message indicating the start of the screencast.
# run ffmpeg with the parameters that suit your configuration.
notify-send "Starting screencast ..." && ffmpeg -f alsa -i hw: 0 -f x11grab -r 25 -s $ WIN_GEO -i: 0.0 + $ WIN_XY -acodec libmp3lame -async 1 -vcodec libx264 -preset ultrafast - crf 0 -threads 0 save.mp4

You don't need to fully understand everything this script does. Simply following the comments is enough. However, it is very possible that you will have to change some parameters in the line where ffmpeg is executed to adjust them to your machine configuration.

Audio architecture

1) If you use OSS, replace -f alsa -i hw: 0 with -f oss -i / dev / dsp

2) If you use only ALSA, you have to use the parameters that come in the script (-f alsa -i hw: 0). To determine the number after hw: you can run aplay -l and choose the number of the appropriate sound card.

3) If you use Pulse Audio (Ubuntu and derivatives use this), use the parameters -f alsa -ac 1 -i pulse.

FPS

-r 25 indicates the fps (frames per second or frames per second) you want to record at. 25 is a good option.

Audio, video and sync codec

-acodec libmp3lame is the audio codec. I chose to record it in mp3. You can use any other.

-async 1 allows synchronization of audio with video.

-vcodec libx264 -preset ultrafast -crf 0 -threads 0, tells ffmpeg that the video codec to use is x264 and that the preset is ultrafast (there is fast, slow, etc), otherwise it will not record correctly at the desired amount of fps. The -crf 0 parameter indicates the compression level (the lower the compression, the lower the compression). Finally, threads 0 indicates the number of threads to use, when passing 0 ffmpeg calculates it automatically. As with the audio settings, you can choose other options.

All these settings were the ones that gave me the best results: a relatively small file, a good video quality, with synchronized audio and no lags. However, I recommend you dive into the ffmpeg documentation to discover others.

The script to end the screencast

#! / Bin / bash
notify-send "Ending screencast ..." && killall ffmpeg
For both scripts to work you must have notify-send installed. All Ubuntu-derived distributions should come with this tool installed. In Arch and derivatives: sudo pacman -S libnotify.

ffcast

Looking for information to write the scripts, I discovered that someone had an idea very similar to mine. Ffcast is a script that uses the same tools (ffmpeg and xwininfo). I mention it because some may find it more convenient to use this script.

In Arch and derivatives, I opened a terminal and ran:

yaourt -S ffcast

The rest, you can download from here.

To end…

Once you have created and saved the scripts saved in your HOME folder, you have to grant it execution permissions. Assuming start_screencast and finish_screencast are called, run the following commands in a terminal:

chmod + x start_screencast
chmox + x terminate_screencast

Finally, it only remains to assign each of the scripts to 2 shortcuts (shortcuts) that are comfortable for you. That way, the scripts will run when you press the selected key combination.


Leave a Comment

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

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Michael Sp said

    Since I got to know Linux, I have asked myself exactly the same thing that was just detailed in this comment. it would be an ambitious project.

  2.   pmf said

    The tutorial seems good to me, but how do you say something easy for novice users? Well, the one that seems best to me is KAzam, it is easy to use and does not consume much in my opinion !!!! Cheers

  3.   Let's use Linux said

    Not all. In addition, I honestly believe that there is no good interface for screencasting (that has everything I need), that works with the formats I want and that consumes few resources.

    In my case, the terminal is the best option. I do not pretend that it is for everyone, but it is good to share what one is learning, right?

    Cheers! Paul.

  4.   Jorge Rocha said

    It is wrong that if we want to expand the OS under GNU / linux we advise users to do that kind of thing in the terminal people want something easy and functional

    @ingjorgerocha

  5.   why am i juan said

    At the server level I also use only console, no graphical environment ... it is the most practical and I do everything from there (automate backups for example as you well say ...), but, on a machine that you have as a server ... for what do you want to do a screencast on it? hehe..ffmpeg is very very valid but I think the console issue should go to the background depending on which distributions.
    Since we have so much variety of distros in the Linux world, and the console is perfected enough to work properly in all situations (it is almost infallible rather ..), we should start improving the apps for the public, start to polish, code and graphic aspect ... which is what is demanded today.
    We have quite archaic and abandoned Linux projects, we all know that ... a good example, for me, is why there is no longer an executable file format that allows you to take a program on a pendrive and simply double click and a simple installation apart from requiring repositories (and therefore network) .. no APTonCD, this is outdated too .. we need a standard, a ".tux" or something similar to a ".exe" of Windows. Is it a matter of space ? .. in Windows, the Gimp executable occupies 72mb and the same one for Linux (.deb) occupies 4,2mb ... why is this? Well, because the Gimp program itself, in Windows it is accompanied by all the libraries to make sure that the program works when installed ... in Linux we could have something the same, the executable would occupy more, we would lose the advantage of size but we gain in portability (and more with the memories so large that we carry today in our pockets ... what difference does it make to carry 4mb than 72mb if almost all of us carry 8gb pendrives?)
    All these things must be polished ... the system is stable, has great support and driver compatibility ... why don't we improve what there is? ... if someone is encouraged, we start the executable project hehe. !

  6.   hidabe said

    Uhm ... well, people who love the console do not love it because dear friend, they love it because with it many things can be automated, for example, at the server level (although this is not the case).

  7.   why am i juan said

    Honestly, the console is very useful ... but there are programs with some GUI that use ffmpeg and are more attractive and constructive for possible new Linux users ... I recommend RecordMyDescktop which is in the repositories of almost all the distros. I know there are many people who love the console, but this must be finished with as much as possible ... Linux must be focused on the desktop and a possible end user who fundamentally wants that, a graphical interface and if possible beautiful and practical ... is it so difficult? that we cannot let ourselves be stepped on ... Linux has tremendous potential and we must squeeze it!
    In spite of everything, it is a great program! Good post ^^

  8.   why am i juan said

    The truth is, yes, it is great to share what one is learning ... This is not a criticism in the devaluing sense of the post, but a criticism of why more people do not use Linux ... whatever they say, the console should be reduced to a second plane to give way to graphical facilities (this does not mean that the possibility of doing things through the console is ruled out, but that whoever wants to use it and whoever does not, well ...)
    At the end of the day, what sells, what is striking is the ease of operating an OS ... Linux is internally unstoppable, but if we add to that ease of use and intuitive interfaces ... we win users and that It benefits the development of the community, the different programs, support from video game companies and therefore specific hardware (say graphics cards ...), creation of specialized software that currently only exists on Windows / Mac ... I don't know, I think this is in the 15M plan ... all very nice but now it's time to change course a bit ... and I say a little!

    Well, the standard executable project in Linux still stands, if someone reads it and wants us to start collaborating, here is my email, I am not hiding hehe (whysoyjuan@gmail.com)

    Greetings and I hope to see more comments here ... be they criticism or praise, the fact is that we have to talk.Xao

  9.   Let's use Linux said

    Good! Good contribution.
    Hug! Paul.

  10.   Gerardo said

    What is this program about ... be careful, I'm not very knowledgeable about this!

    1.    let's use linux said

      Generally, it is used to convert videos. In this case, we demonstrate how to use it to record your desktop and make a video tutorial, for example.
      Hug! Paul.

  11.   tarribalis said

    Thank you. In resolution, eg 1280X800, the X must be capitalized for the ffmpeg command to work.