../mirror-android

Mirroring your Android display to Mac or Linux

(This is an old post originally posted on our company Medium blog, which is now gone. I transplanted it here for posterity.)

For a presentation, I needed to show the audience what was going on on my Android phone. I expected this to be easy, but I ended up spending more than enough time figuring it out.

My first idea was to connect my phone to the projector using a USB-C to HDMI adapter. After testing with two different adapters, trawling the phone settings, and finally finding that this (now deleted) Wikipedia Article did not list my phone, I realized that direct video output was not an option.

My next attempt was to find an app that would let me accomplish my goal. Several alternatives exist, such as Vysor, AirDroid and Droid@Screen among others. Vysor is capped at a low bandwidth unless you pay for the premium features, resulting in a low-resolution image. It also required installing an app on both my phone and computer. AirDroid was cumbersome to use, required me to register an account and also delivered sub-par resolution. Droid@Screen I could not even get to work on my Mac. Back to the googling board, then.

Hidden in an answer on the Android Stackexchange, with ten measly upvotes, I finally found my solution. It came in the most pleasing of forms, a simple combination of command line tools that can be run on your Mac or Linux laptop. It relies on Android Debug Bridge (adb), a tool that lets you interact with your phone using the terminal.

adb shell screenrecord --output-format=h264 — | ffplay -framerate 60 -probesize 32 -sync video -\

It’s working!

This solved my problem in a simple and developer-friendly way. It also did not force me to install any additional software, as I had installed both Android Debug Bridge and ffmpeg previously. Awesome!

If adb is not installed, it can be done with brew cask install android-platform-tools on Mac or with your package manager of choice on Linux.

So what does this command do? Let’s break it down a bit:

adb shell screenrecord starts the screenrecord application on the phone. Screenrecord has been available on Android since 4.4, and does, as the name implies, record whatever is on the screen. --output-format=h264 spcecifies the output format, and the final hyphen sets the output of screenrecord to stdout.

| ffplay pipes the output from screenrecord to ffplay on the computer. ffplay is a simple media player that comes with ffmpeg. -framerate 60 -probesize 32 specifies the frame rate and probesize. 32 is the lowest setting for probe size, and minimizes the delay in the playback.-sync video drops frames instead of fast-forwarding them, and the last hyphen sets the video input to stdin.

This solution does come with some caveats: