Skip to content

Video stream settings#

In LUNA ID, you can configure the following parameters for video stream recording:

Setting Platform
Video stream quality
Timeout before starting recording
Video stream duration
Custom frame resolution
Autofocus
Compression

Video stream quality#

Applies to LUNA ID for Android only.

To configure the video stream quality, pass the LunaVideoQuality parameter to the LunaConfig method. The parameter has the following values:

  • SD - Default. Provides a lower resolution and smaller file size suitable for most use cases (~640x480 pixels).
  • HD - Increases the resolution, frame rate, and bitrate, resulting in better video quality but larger file sizes and potentially higher processing requirements.

Video stream quality is determined by the following parameters:

Parameter SD (Low quality) SD (High quality) HD 720p HD 1080p
Video resolution 640x480 px 720×480 px 1280×720 px 1920×1080 px
Video frame rate 20 fps 30 fps 30 fps 30 fps
Video bitrate 384 Kbps 2 Mbps 4 Mbps 20 Mbps

Timeout before starting recording#

Applies to LUNA ID for iOS only.

To configure a delay before starting video recording, use the LCLunaConfiguration.startDelay parameter. This parameter allows you to specify the duration (in seconds) to wait before initiating the recording process.

By default, the parameter value is set to 0.

Video stream duration#

In LUNA ID for Android#

To limit a video stream's duration, use the recordingTimeMillis parameter within the LunaID.ShowCameraParams configuration. This parameter defines the video stream duration in milliseconds. By default, this value is not set , meaning you must explicitly configure it when enabling video recording.

LunaID.showCamera(
    activity,
    LunaID.ShowCameraParams(
        recordVideo = true,
        recordingTimeMillis = 10000 // Sets the video recording duration to 10 seconds
    )
)

Important: The recordingTimeMillis parameter is mandatory if recordVideo is set to true. Failing to provide a valid positive value will result in the following exception:

IllegalStateException, when param recordVideo is true -> param recordingTimeMillis must be positive

In LUNA ID for iOS#

To limit the duration of a video stream:

  1. Enable face identity tracking
    Set the LCLunaConfiguration.trackFaceIdentity property to true to enable face identity tracking during the video stream.
  2. Set video stream length
    Use the LCLunaConfiguration::videoRecordLength parameter to specify the maximum duration of the video stream in seconds.
  3. Initialize the watchdog object
    Call LMCameraCaptureManager::createVideoRecordWatchDog(LunaCore::LCBestShotDetectorProtocol) in your ViewController.
    This initializes a watchdog object that monitors the primary face search and starts the video recording process. Once the time specified in videoRecordLength elapses, the recording automatically stops.
    The watchdog object lives inside the capture manager and is not available for public usage.

Custom frame resolution#

Applies to LUNA ID for Android only.

To specify precise resolution requirements for your application, use the following parameters of the ShowCameraParams class:

  • preferredAnalysisFrameWidth
  • preferredAnalysisFrameHeight

These parameters allow you to specify a preferred resolution for frame analysis. However, note that the preferred prefix implies the specified resolution may not always be supported by the device's camera. In such cases, the system automatically adjusts to the nearest available resolution.

By configuring these parameters, you can optimize the frame resolution to better suit your application's needs while ensuring compatibility with the device's hardware capabilities.

The default frame resolution for frame analysis is 480x320.

Autofocus#

Applies to LUNA ID for Android only.

To control whether the camera's autofocus feature will be enabled or disabled upon startup, use the disableAutoFocus parameter of the ShowCameraParams class. The parameter has the following values:

  • true - Disables the camera's autofocus functionality, allowing for a fixed focus setting regardless of device capabilities.
  • false - Default. Enabled the camera’s autofocus feature if the device supports it. This aligns with the default behavior of CameraX, which enables autofocus when supported by the hardware.

Compression#

Applies to LUNA ID for Android only.

To compress a video, you need to integrate FFmpegKit into your Android project:

1․ Add the JitPack repository

In your settings.gradle.kts file, include the JitPack repository as follows:

pluginManagement {
    repositories {
        google()
        gradlePluginPortal()
        mavenCentral()
        maven("https://jitpack.io ")
    }
}

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven("https://jitpack.io ")
    }
}

2․ Add the FFmpegKit Dependency

In your module's build.gradle.kts file (for example, app/build.gradle.kts), add the following dependency under dependencies:

dependencies {
    implementation("com.github.arthenica:ffmpeg-kit-min-gpl:6.0-2.LTS") // Minimal GPL version
    // For the full version, use:
    // implementation("com.github.arthenica:ffmpeg-kit-full-gpl:6.0-2.LTS")
}

3․ Sync your project

After adding the dependencies, sync your project with Gradle files.

In Android Studio, go to File > Sync Project with Gradle Files.

4․ Request permissions (if needed)

Add the necessary permissions to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Note: If targeting Android 10 (API level 29) or higher, consider using the Storage Access Framework (SAF) instead of requesting direct storage permissions.

5․ Add the FFmpegUtils utility class

Create a utility class named FFmpegUtils to handle FFmpeg operations. Here's an example implementation:

package ru.visionlabs.sdk.lunacore.utils

import com.arthenica.ffmpegkit.FFmpegKit
import com.arthenica.ffmpegkit.ReturnCode
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

object FFmpegUtils {

    /**
     * Compresses a video file using FFmpeg.
     *
     * @param inputPath The path to the input video file.
     * @param outputPath The path where the compressed video will be saved.
     * @param onSuccess Callback invoked on successful compression.
     * @param onFailure Callback invoked if an error occurs.
     */
    fun compressWithFFmpeg(
        inputPath: String,
        outputPath: String,
        onSuccess: () -> Unit,
        onFailure: (Throwable) -> Unit
    ) {
        val cmd = listOf(
            "-y", "-i", inputPath,
            "-vf", "scale=iw/2:ih/2", // Scale video resolution by half
            "-c:v", "libx264", "-b:v", "1M", "-preset", "fast", // Video codec settings
            "-c:a", "aac", "-b:a", "128k", // Audio codec settings
            outputPath
        )

        CoroutineScope(Dispatchers.IO).launch {
            try {
                val session = FFmpegKit.execute(cmd.joinToString(" "))
                if (ReturnCode.isSuccess(session.returnCode)) {
                    onSuccess()
                } else {
                    onFailure(
                        RuntimeException("FFmpeg failed: ${session.returnCode}\n${session.failStackTrace}")
                    )
                }
            } catch (e: Exception) {
                onFailure(e)
            }
        }
    }
}

6․ Use the utility in your activity or fragment

To compress a video, use the FFmpegUtils.compressWithFFmpeg method as shown below:

val input = "/sdcard/DCIM/input.mp4" // Path to the input video file
val output = cacheDir.resolve("compressed.mp4").absolutePath // Path to save the compressed video

FFmpegUtils.compressWithFFmpeg(
    inputPath = input,
    outputPath = output,
    onSuccess = {
        // Handle success (e.g., show a Toast or notify the user)
        println("Compression successful!")
    },
    onFailure = { err ->
        // Handle failure (e.g., log the error or show a message)
        println("Compression failed: ${err.message}")
    }
)