How to Use android_camera Input Device in FFmpeg

This article explains how to use the android_camera input device in FFmpeg to capture video directly from an Android device's cameras. You will learn the prerequisites for using this feature, the basic command-line syntax, key configuration options such as resolution and camera selection, and practical examples for recording video.

Prerequisites

The android_camera input device is designed to run natively on Android. This means you must run FFmpeg inside an Android environment, such as Termux, or within a native Android application via the Android NDK.

To use this feature, your FFmpeg binary must be compiled with the following flags enabled:

  • --enable-avdevice
  • --enable-android-camera

Your Android application or terminal environment must also have permission to access the device's camera.

Basic Syntax

To capture video using the android_camera device, use -f android_camera to specify the input format, followed by the camera index as the input source:

ffmpeg -f android_camera -i <camera_index> output.mp4

On most Android devices:

  • 0 represents the primary rear-facing camera.
  • 1 represents the primary front-facing camera.

Key Configuration Options

You can customize the video stream by placing these options before the -i input flag:

  • -video_size: Sets the image resolution. Common values include 1280x720 (720p) or 1920x1080 (1080p). The camera must support the requested resolution.
  • -framerate: Sets the capturing frame rate (e.g., 30 or 60).
  • -input_queue_size: Sets the maximum number of frames to queue (useful for preventing dropped frames on slower hardware).

Practical Examples

1. Record 1080p Video from the Rear Camera

To record a high-definition video from the back camera at 30 frames per second:

ffmpeg -f android_camera -video_size 1920x1080 -framerate 30 -i 0 -c:v libx264 -preset ultrafast output.mp4

2. Record from the Front Camera

To record from the front-facing camera at 720p:

ffmpeg -f android_camera -video_size 1280x720 -i 1 output_front.mp4

3. Capture Video with Audio

The android_camera input device only captures video. To record audio simultaneously, you must combine it with an Android audio input device like opensles:

ffmpeg -f android_camera -video_size 1280x720 -i 0 -f opensles -i default -c:v libx264 -c:a aac output_with_audio.mp4