It’s one of those innevitabilities. Start with fiddling about with low level graphics system calls, do some screen capture, then some single file saving, and suddenly enough you’ve got screen capture movies! Assuming WordPress does this right.
If you’ve been following along, the relevant code looks like this:
-- Create the resource that will be used -- to copy the screen into. Do this so that -- we can reuse the same chunk of memory local resource = DMXResource(displayWidth, displayHeight, ffi.C.VC_IMAGE_RGB888); local p_rect = VC_RECT_T(0,0,displayWidth, displayHeight); local pixdata = resource:CreateCompatiblePixmap(displayWidth, displayHeight); local framecount = 120 for i=1,framecount do -- Do the snapshot Display:Snapshot(resource); local pixeldata, err = resource:ReadPixelData(pixdata, p_rect); if pixeldata then -- Write the data out local filename = string.format("screencast/desktop_%06d.ppm", i); print("Writing: ", filename); WritePPM(filename, pixeldata); end end
In this case, I’m capturing into a bitmap that is 640×320, which roughly matches the aspect ratio of my wide monitor.
This isn’t the fastest method of capturing on the planet. It actually takes a fair amount of time to save each image to the SD card in my Pi. Also, I might be able to eliminate the copy (ReadPixelData), if I can get the pointer to the memory that the resource uses.
This little routine will generate a ton of .ppm image files stored in the local ‘screencast’ directory.
From there, I use ffmpeg to turn the sequence of images into a movie:
ffmpeg -i desktop_0x%06.ppm desktop.mp4
If you’re a ffmpeg guru, you can set all sorts of flags to change the framerate, encoder, and the like. I just stuck with defaults, and the result is what you see here.
So, the Pi is capable. It’s not the MOST capable, but it can get the job done. If I were trying to do this in a production environment, I’d probably attach a nice SSD drive to the USB port, and stream out to that. I might also choose a smaller image format such as YUV, which is easier to compress. As it is, the compression was getting about 9fps, which ain’t too bad for short clips like this.
One nice thing about this screen capture method is that it doesn’t matter whether you’re running X Windows, or not. So, you’re not limited to things that run in X. You can capture simple terminal sessions as well.
This works, and it can only get better from here.
It is part of the LJIT2RPi project.