You are not logged in.

#1 2009-11-21 10:52:46

Neuro
Member
From: Poland
Registered: 2005-10-12
Posts: 352

x264 Lossless Screencast Script

Hi,

Lately I've been faced with the problem of creating a lossless screencast under Linux. Although recordmydesktop and instanbul are fine, they're quite limited by their quality. A friend of mine showed my that under Windows you can create a lossless, small screencast using x264 and CamStudio. Since x264 is also avaialble on Linux, I thought it would be a nice thing to try out. After a bit of hacking and trial and error, lossless_screencast.sh was born.

Requirements:
- FFMPeg (req >= 0.5) [Arch default]
- x264 (req X264_BUILD  >= 67) [Arch default]
- Bash 3
- xwininfo

Features:
- auto select window to capture
- small file output
- auto truncate capture region (x264 requires video dimensions to be multiplies of 16)
- exchangeable x264 lossless presets
- able to capture full screen

Future features:
- encode to wmv2
- encode to flv
- capture audio from microphone

Note: Since ffmpeg's x11grab is used, this script is unusable under ATI proprietary drivers (catalyst/fglrx). Under these drivers X process takes 100% CPU and even moving a mouse is impossible. I've tested with open source ATI drivers and they work fine. Intel drivers also work fine.

The script is available here:
http://continuity.intelink.pl/~neuro/pr … eencast.sh

Offline

#2 2009-11-21 15:30:33

quantumphaze
Member
From: Melbourne, Australia
Registered: 2008-11-14
Posts: 175

Re: x264 Lossless Screencast Script

Very nice. I like how it automatically calls xwininfo to grab a specific window instead of requiring you to find the window id yourself.

It shaves off the right and bottom by a few pixels when height/width isn't a multiple of 16. Why can't ffmpeg work with height/width that isn't a multiple of 16?


▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▏▎▍▌▋▊▉█▇▆▅▄▃▂▁

Offline

#3 2009-11-21 17:46:58

Neuro
Member
From: Poland
Registered: 2005-10-12
Posts: 352

Re: x264 Lossless Screencast Script

It's because of quality. x264 uses blocks of 16x16 for compression. If the sizes aren't the multiplies of 16 quality suffers a lot and you loose all the lossless goodness.

Offline

#4 2009-11-22 02:54:09

anonova
Member
Registered: 2009-07-22
Posts: 23

Re: x264 Lossless Screencast Script

Neuro wrote:

It's because of quality. x264 uses blocks of 16x16 for compression. If the sizes aren't the multiplies of 16 quality suffers a lot and you loose all the lossless goodness.

This is incorrect. Yes, x264 will pad the edges to be a multiple of 16, causing useless data to be encoded, but the quality difference between a mod 16 resolution and one that's not is negligible.

Last edited by anonova (2009-11-22 02:55:36)

Offline

#5 2009-11-22 04:52:42

Ranguvar
Member
Registered: 2008-08-12
Posts: 2,544

Re: x264 Lossless Screencast Script

anonova is completely correct.  I'm very experienced with x264, and what it does is it mirrors the edge of the video you provide to create a 16x16 video, and then crops that part off.

In the end, the quality difference is _extremely_ tiny.
See: http://git.videolan.org/gitweb.cgi?p=x2 … f2d4847a96

By the way, Arch's x264 is very out-of-date.  There have been tons of improvements lately; I advise anyone who wants to use this to grab the very latest r1342 smile
(x264 devs still use SVN revision numbers even though they use Git now, see: http://x264.nl/)

Offline

#6 2009-11-22 08:28:55

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: x264 Lossless Screencast Script

Hi, Neuro
I've modified your script, adding stuff like:

* support for capturing a rectangle area defined by multiple windows (think of GIMP)
* fixed issue with windows with thick borders
* added ignore window border option (--borderless)
* fixed issue when window exceeds screen area
* expand to mod 16 rather than truncate, whenever possible
* default output filename makes -o option optional

A sample screencast can be downloaded here:
http://omploader.org/vMnR3dw
I have to play it with:

$ mplayer -noconfig all ffcast.mkv

or mplayer will screw up (vdpau sucks probably)

Edit:
web-view link to my script: http://github.com/lolilolicon/ffcast/blob/master/ffcast
Or pull it to your local directory:

git clone git://github.com/lolilolicon/ffcast.git

Edit 2:
AUR page here:
http://aur.archlinux.org/packages.php?ID=32158

Last edited by lolilolicon (2009-11-22 14:53:49)


This silver ladybug at line 28...

Offline

#7 2009-11-22 11:16:24

Neuro
Member
From: Poland
Registered: 2005-10-12
Posts: 352

Re: x264 Lossless Screencast Script

anonova wrote:
Neuro wrote:

It's because of quality. x264 uses blocks of 16x16 for compression. If the sizes aren't the multiplies of 16 quality suffers a lot and you loose all the lossless goodness.

This is incorrect. Yes, x264 will pad the edges to be a multiple of 16, causing useless data to be encoded, but the quality difference between a mod 16 resolution and one that's not is negligible.

That's really nice to know smile I've based my assumption on the fact that ffmpeg reported that sizes which are not multiplies of 16 may cause serious quality degradation. And since at first I've been trying with -cqs it seemed to have some impact (as compared to the Camstudio+x264 screencast), I presumed it would still have some in -crf.

lolilolicon wrote:

Hi, Neuro
I've modified your script, adding stuff like:

Cool smile It seems you've got far more bash experience than I do wink I'm pretty impressed with all the bash gizmos I wasn't aware even existed big_smile
There are two things I'd like to point out:
1. IMHO Borderless should be default smile
2. Putting ffmpegs err output to /dev/null is IMHO not a good idea. I presume you wanted to get rid of the ffmpeg's configuration dump, but if something goes wrong it would fail silently.

Still I think your version is really cool. Maybe you should put it in AUR? smile

Cheers,
M

Last edited by Neuro (2009-11-22 12:01:46)

Offline

#8 2009-11-22 11:54:13

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

Re: x264 Lossless Screencast Script

this works really nice. I wish recordmydesktop would include such tweaks. Its ability to follow the mouse is really nice for online tutorials. But atm, it doesnt work with e.g. plasma at all...


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

#9 2009-11-22 12:41:06

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: x264 Lossless Screencast Script

Neuro wrote:
lolilolicon wrote:

Hi, Neuro
I've modified your script, adding stuff like:

Cool smile It seems you've got far more bash experience than I do wink I'm pretty impressed with all the bash gizmos I wasn't aware even existed big_smile
There are two things I'd like to point out:
1. IMHO Borderless should be default smile
2. Putting ffmpegs err output to /dev/null is IMHO not a good idea. I presume you wanted to get rid of the ffmpeg's configuration dump, but if something goes wrong it would fail silently.

Good observations
1. Now borderless is default, and you can use -B to take borders into account.
2. added --debug option. When turned on, those info all goes to stderr. I turn them all to /dev/null by default since they flood all over the screen, flushing my ":: info" out.
3. Now ffcast by default makes the capture frame size to mod 2, which is *required* by ffmpeg; you can force it to mod 16 by using the --mod16 option. This default behavior makes your capture really close to the area you select (+/- 1 pixel)

Neuro wrote:

Still I think your version is really cool. Maybe you should put it in AUR? smile

Cheers,
M

Ah... an AUR package?... wait...
:: Here's the news:
http://aur.archlinux.org/packages.php?ID=32158
Let me know if there's any problems with the PKGBUILD/ffcast

Last edited by lolilolicon (2009-11-22 12:46:42)


This silver ladybug at line 28...

Offline

#10 2009-11-22 17:31:05

JohannesSM64
Member
From: Norway
Registered: 2009-10-11
Posts: 623
Website

Re: x264 Lossless Screencast Script

By the way, Arch's x264 is very out-of-date.  There have been tons of improvements lately; I advise anyone who wants to use this to grab the very latest r1342

Indeed. I recommend sudo pacman -R x264 ffmpeg mplayer && yaourt -S x264-git ffmpeg-svn mplayer-svn smile

Offline

#11 2009-11-22 18:27:03

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: x264 Lossless Screencast Script

*NOTICE: -f now equals to --format, while -a equals to --fullscreen
*added function listEVcodecs() valueEVcodec() listEVformats() valueEVformat()
*added options: --format --guessfmt --noguessfmt
*removed old crappy mkv|avi|mp4 extention check

Anyone using ffcast, Pay Attention to NOTICE please! (above)
I believe it's not a good idea to change an old option to a new name/letter, but I think I'd better do it this time:
-a for --fullscreen is quite easy to accept, it's like "all", meaning to capture all of the screen... xD
-f for --format and there is a reason, see man ffmpeg and search for -f option, then you'll see me wink

Now ffcast does accept basically any output format ffmpeg will accept. Either use --format <fmt>, or use a filename extension with --guessfmt (which is default) to try it out!
I'm wondering how --format will conflict with --codec though.
Ah, you can use `--format list' to print out a list of available output formats; the same goes with `--codec list'


This silver ladybug at line 28...

Offline

#12 2009-11-22 18:31:42

Ranguvar
Member
Registered: 2008-08-12
Posts: 2,544

Re: x264 Lossless Screencast Script

JohannesSM64 wrote:

By the way, Arch's x264 is very out-of-date.  There have been tons of improvements lately; I advise anyone who wants to use this to grab the very latest r1342

Indeed. I recommend sudo pacman -R x264 ffmpeg mplayer && yaourt -S x264-git ffmpeg-svn mplayer-svn smile

Or just edit the x264 PKGBUILD to grab the latest source smile

Offline

#13 2009-11-22 18:40:50

JohannesSM64
Member
From: Norway
Registered: 2009-10-11
Posts: 623
Website

Re: x264 Lossless Screencast Script

yaourt -S x264-git grabs a PKGBUILD that's already edited to grab the latest source smile

Offline

#14 2009-11-22 19:24:33

Ranguvar
Member
Registered: 2008-08-12
Posts: 2,544

Re: x264 Lossless Screencast Script

JohannesSM64 wrote:

yaourt -S x264-git grabs a PKGBUILD that's already edited to grab the latest source smile

I know smile

Offline

#15 2009-11-23 08:55:10

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: x264 Lossless Screencast Script

Ever missed `scrot -s' when capturing with ffcast?
Now here's the great good news!!!
This new option -s gives you just about the same behavior as with `scrot -s'
Now you are free to select any area of your screen to be captured by ffcast!
Ahh, and I made this behavior default. To roll back to the old xwininfo approach, just use -S option.

AUR package updated. Builds fine, but may need some correction as per prophecy tongue

Edit: the default for now is -S, rolled back to old behavior
      there is an issue with xrectsel.c, it causes high CPU usage... any ideas?
      new option --duration: now you don't have to press [q] to finish capture, \
                                    it quits if given duration is reached, \
                                    and it understands time in seconds or in hh:mm:ss.xxx \
                                    (and that's why bc is added as new dependency)

Last edited by lolilolicon (2009-11-23 17:07:00)


This silver ladybug at line 28...

Offline

#16 2009-11-24 11:25:27

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: x264 Lossless Screencast Script

Hi ppl,

Ever noticed the high CPU usage caused by -s option?
It was caused by some mistake in xrectsel.c.
And now it is fixed, and many thanks to HashBox!
Maybe... It might be further optimized.

Anyway, update now for your benefit!

Cheers

Edit:

There's another thing I forgot to mention, output to gif did not work due to bug in ffmpeg.
According to Dark_Shikari, ffmpeg's gif output is "better than nothing"
Well now ffcast does support gif output, just very HUGE
-- ffmpeg seems to support only uncompressed gif output.

A solution for now is to use `-o -` and pipe stdout to convert, like this:

ffcast -s -t 5 -r 3 -f gif -o - | convert gif:- cast.gif

Last edited by lolilolicon (2009-11-24 11:33:47)


This silver ladybug at line 28...

Offline

#17 2009-11-25 10:22:15

Neuro
Member
From: Poland
Registered: 2005-10-12
Posts: 352

Re: x264 Lossless Screencast Script

Wow lolilolicon, I have to say I'm pretty impressed with the feature list that ffcast had gained recently smile

Offline

#18 2009-11-26 07:57:05

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

Re: x264 Lossless Screencast Script

For some reasons my colors look kinda washed out in the resulting video - any clue what might cause this? Looks like jpeg compression...

This was my command: ffcast -b -n 3 -o ~/video.mkv

Last edited by Rasi (2009-11-26 07:57:33)


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

#19 2009-11-26 09:21:24

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: x264 Lossless Screencast Script

@Neuro, thanks. I find almost everybody who uses it likes it. I'm glad for this, and for your initial idea being extended by my very self. I'm a bit impressed too tongue And yet still this is a work in progress smile

I got news:

:: Added new option: -j,--xjustcap <geometry>
The -j option enables the user to tell ffcast about the exact capture area using <geometry>, thus no manual selection needed.
This is free selection, like -s, just without the need of touching your mouse.
I want <geometry> to be as human-readable as possible; currently supported formats are:
  *1) "(x1,y1) (x2,y2)"
        (x1,y1) and (x2,y2) are the positions of two diagonal corners of the rectangle area in question.
        The comma between xN and yN is required, but the parentheses are optional.
        Any of the xN yN parameters consists of digits, or is a percentage value written as digits%
        If any of xN yN is written as percentage, e.g. 20%, it is equivalent to 20% of the full x or y resolution of you screen.
        Example command:
        ffcast -j '20%,30% 960,600' -o cast.mkv
  *2) "wxh-x+y Southeast"
        w: width of the rectangle area.
        h: height of the rectangle area.
        -x: additional horizontal positioning of the rectangle, added to the initial positioning by gravity.
        +y: additional vertical positioning of the rectangle, added to the initial positioning by gravity.
        Southeast: gravity. It initially puts the rectangle at the southeast corner of the screen. All eight directions are valid as gravity, plus center(or centre).
        Any of w, h, x, y parameters consists of digits, or is a percentage value written as digits%
        Example Command:
        ffcast -j '848x640+4-6 southwest' -o cast.mp4

:: Changed old option -S to -w, and -m to -M. For good reasons wink

:: Added manpage. Though it's incomplete by far.

:: Bug fix

@Rasi
Maybe I'll have to improve the ffmpeg command line?
I think it possibly has something to do with your video driver.
Try update ffcast to the latest version, run your command with --debug, and look for `(DD)  ffmpeg: command line:'
You can try adjusting this ffmpeg command line by yourself. Test a bit, and you may get a better result. If you don't, just post the ffmpeg command line output and the resulting video here, or preferably #ffmpeg wink
Don't forget to post again if you get a better result tongue

Regards

Last edited by lolilolicon (2009-11-26 09:26:29)


This silver ladybug at line 28...

Offline

#20 2009-11-28 14:59:36

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: x264 Lossless Screencast Script

Cooked up a configuration file... Just for completeness

#############################################################################
##                         FFCAST CONFIGURATION FILE                         
#############################################################################
## This is a configuration file for ffcast.                                 
## A file containing default settings can be found at:                       
## $PREFIX/share/ffcast/doc/ffcast.conf                                     
## Where $PREFIX is usually either /usr or /usr/local due to build settings.
## User configuration file is located at:                                   
## $XDG_CONFIG_HOME/ffcast/ffcast.conf                                       
## If $XDG_CONFIG_HOME is not defined, it's replaced by $HOME/.config       
#############################################################################
##                              NOTES ON SYNTAX                             
#############################################################################
## Basically this is a bash script, with some extra restrictions:           
## 1. Only lines with only key='value' pairs are read (effective lines).     
## 2. Note you MUST put a pair of SINGLE quotes around value, e.g.           
##    FPS='20' is valid, while FPS="20" is not.                             
## 3. You can optionally add #comments at the end of effective lines.       
## 4. You must follow the bash syntax.                                       
##    FYI, you put a single quote inside two single quotes using '"'"', e.g.
##    OUTPUT='Annie'"'"'s.mkv' will assign OUTPUT as Annie's.mkv             
## 5. Some keys may be NULL, that means either key= or key=''               
#############################################################################

Removed -g and -G option. For the behavior of -g, one now uses -f '' -c ''
Updated man page and Makefile.

Last edited by lolilolicon (2009-11-28 15:00:56)


This silver ladybug at line 28...

Offline

#21 2009-12-06 22:51:52

DrZaius
Member
Registered: 2008-01-02
Posts: 193

Re: x264 Lossless Screencast Script

Nice script, Neuro and lolilolicon.  Audio capture would be a great feature (I see it in the first post as a future feature).

Offline

#22 2009-12-07 09:52:17

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: x264 Lossless Screencast Script

Hi, DrZaius. Recording sound isn't that hard, but the thing is that people use different sound drivers, i.e. OSS or ALSA. The majority use ALSA nowadays, if I'm not mistaken.

*1)
If you're using OSS, then you can tell ffmpeg to record sound with something like

$ ffmpeg -f oss -ac 2 -i /dev/dsp -acodec pcm_s16le test.wav

*2)
If you use ALSA, I you cannot use ffmpeg to record sound AFAIK. (Although I've seen somewhere else `ffmpeg -f alsa', so alsa support might be enabled at compile time.)
Anyway you may use arecord from alsa-utils

$ arecord -l # find out your device
**** List of CAPTURE Hardware Devices ****
card 0: Intel [HDA Intel], device 0: ALC660-VD Analog [ALC660-VD Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: Intel [HDA Intel], device 6: Si3054 Modem [Si3054 Modem]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

# so it is 0,0, now let's record
$ arecord -D hw:0,0 -fS16_LE -r44100 -c2 test.wav

Also there is AOSS which is the userland-level OSS emulation, which I myself have disabled by putting !snd-pcm-oss in MODULES array in rc.conf due to some funky flash stuff (I don't have /dev/dsp). But if you have it enabled, you may be able to just use ffmpeg -f oss as seen above (I may be wrong).
------------------
And if you want to record sound in good quality, you must adjust your mixer settings carefully. I've never experimented with it myself so I can't say much, but it is truly very important.
Also you may want to normalize your sound file after recording with sox or audacity.

To mix video and sound, you may do something like

ffmpeg -isync -i video.mkv -i sound.wav -acodec libmp3lame -ab 192k -vcodec copy cast.mkv

I think it also possible to do the mixing on the fly by mkfifo and backgrounding video/sound recording.

But sound recording may not be implemented into ffcast too soon I think. If only ffmpeg support this better. But further suggestions are always welcome.

Last edited by lolilolicon (2009-12-07 09:55:11)


This silver ladybug at line 28...

Offline

#23 2010-02-02 20:31:47

robmaloy
Member
From: Germany
Registered: 2008-05-14
Posts: 263

Re: x264 Lossless Screencast Script

hi, i get the following error (doesn't matter if with "-s" or not)

$ ffcast -s --debug
(DD)  Checking display :0.0 with `xrectsel :0.0 checkdpy'
xrectsel: Valid display_name :0.0
(DD)  VCODEC: -vcodec libx264 -vpre main -vpre lossless_slow -crf 0
(DD)  FORMAT: -f h264
(DD)  geometry: root: 1680x1050
==>  Select screen area to be captured by mouse drag
  ->  Drag mouse with button one pressed
  ->  Press any key on keyboard to abort
(DD)  geometry: mod 2: 296x142
(DD)  geometry: in screen:296x142+505+382
==>  Capture geometry: 296x142+505+382
(DD)  ffmpeg: command line:
ffmpeg -r 15 -v 1 -s 296x142 -an -f x11grab -i :0.0+505,382 -vcodec libx264 \
-vpre main -vpre lossless_slow -crf 0 -f h264  -sameq "ffcast.100202213056.mkv"
==>  Capture started, press [q] to finish
FFmpeg version UNKNOWN, Copyright (c) 2000-2010 Fabrice Bellard, et al.
  built on Jan 10 2010 17:47:18 with gcc 4.4.2 20091208 (prerelease)
  configuration: --prefix=/usr --enable-gpl --enable-libmp3lame --enable-libvorbis \
--enable-libfaac --enable-libfaad --enable-libxvid --enable-libx264 --enable-libtheora \
--enable-postproc --enable-shared --enable-pthreads --enable-x11grab \
--enable-libopencore_amrnb --enable-libopencore_amrwb --enable-version3 --enable-nonfree
  libavutil     50. 7. 0 / 50. 7. 0
  libavcodec    52.45. 0 / 52.45. 0
  libavformat   52.46. 0 / 52.46. 0
  libavdevice   52. 2. 0 / 52. 2. 0
  libswscale     0. 8. 0 /  0. 8. 0
  libpostproc   51. 2. 0 / 51. 2. 0
:0.0+505,382: no such file or directory

edit: inserted some line breaks

Last edited by robmaloy (2010-02-02 20:33:25)


☃ Snowman ☃

Offline

#24 2010-02-02 21:37:05

DrZaius
Member
Registered: 2008-01-02
Posts: 193

Re: x264 Lossless Screencast Script

lolilolicon,

Why do you include -vpre main when performing a capture using default settings?  The -vpre lossless_slow option enables the options that -vpre main disables, and since the order of presets matters, -vpre main is essentially being ignored.

Also, you should re-enable -threads 0 for libx264.  I think "0" is usable only by libx264, and other encoders need the user to manually specify the correct number of threads.  I recommend changing line 456 to (ignoring the -vpre main suggestion):

VCODEC="libx264 -vpre main -vpre ${PRESET} -crf 0 -threads 0" ;;

Offline

#25 2010-02-03 03:10:06

GogglesGuy
Member
From: Rocket City
Registered: 2005-03-29
Posts: 610
Website

Re: x264 Lossless Screencast Script

This works very nice! Thanks!

Offline

Board footer

Powered by FluxBB