How to record Virtual Machine Manager QEMU/KVM virtual machine screen? I don't want to record the host Virtual Machine Manager window, nor record the screen from the virtual machine.
2/2
Jul 21, 10:41am

Recording the screen of a QEMU/KVM virtual machine (VM) directly—without capturing the host's Virtual Machine Manager window or the guest OS screen—can be achieved by leveraging QEMU's built-in capabilities. Here’s how you can do it:


Method 1: Using QEMU's VNC and ffmpeg

This method involves redirecting the VM's display to a VNC server and then recording the VNC output using ffmpeg.

Steps:

  1. Start the VM with VNC Display When starting your VM, use the -vnc option to expose the display over VNC. For example:

    bash
    qemu-system-x86_64 -vnc :1 -display none ...
    • :1 means the VNC server will listen on port 5901 (default VNC port is 5900 + display number).
    • -display none disables the default graphical window.
  2. Connect to the VNC Server Use a VNC client (like vinagre, tigervnc-viewer, or RealVNC) to connect to localhost:5901 and verify the display is working.

  3. Record the VNC Display with ffmpeg Use ffmpeg to capture the VNC display:

    bash
    ffmpeg -f vnc -i localhost:5901 -r 30 -c:v libx264 -preset ultrafast -crf 0 output.mp4
    • -r 30: Sets the frame rate to 30 FPS.
    • -c:v libx264: Uses the H.264 codec for recording.
    • -preset ultrafast: Optimizes for speed (lower quality, but faster encoding).
    • -crf 0: Lossless recording (adjust for quality vs. file size).
    • output.mp4: Output file name.
  4. Stop Recording Press q in the ffmpeg terminal to stop recording.


Method 2: Using QEMU's SPICE and spice-vdagent

If your VM uses SPICE for display, you can record the SPICE stream.

Steps:

  1. Start the VM with SPICE Use the -spice option to enable SPICE:

    bash
    qemu-system-x86_64 -spice port=5900,disable-ticketing -vga qxl -device virtio-serial-pci -device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0 -chardev spicevmc,id=spicechannel0,name=vdagent ...
  2. Connect to SPICE Use a SPICE client (like virt-viewer or Remote Viewer) to connect to localhost:5900.

  3. Record SPICE with ffmpeg Use ffmpeg to capture the SPICE display:

    bash
    ffmpeg -f spice -i localhost:5900 -r 30 -c:v libx264 -preset ultrafast -crf 0 output.mp4

Method 3: Using QEMU's -display sdl and ffmpeg

If you want to avoid VNC/SPICE, you can use SDL (Simple DirectMedia Layer) for display and pipe it to ffmpeg.

Steps:

  1. Start the VM with SDL Display

    bash
    qemu-system-x86_64 -display sdl ...
  2. Record SDL Window with ffmpeg Use ffmpeg to capture the SDL window by its title:

    bash
    ffmpeg -f x11grab -i :0.0+100,200 -r 30 -c:v libx264 -preset ultrafast -crf 0 output.mp4
    • :0.0+100,200: X11 display and offset (adjust 100,200 to match the SDL window position).
    • This method is less reliable for headless recording.

Method 4: Using QEMU's -display none and Framebuffer

If you want to record the raw framebuffer, you can dump it to a file and encode it.

Steps:

  1. Start the VM with Framebuffer Dump

    bash
    qemu-system-x86_64 -display none -serial stdio -device virtio-gpu-pci,xres=1920,yres=1080 -vga none ...
  2. Use ffmpeg to Record Framebuffer This method is complex and requires custom scripting to dump the framebuffer to a file and encode it.


Recommended Method

Use Method 1 (VNC + ffmpeg) for simplicity and reliability. It works well for headless recording and avoids capturing the host window.


Notes:

  • Ensure ffmpeg is installed (sudo apt install ffmpeg on Debian/Ubuntu).
  • Adjust -r, -crf, and -preset in ffmpeg for performance/quality trade-offs.
  • For high-performance recording, consider using -preset superfast or -preset veryfast instead of ultrafast.
Jul 21, 10:41am

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.

Vibe can make mistakes. Check answers. Learn more