Skip to content

Instantly share code, notes, and snippets.

@1999AZZAR
Last active November 18, 2024 21:53
Show Gist options
  • Save 1999AZZAR/2d075b3acf97886f78f0f83759ac46db to your computer and use it in GitHub Desktop.
Save 1999AZZAR/2d075b3acf97886f78f0f83759ac46db to your computer and use it in GitHub Desktop.
This Bash script creates a virtual display on an Android device and streams it using scrcpy. It's designed to work with Android Debug Bridge (ADB) and scrcpy, providing a seamless way to create and interact with a secondary display on your Android device.

Virtual Display for Android Devices

Overview

This Bash script creates a virtual display on an Android device and streams it using scrcpy. It's designed to work with Android Debug Bridge (ADB) and scrcpy, providing a seamless way to create and interact with a secondary display on your Android device.

Prerequisites

  • Android Debug Bridge (ADB) installed and accessible from the command line
  • scrcpy installed and accessible from the command line
  • An Android device connected via USB with USB debugging enabled

Features

  • Creates a virtual 4K (3840x2160) display on the connected Android device
  • Automatically detects the new display ID
  • Streams the virtual display using scrcpy with optimized settings
  • Cleans up the virtual display on script termination

Usage

  1. Ensure your Android device is connected and USB debugging is enabled.

  2. Save the script below this file.

  3. Add the executable permission to the script:

     chmod +x virtual_display.sh
    
  4. Run the script:

    ./virtual_display.sh
    
  5. The script will create a virtual display and launch scrcpy to stream it.

  6. To stop the script and clean up, simply close the scrcpy window or use Ctrl+C in the terminal.

Script Workflow

  1. Checks for the presence of adb and a connected Android device.
  2. Creates a virtual 4K display on the Android device.
  3. Detects the ID of the newly created display.
  4. Launches scrcpy to stream the virtual display with the following settings:
    • 8 Mbps bitrate
    • Window title set to 'Pixel'
    • Maximum 55 FPS
    • Audio disabled
  5. Automatically removes the virtual display when the script is terminated.

Error Handling

The script includes error checking for:

  • Missing adb or scrcpy installations
  • No connected Android device
  • Failure to detect the new display ID

Cleanup

The script uses a trap to ensure the virtual display is removed when the script exits, even if terminated unexpectedly.

Customization

You can modify the following parameters in the script:

  • Virtual display resolution (default: 3840x2160)
  • scrcpy settings (bitrate, FPS, window title, etc.)

Troubleshooting

If you encounter issues:

  1. Ensure ADB and scrcpy are correctly installed and in your PATH.
  2. Check that your Android device is properly connected and USB debugging is enabled.
  3. If the virtual display isn't detected, try increasing the sleep duration after creating the display.

Contributing

Feel free to fork this project and submit pull requests with improvements or additional features.

Disclaimer

This script interacts with your Android device settings. Use at your own risk. Always ensure you have a backup of important data before modifying device settings.

#!/bin/bash
# Function to clean up the virtual display when the script is terminated
cleanup() {
echo "Cleaning up..."
echo "Removing the virtual display..."
adb shell settings put global overlay_display_devices none
echo "Virtual display removed."
exit 0
}
# Trap to call cleanup function when the script is closed
trap cleanup EXIT
# Check if adb is available
if ! command -v adb &> /dev/null; then
echo "Error: adb is not installed or not in PATH. Please install Android Debug Bridge."
exit 1
fi
# Check if device is connected
if ! adb devices | grep -q device$; then
echo "Error: No Android device connected. Please connect a device and try again."
exit 1
fi
# Function to get the list of display IDs
get_display_ids() {
adb shell dumpsys display | grep "mDisplayId=" | awk '{print $1}' | cut -d= -f2 | sort -u
}
# Get initial display IDs
initial_displays=$(get_display_ids)
# Create a virtual display on the Android device
echo "Creating a virtual display..."
adb shell settings put global overlay_display_devices 1920x1080/240
# Wait for the new display to be recognized
sleep 2
# Get new display IDs
new_displays=$(get_display_ids)
# Find the new display ID
secondary_display_id=$(comm -13 <(echo "$initial_displays") <(echo "$new_displays") | tr -d '[:space:]')
if [ -z "$secondary_display_id" ]; then
echo "Failed to detect the new display ID."
cleanup
exit 1
else
echo "Detected secondary display ID: $secondary_display_id"
fi
# Check if scrcpy is available
if ! command -v scrcpy &> /dev/null; then
echo "Error: scrcpy is not installed or not in PATH. Please install scrcpy."
cleanup
exit 1
fi
# Start scrcpy with the provided options and detected display ID
echo "Starting scrcpy on the virtual display..."
scrcpy -b 24M --window-title='Pixel' --max-fps=60 --no-audio --display-id "$secondary_display_id"
# The cleanup function will be called automatically when the script exits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment