Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Simplify Android video core naming

Neeraj Guptad4835b2812c5
diff --git a/mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/NativeVideoEditorPlugin.kt b/mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/NativeVideoEditorPlugin.kt
index 28bb784712..02d2855358 100644
--- a/mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/NativeVideoEditorPlugin.kt
+++ b/mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/NativeVideoEditorPlugin.kt
@@ -4,9 +4,9 @@ import android.util.Log
import androidx.annotation.NonNull
import androidx.media3.common.util.UnstableApi
import androidx.media3.transformer.ExportException
-import io.ente.native_video_editor.core.AndroidVideoExportEngine
-import io.ente.native_video_editor.core.AndroidVideoFrameExtractor
-import io.ente.native_video_editor.core.AndroidVideoMetadataReader
+import io.ente.native_video_editor.core.VideoExportEngine
+import io.ente.native_video_editor.core.VideoFrameExtractor
+import io.ente.native_video_editor.core.VideoMetadataReader
import io.ente.native_video_editor.core.VideoCrop
import io.ente.native_video_editor.core.VideoEditRequest
import io.ente.native_video_editor.core.VideoEditResult
@@ -42,9 +42,9 @@ class NativeVideoEditorPlugin : FlutterPlugin, MethodCallHandler, EventChannel.S
private var scope = newPluginScope()
private var currentExportJob: Job? = null
private val frameJobs = mutableMapOf<String, Job>()
- private lateinit var metadataReader: AndroidVideoMetadataReader
- private lateinit var frameExtractor: AndroidVideoFrameExtractor
- private lateinit var videoExporter: AndroidVideoExportEngine
+ private lateinit var metadataReader: VideoMetadataReader
+ private lateinit var frameExtractor: VideoFrameExtractor
+ private lateinit var videoExporter: VideoExportEngine
override fun onAttachedToEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
if (!scope.isActive) scope = newPluginScope()
@@ -54,9 +54,9 @@ class NativeVideoEditorPlugin : FlutterPlugin, MethodCallHandler, EventChannel.S
progressChannel = EventChannel(binding.binaryMessenger, PROGRESS_CHANNEL)
progressChannel.setStreamHandler(this)
- metadataReader = AndroidVideoMetadataReader()
- frameExtractor = AndroidVideoFrameExtractor(binding.applicationContext, metadataReader)
- videoExporter = AndroidVideoExportEngine(binding.applicationContext, metadataReader)
+ metadataReader = VideoMetadataReader()
+ frameExtractor = VideoFrameExtractor(binding.applicationContext, metadataReader)
+ videoExporter = VideoExportEngine(binding.applicationContext, metadataReader)
}
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
diff --git a/mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/core/AndroidVideoMetadataReader.kt b/mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/core/AndroidVideoMetadataReader.kt
deleted file mode 100644
index 615fdd4aa2..0000000000
--- a/mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/core/AndroidVideoMetadataReader.kt
+++ /dev/null
@@ -1,90 +0,0 @@
-package io.ente.native_video_editor.core
-
-import android.media.MediaMetadataRetriever
-import android.os.Build
-import android.util.Log
-
-class AndroidVideoMetadataReader {
- fun read(videoPath: String): VideoMetadata {
- val retriever = MediaMetadataRetriever()
- return try {
- retriever.setDataSource(videoPath)
- read(retriever, videoPath)
- } finally {
- retriever.release()
- }
- }
-
- fun read(retriever: MediaMetadataRetriever, videoPath: String): VideoMetadata {
- val durationMs = metadataLong(
- retriever,
- MediaMetadataRetriever.METADATA_KEY_DURATION,
- "duration",
- videoPath
- ) ?: 0L
- val width = metadataInt(
- retriever,
- MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH,
- "width",
- videoPath
- ) ?: 0
- val height = metadataInt(
- retriever,
- MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT,
- "height",
- videoPath
- ) ?: 0
- val rotation = metadataInt(
- retriever,
- MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION,
- "rotation",
- videoPath
- ) ?: 0
- val bitrate = retriever.extractMetadata(
- MediaMetadataRetriever.METADATA_KEY_BITRATE
- )?.toLongOrNull()?.takeIf { it > 0 }
- val frameRate = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- retriever.extractMetadata(
- MediaMetadataRetriever.METADATA_KEY_CAPTURE_FRAMERATE
- )?.toFloatOrNull()?.takeIf { it > 0 }
- } else {
- null
- }
-
- val normalizedRotation = ((rotation % 360) + 360) % 360
- require(width > 0 && height > 0 && normalizedRotation in setOf(0, 90, 180, 270)) {
- "Invalid video dimensions or rotation"
- }
-
- return VideoMetadata(
- durationMs = durationMs,
- width = width,
- height = height,
- rotationDegrees = normalizedRotation,
- bitrate = bitrate,
- frameRate = frameRate
- )
- }
-
- private fun metadataInt(
- retriever: MediaMetadataRetriever,
- key: Int,
- name: String,
- path: String
- ): Int? = retriever.extractMetadata(key)?.toIntOrNull().also {
- if (it == null) Log.w(TAG, "Failed to extract $name metadata from video: $path")
- }
-
- private fun metadataLong(
- retriever: MediaMetadataRetriever,
- key: Int,
- name: String,
- path: String
- ): Long? = retriever.extractMetadata(key)?.toLongOrNull().also {
- if (it == null) Log.w(TAG, "Failed to extract $name metadata from video: $path")
- }
-
- private companion object {
- const val TAG = "VideoMetadataReader"
- }
-}
diff --git a/mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/core/AndroidVideoExportEngine.kt b/mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/core/VideoExportEngine.kt
similarity index 99%
rename from mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/core/AndroidVideoExportEngine.kt
rename to mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/core/VideoExportEngine.kt
index 06229d83bb..7be16233ec 100644
--- a/mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/core/AndroidVideoExportEngine.kt
+++ b/mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/core/VideoExportEngine.kt
@@ -35,9 +35,9 @@ import kotlinx.coroutines.withContext
import java.io.File
@UnstableApi
-class AndroidVideoExportEngine(
+class VideoExportEngine(
private val context: Context,
- private val metadataReader: AndroidVideoMetadataReader
+ private val metadataReader: VideoMetadataReader
) {
suspend fun processVideo(
request: VideoEditRequest,
diff --git a/mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/core/AndroidVideoFrameExtractor.kt b/mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/core/VideoFrameExtractor.kt
similarity index 97%
rename from mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/core/AndroidVideoFrameExtractor.kt
rename to mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/core/VideoFrameExtractor.kt
index 65064a3362..e4eeff3d45 100644
--- a/mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/core/AndroidVideoFrameExtractor.kt
+++ b/mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/core/VideoFrameExtractor.kt
@@ -26,9 +26,9 @@ import java.util.concurrent.ExecutionException
import kotlin.coroutines.coroutineContext
@UnstableApi
-class AndroidVideoFrameExtractor(
+class VideoFrameExtractor(
private val context: Context,
- private val metadataReader: AndroidVideoMetadataReader
+ private val metadataReader: VideoMetadataReader
) {
private val permits = Semaphore(2)
@@ -61,7 +61,7 @@ class AndroidVideoFrameExtractor(
var fallbackExtractor: FrameExtractor? = null
try {
retriever.setDataSource(request.inputPath)
- val videoInfo = metadataReader.read(retriever, request.inputPath)
+ val videoInfo = metadataReader.read(retriever)
val targetSize = boundedVideoSize(
videoInfo.displayWidth,
videoInfo.displayHeight,
diff --git a/mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/core/VideoMetadataReader.kt b/mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/core/VideoMetadataReader.kt
new file mode 100644
index 0000000000..332b9e19f2
--- /dev/null
+++ b/mobile/packages/native_video_editor/android/src/main/kotlin/io/ente/native_video_editor/core/VideoMetadataReader.kt
@@ -0,0 +1,53 @@
+package io.ente.native_video_editor.core
+
+import android.media.MediaMetadataRetriever
+import android.os.Build
+
+class VideoMetadataReader {
+ fun read(videoPath: String): VideoMetadata {
+ val retriever = MediaMetadataRetriever()
+ return try {
+ retriever.setDataSource(videoPath)
+ read(retriever)
+ } finally {
+ retriever.release()
+ }
+ }
+
+ fun read(retriever: MediaMetadataRetriever): VideoMetadata {
+ val durationMs = retriever.longValue(MediaMetadataRetriever.METADATA_KEY_DURATION) ?: 0L
+ val width = retriever.intValue(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH) ?: 0
+ val height = retriever.intValue(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT) ?: 0
+ val rotation = retriever.intValue(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION) ?: 0
+ val bitrate = retriever.extractMetadata(
+ MediaMetadataRetriever.METADATA_KEY_BITRATE
+ )?.toLongOrNull()?.takeIf { it > 0 }
+ val frameRate = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+ retriever.extractMetadata(
+ MediaMetadataRetriever.METADATA_KEY_CAPTURE_FRAMERATE
+ )?.toFloatOrNull()?.takeIf { it > 0 }
+ } else {
+ null
+ }
+
+ val normalizedRotation = ((rotation % 360) + 360) % 360
+ require(width > 0 && height > 0 && normalizedRotation in setOf(0, 90, 180, 270)) {
+ "Invalid video dimensions or rotation"
+ }
+
+ return VideoMetadata(
+ durationMs = durationMs,
+ width = width,
+ height = height,
+ rotationDegrees = normalizedRotation,
+ bitrate = bitrate,
+ frameRate = frameRate
+ )
+ }
+
+ private fun MediaMetadataRetriever.intValue(key: Int): Int? =
+ extractMetadata(key)?.toIntOrNull()
+
+ private fun MediaMetadataRetriever.longValue(key: Int): Long? =
+ extractMetadata(key)?.toLongOrNull()
+}