<p>Capturing the video frames in XWD format then converting to PNG yields results that are <em>almost</em> pixel perfect when encoded with ffmpeg.</p>

<p>Install <a href="http://xvidcap.sourceforge.net/" rel="nofollow">xvidcap</a>:</p>

<pre><code>mkdir -p $HOME/dev
cd $HOME/dev
svn checkout svn://svn.code.sf.net/p/xvidcap/code/trunk xvidcap-code
cd xvidcap*
./autogen.sh
LIBS="-ldl -lX11 -lXext" ./configure --prefix=/usr/local
make &amp;&amp; sudo make install
</code></pre>

<p>Capture stills at 25 frames per second:</p>

<pre><code>mkdir -p $HOME/video
cd $HOME/video
xvidcap --fps 25 --quality 100 --cap_geometry 1024x768+768+288 --file out%05d.xwd
</code></pre>

<p>This creates a series of X Windows capture files inside <code>$HOME/video</code>. Convert the files to PNG format using <a href="http://www.imagemagick.org/" rel="nofollow">ImageMagick</a> then combine into a video:</p>

<pre><code>for i in out*.xwd; do convert -define png:color-type=2 $i $(basename $i .xwd).png; done
ffmpeg -i out%05d.png -c:v huffyuv -preset veryslow -qp 0 -qscale:v 0 output.avi
</code></pre>

<p>An example script:</p>

<pre><code>echo "Click window to capture..."

INFO=$(xwininfo -frame)
WIN_GEO=$(echo $INFO | grep -oEe 'geometry [0-9]+x[0-9]+' | grep -oEe '[0-9]+x[0-9]+')
WIN_XY=$(echo $INFO | grep -oEe 'Corners:\s+\+[0-9]+\+[0-9]+' | grep -oEe '[0-9]+\+[0-9]+' | sed -e 's/\+/,/' )

OUTPUT_DIR=$HOME/video

echo Capturing to $OUTPUT_DIR...
rm -rf $OUTPUT_DIR
mkdir -p $OUTPUT_DIR
cd $OUTPUT_DIR

WIN_XY=$(echo $WIN_XY | tr , +)

xvidcap --fps 25 --quality 100 --cap_geometry $WIN_GEO+$WIN_XY --file out%05d.xwd

# Convert captured images to PNG format
echo "Converting XWD to PNG..."
for i in out*.xwd; do convert -define png:color-type=2 $i $(basename $i .xwd).png; done

ffmpeg -i out%05d.png -c:v huffyuv -preset veryslow -qp 0 -qscale:v 0 output.avi
</code></pre>

<p>This creates a high-quality video that looks sharp with <a href="http://www.videolan.org/vlc/index.html" rel="nofollow">VLC</a>, can be edited in <a href="http://www.openshotvideo.com/" rel="nofollow">OpenShot</a>, and uploaded to various video hosts. Playback with MPlayer yields slightly fuzzy and off-coloured results.</p>

CC BY-SA 3.0
2014-03-22T19:18:56.817Z
2014-03-22T21:59:29.610Z
2014-03-22T21:59:29.610Z
Dave Jarvis
<h1>Background</h1>

<p>Recording a screen-cast (no audio) in Xubuntu Linux. The following image shows a side-by-side comparison of the video (left) versus the desktop (right):</p>

<p><img src="https://i.sstatic.net/lSXXY.png" alt="Lossless encoding"></p>

<p>The screen-cast is recorded using:</p>

<pre><code>ffmpeg -r 30 -f x11grab -s $WIN_GEO -i :0.0+$WIN_XY -vcodec huffyuv -preset ultrafast -qp 0 output.mkv
</code></pre>

<p>Here, <code>$WIN_GEO</code> stores the browser dimensions (1030x794) and <code>$WIN_XY</code> has the browser location (1020x221).</p>

<pre><code>$ cat /etc/issue
Ubuntu Trusty Tahr (development branch) \n \l
</code></pre>

<h1>Problem</h1>

<p>The colours in the video differ from the colours shown on the desktop, resulting in a slightly blurry playback. I have read that this could be due to conversion between RGB and YUV colours.</p>

<h1>Question</h1>

<p>How do you record a desktop video (screen-cast) that when played back (e.g., using mplayer, VLC, or ffplay) results in an <em>exact</em> match to the original desktop colours and crispness?</p>

<h1>Related</h1>

<p>Relevant pages:</p>

<ul>
<li><a href="http://www.unixuser.org/~euske/vnc2swf/" rel="nofollow noreferrer">http://www.unixuser.org/~euske/vnc2swf/</a></li>
<li><a href="http://technically.us/pictures/test.html" rel="nofollow noreferrer">http://technically.us/pictures/test.html</a></li>
<li><a href="http://ubuntuforums.org/showthread.php?t=1392026" rel="nofollow noreferrer">http://ubuntuforums.org/showthread.php?t=1392026</a></li>
<li><a href="https://askubuntu.com/questions/347574/how-to-do-screencasting-desktop-recording-with-high-quality-audio-and-video">https://askubuntu.com/questions/347574/how-to-do-screencasting-desktop-recording-with-high-quality-audio-and-video</a></li>
<li><a href="http://step4wd.com/2012/08/03/recording-screencast-in-ubuntu-linux/" rel="nofollow noreferrer">http://step4wd.com/2012/08/03/recording-screencast-in-ubuntu-linux/</a></li>
<li><a href="https://unix.stackexchange.com/questions/73622/how-to-get-near-perfect-screen-recording-quality">https://unix.stackexchange.com/questions/73622/how-to-get-near-perfect-screen-recording-quality</a></li>
<li><a href="https://unix.stackexchange.com/questions/65970/washed-out-colours-with-screencast-software-but-not-screenshot-on-ubuntu-all-ve">https://unix.stackexchange.com/questions/65970/washed-out-colours-with-screencast-software-but-not-screenshot-on-ubuntu-all-ve</a></li>
<li><a href="https://trac.ffmpeg.org/wiki/How%20to%20grab%20the%20desktop%20%28screen%29%20with%20FFmpeg" rel="nofollow noreferrer">https://trac.ffmpeg.org/wiki/How%20to%20grab%20the%20desktop%20%28screen%29%20with%20FFmpeg</a></li>
<li><a href="http://rayslinux.blogspot.ca/2012/04/capturing-linux-desktop-via-command.html" rel="nofollow noreferrer">http://rayslinux.blogspot.ca/2012/04/capturing-linux-desktop-via-command.html</a></li>
</ul>

CC BY-SA 3.0
2014-03-21T22:10:57.660Z
What is the final context that is going to require such exacting playback?  If you plan on using the video on the Internet, it will need to be transcoded and compressed at some point which is going to result in far greater alteration and loss of clarity.  You are most likely correct about the color space shift, but I can't find any details about how to make sure ffmpeg is using huffyuv in RGB mode (which it does support).
AJ Henderson
CC BY-SA 3.0
2014-06-08T05:09:13.193Z
huffyuv in rgb should be lossless by default, is FFV1 lossless here?  If not, then the input is somehow bad...
rogerdpack
<ffmpeg><screen-casting><linux>
How to get a crisp, exact color match for lossless screen-cast?
Dave Jarvis
# How to get a crisp, exact color match for lossless screen-cast?

(Asked by: username_0 on 2014-03-21)

Background
Recording a screen-cast (no audio) in Xubuntu Linux. The following image shows a side-by-side comparison of the video (left) versus the desktop (right):

The screen-cast is recorded using:
ffmpeg -r 30 -f x11grab -s $WIN_GEO -i :0.0+$WIN_XY -vcodec huffyuv -preset ultrafast -qp 0 output.mkv

Here, $WIN_GEO stores the browser dimensions (1030x794) and $WIN_XY has the browser location (1020x221).
$ cat /etc/issue
Ubuntu Trusty Tahr (development branch) \n \l

Problem
The colours in the video differ from the colours shown on the desktop, resulting in a slightly blurry playback. I have read that this could be due to conversion between RGB and YUV colours.
Question
How do you record a desktop video (screen-cast) that when played back (e.g., using mplayer, VLC, or ffplay) results in an exact match to the original desktop colours and crispness?
Related
Relevant pages:

http://www.unixuser.org/~euske/vnc2swf/
http://technically.us/pictures/test.html
http://ubuntuforums.org/showthread.php?t=1392026
https://askubuntu.com/questions/347574/how-to-do-screencasting-desktop-recording-with-high-quality-audio-and-video
http://step4wd.com/2012/08/03/recording-screencast-in-ubuntu-linux/
https://unix.stackexchange.com/questions/73622/how-to-get-near-perfect-screen-recording-quality
https://unix.stackexchange.com/questions/65970/washed-out-colours-with-screencast-software-but-not-screenshot-on-ubuntu-all-ve
https://trac.ffmpeg.org/wiki/How%20to%20grab%20the%20desktop%20%28screen%29%20with%20FFmpeg
http://rayslinux.blogspot.ca/2012/04/capturing-linux-desktop-via-command.html

**Tags:** ffmpeg, screen-casting, linux

> **Comments:**
> **username_1**: What is the final context that is going to require such exacting playback?  If you plan on using the video on the Internet, it will need to be transcoded and compressed at some point which is going to result in far greater alteration and loss of clarity.  You are most likely correct about the color space shift, but I can't find any details about how to make sure ffmpeg is using huffyuv in RGB mode (which it does support).  
> **username_2**: huffyuv in rgb should be lossless by default, is FFV1 lossless here?  If not, then the input is somehow bad...  

---

## 1 answers

### Answer by username_0 on 2014-03-22. Score: 1 | Accepted answer

Capturing the video frames in XWD format then converting to PNG yields results that are almost pixel perfect when encoded with ffmpeg.
Install xvidcap:
mkdir -p $HOME/dev
cd $HOME/dev
svn checkout svn://svn.code.sf.net/p/xvidcap/code/trunk xvidcap-code
cd xvidcap*
./autogen.sh
LIBS="-ldl -lX11 -lXext" ./configure --prefix=/usr/local
make && sudo make install

Capture stills at 25 frames per second:
mkdir -p $HOME/video
cd $HOME/video
xvidcap --fps 25 --quality 100 --cap_geometry 1024x768+768+288 --file out%05d.xwd

This creates a series of X Windows capture files inside $HOME/video. Convert the files to PNG format using ImageMagick then combine into a video:
for i in out*.xwd; do convert -define png:color-type=2 $i $(basename $i .xwd).png; done
ffmpeg -i out%05d.png -c:v huffyuv -preset veryslow -qp 0 -qscale:v 0 output.avi

An example script:
echo "Click window to capture..."

INFO=$(xwininfo -frame)
WIN_GEO=$(echo $INFO | grep -oEe 'geometry [0-9]+x[0-9]+' | grep -oEe '[0-9]+x[0-9]+')
WIN_XY=$(echo $INFO | grep -oEe 'Corners:\s+\+[0-9]+\+[0-9]+' | grep -oEe '[0-9]+\+[0-9]+' | sed -e 's/\+/,/' )

OUTPUT_DIR=$HOME/video

echo Capturing to $OUTPUT_DIR...
rm -rf $OUTPUT_DIR
mkdir -p $OUTPUT_DIR
cd $OUTPUT_DIR

WIN_XY=$(echo $WIN_XY | tr , +)

xvidcap --fps 25 --quality 100 --cap_geometry $WIN_GEO+$WIN_XY --file out%05d.xwd

# Convert captured images to PNG format
echo "Converting XWD to PNG..."
for i in out*.xwd; do convert -define png:color-type=2 $i $(basename $i .xwd).png; done

ffmpeg -i out%05d.png -c:v huffyuv -preset veryslow -qp 0 -qscale:v 0 output.avi

This creates a high-quality video that looks sharp with VLC, can be edited in OpenShot, and uploaded to various video hosts. Playback with MPlayer yields slightly fuzzy and off-coloured results.


---

