Record Android Screen Using FFmpeg Framebuffer

This article provides a quick guide on how to capture and record the screen activity of an Android device using the FFmpeg fbdev (framebuffer) input device. You will learn the necessary prerequisites, how to access the Android framebuffer, and the exact FFmpeg commands required to record your screen directly to a video file.

Prerequisites

To record the Android screen using the framebuffer device, you must meet the following requirements: * Root Access: The framebuffer device file (/dev/graphics/fb0) is protected. You must have root privileges on the Android device to read it. * FFmpeg Binary for Android: You need an FFmpeg binary compiled for your Android device’s architecture (usually ARM or ARM64). * ADB (Android Debug Bridge): Installed on your computer to interface with the device.


Step 1: Gain Root Access via ADB

Connect your Android device to your computer via USB, open your terminal, and access the device’s shell with root permissions:

adb shell
su

Step 2: Identify the Framebuffer and Screen Resolution

On Android, the framebuffer is typically located at /dev/graphics/fb0.

Before recording, you need to know your device’s screen resolution and pixel format. You can find the resolution by running:

wm size

Example output: Physical size: 1080x1920


Step 3: Run the FFmpeg Command

Use the fbdev demuxer to capture the screen. You must specify the input format as fbdev, the frame rate, the video size, and the input path /dev/graphics/fb0.

Basic Recording Command:

ffmpeg -f fbdev -framerate 25 -video_size 1080x1920 -i /dev/graphics/fb0 /sdcard/output.mp4

Advanced Command (Specifying Pixel Format):

If the colors in your output video are distorted or inverted, you may need to specify the correct pixel format (usually rgb565le, bgra, or rgba depending on your device’s hardware):

ffmpeg -f fbdev -framerate 30 -pixel_format bgra -video_size 1080x1920 -i /dev/graphics/fb0 -c:v libx264 -pix_fmt yuv420p /sdcard/output.mp4

Command Breakdown: * -f fbdev: Forces the input format to framebuffer device. * -framerate 30: Sets the capture frame rate to 30 frames per second. * -pixel_format bgra: Defines the pixel format of the source framebuffer. * -video_size 1080x1920: Matches the capture size to your device’s resolution. * -i /dev/graphics/fb0: Points to the input framebuffer device. * -c:v libx264: Encodes the output video using the H.264 codec. * -pix_fmt yuv420p: Ensures the output video is compatible with standard media players.


Step 4: Stop Recording and Retrieve the File

To stop the recording, press Ctrl + C in your terminal window.

Once the process finishes, transfer the recorded video file from your Android device to your computer using ADB:

adb pull /sdcard/output.mp4 .