Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ interface MediaResult {
height: number; // output height in pixels
duration: number; // duration in ms (0 for images)
mime: string; // MIME type, e.g. 'video/mp4'
encoder?: string; // Android video exports: '<default|software_encoder>:<codec>', see below
}

interface MediaMetadata {
Expand Down Expand Up @@ -607,6 +608,10 @@ Standard image processing operations can cause Out-Of-Memory (OOM) exceptions wh
- **Android:** Utilizes `BitmapFactory.Options.inSampleSize` to subsample the image during the hardware decoding phase, bypassing full-resolution memory allocation entirely.
- **iOS:** Uses `CGImageSourceCreateThumbnailAtIndex` to instruct `ImageIO` to decode and downscale directly from the file descriptor buffer.

### Hardware encoder fallback (Android)

Every video export tries Media3's default (hardware) encoder first. If the video encoder fails, at initialisation or mid-export (some devices reject a clip on the first frame, e.g. `OMX.qcom.video.encoder.avc` on a Samsung SM-A526B with `OMX_ErrorHardware`), the export runs once more with a software encoder before failing. `MediaResult.encoder` reports which one produced the file; a failure's message lists each attempt with Media3's error code and the `MediaCodec.CodecException` details, and logcat tag `VideoProcessor` carries the same.

### Smart Video Compression

The `compressVideo` API provides a dynamically balanced encoding strategy via the `targetSizeInMB` flag. When provided, the library will:
Expand Down
6 changes: 4 additions & 2 deletions android/src/main/java/com/mediatoolkit/HybridMediaToolkit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ class HybridMediaToolkit : HybridMediaToolkitSpec() {
width = result.width,
height = result.height,
duration = result.duration,
mime = "audio/m4a"
mime = "audio/m4a",
encoder = result.encoder
)
}
}
Expand Down Expand Up @@ -444,6 +445,7 @@ private fun Map<String, Any>.toMediaResult(): MediaResult {
width = (this["width"] as? Number)?.toDouble() ?: 0.0,
height = (this["height"] as? Number)?.toDouble() ?: 0.0,
duration = (this["duration"]as? Number)?.toDouble() ?: 0.0,
mime = this["mime"] as? String ?: ""
mime = this["mime"] as? String ?: "",
encoder = this["encoder"] as? String
)
}
129 changes: 102 additions & 27 deletions android/src/main/java/com/mediatoolkit/VideoProcessor.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
package com.margelo.nitro.com.mediatoolkit

import android.content.Context
import android.media.MediaCodec
import android.net.Uri
import android.util.Log
import androidx.media3.common.MediaItem
import androidx.media3.common.MimeTypes
import androidx.media3.transformer.Composition
import androidx.media3.transformer.DefaultEncoderFactory
import androidx.media3.transformer.EditedMediaItem
import androidx.media3.transformer.EditedMediaItemSequence
import androidx.media3.transformer.Effects
import androidx.media3.transformer.EncoderSelector
import androidx.media3.transformer.EncoderUtil
import androidx.media3.transformer.ExportException
import androidx.media3.transformer.ExportResult
import androidx.media3.transformer.ProgressHolder
import androidx.media3.transformer.Transformer
import androidx.media3.transformer.VideoEncoderSettings
import androidx.media3.effect.Presentation
import androidx.media3.effect.ScaleAndRotateTransformation
import androidx.media3.effect.SpeedChangeEffect
import androidx.media3.common.audio.SonicAudioProcessor
import androidx.media3.common.util.UnstableApi
import com.google.common.collect.ImmutableList
import java.io.File
import java.util.UUID
import java.util.concurrent.CountDownLatch
Expand All @@ -27,6 +34,7 @@ import java.util.concurrent.CountDownLatch
*/
@UnstableApi
internal object VideoProcessor {
private const val TAG = "VideoProcessor"
private const val MAX_GIF_TOTAL_PIXELS = 40_000_000L

// ─── TRIM ────────────────────────────────────────────────────────────────
Expand All @@ -52,7 +60,7 @@ internal object VideoProcessor {
.setClippingConfiguration(clippingConfig)
.build()

return runTransform(context, mediaItem, effects = Effects.EMPTY, out, onProgress, transmux = true)
return runTransform(context, mediaItem, effects = Effects.EMPTY, out, onProgress)
}

// ─── CROP ────────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -120,7 +128,7 @@ internal object VideoProcessor {
val effects = Effects(emptyList(), listOf(cropEffect))

// Must re-encode for crop, but only ONE pass (faster than trim then crop)
return runTransform(context, mediaItem, effects, out, onProgress, transmux = false)
return runTransform(context, mediaItem, effects, out, onProgress)
}

// ─── PROCESS (Trim + Crop + Flip + Rotate) ───────────────────────────────
Expand Down Expand Up @@ -186,11 +194,11 @@ internal object VideoProcessor {

val effects = Effects(emptyList(), effectList)

// processVideo always re-encodes (transmux = false). Without an explicit bitrate,
// processVideo always re-encodes. Without an explicit bitrate,
// Media3 Transformer picks a very low default, causing severe blur.
// 4 Mbps balances quality vs file size — locket clips are short (2-5s) so this
// keeps video sharp enough while not inflating size before compressVideo.
return runTransform(context, mediaItem, effects, out, onProgress, transmux = false, targetBitrate = 4_000_000)
return runTransform(context, mediaItem, effects, out, onProgress, targetBitrate = 4_000_000)
}

// ─── ROTATE ──────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -720,23 +728,69 @@ internal object VideoProcessor {

// ─── Core ────────────────────────────────────────────────────────────────

// A device whose hardware encoder rejects the export (Samsung SM-A526B, OMX.qcom.video.encoder.avc:
// OMX_ErrorHardware on the first surface frame for some clips, regardless of output size,
// orientation, colour tags or performance hints) reports the fallback through the result's
// `encoder` field and the VideoProcessor logcat lines.
private enum class EncoderAttempt {
DEFAULT,
// Media3's default selector only offers hardware encoders when the device has any.
SOFTWARE_ENCODER,
}

private fun runTransform(
context: Context,
mediaItem: MediaItem,
effects: Effects,
outputPath: String,
onProgress: (Int) -> Unit,
transmux: Boolean = false,
targetBitrate: Int = 0, // 0 = let Media3 decide
removeAudio: Boolean = false, // true = strip audio track
removeVideo: Boolean = false // true = strip video track
): Map<String, Any> {
val outFile = File(outputPath)
outFile.parentFile?.mkdirs()

val failures = mutableListOf<String>()
for (attempt in EncoderAttempt.values()) {
if (outFile.exists()) outFile.delete()
try {
val encoderName = export(context, mediaItem, effects, outputPath, onProgress, targetBitrate, removeAudio, removeVideo, attempt)
val encoder = "${attempt.name.lowercase()}:${encoderName ?: "none"}"
if (failures.isEmpty()) {
Log.i(TAG, "Export used $encoder")
} else {
Log.w(TAG, "Export used $encoder after: ${failures.joinToString(" || ")}")
}
return buildResult(outputPath, 0) + ("encoder" to encoder)
} catch (e: ExportException) {
failures += "${attempt.name.lowercase()}: ${describe(e)}"
if (!isVideoEncoderFailure(e)) break
Log.w(TAG, "Video encoder failed, trying the next configuration: ${failures.last()}")
} catch (e: Exception) {
failures += "${attempt.name.lowercase()}: ${e.javaClass.simpleName}: ${e.message}"
break
}
}
if (outFile.exists()) outFile.delete()
throw MediaToolkitException.ProcessingFailed("Transform failed: ${failures.joinToString(" || ")}")
}

/** Runs one export and returns the name of the video encoder Media3 used, if any. */
private fun export(
context: Context,
mediaItem: MediaItem,
effects: Effects,
outputPath: String,
onProgress: (Int) -> Unit,
targetBitrate: Int,
removeAudio: Boolean,
removeVideo: Boolean,
attempt: EncoderAttempt
): String? {
val latch = CountDownLatch(1)
var exportError: Exception? = null
var videoEncoderName: String? = null

val editedItemBuilder = EditedMediaItem.Builder(mediaItem)
.setEffects(effects)
Expand All @@ -751,33 +805,17 @@ internal object VideoProcessor {
val transformerBuilder = Transformer.Builder(context)
.addListener(object : Transformer.Listener {
override fun onCompleted(composition: Composition, result: ExportResult) {
videoEncoderName = result.videoEncoderName
latch.countDown()
}
override fun onError(composition: Composition, result: ExportResult, exception: ExportException) {
exportError = exception
latch.countDown()
}
})

if (transmux) {
// Passthrough: remux without re-encoding — fast trim
transformerBuilder.setVideoMimeType(MimeTypes.VIDEO_H264)
} else {
if (targetBitrate > 0) {
val videoSettings = androidx.media3.transformer.VideoEncoderSettings.Builder()
.setBitrate(targetBitrate)
.build()
val encoderFactory = androidx.media3.transformer.DefaultEncoderFactory.Builder(context)
.setRequestedVideoEncoderSettings(videoSettings)
.build()
transformerBuilder.setEncoderFactory(encoderFactory)
// Use H.264: more mature encoders on Android, better bitrate compliance than HEVC.
transformerBuilder.setVideoMimeType(MimeTypes.VIDEO_H264)
} else {
transformerBuilder.setVideoMimeType(MimeTypes.VIDEO_H264)
}
}

// Use H.264: more mature encoders on Android, better bitrate compliance than HEVC.
.setVideoMimeType(MimeTypes.VIDEO_H264)
.setEncoderFactory(buildEncoderFactory(context, targetBitrate, attempt))
val transformer = transformerBuilder.build()

// Run on main thread (Media3 requirement), then wait
Expand Down Expand Up @@ -810,9 +848,46 @@ internal object VideoProcessor {

latch.await()

exportError?.let { throw MediaToolkitException.ProcessingFailed("Transform failed: ${it.message}") }
exportError?.let { throw it }
return videoEncoderName
}

private fun buildEncoderFactory(context: Context, targetBitrate: Int, attempt: EncoderAttempt): DefaultEncoderFactory {
val settings = VideoEncoderSettings.Builder()
if (targetBitrate > 0) {
settings.setBitrate(targetBitrate)
}
val builder = DefaultEncoderFactory.Builder(context)
.setRequestedVideoEncoderSettings(settings.build())
if (attempt == EncoderAttempt.SOFTWARE_ENCODER) {
builder.setVideoEncoderSelector(softwareVideoEncoderSelector)
}
return builder.build()
}

private val softwareVideoEncoderSelector = EncoderSelector { mimeType ->
ImmutableList.copyOf(
EncoderUtil.getSupportedEncoders(mimeType).filterNot { EncoderUtil.isHardwareAccelerated(it, mimeType) }
)
}

// Covers init failures and errors MediaCodec reports asynchronously once running
// (ERROR_CODE_ENCODING_FAILED), e.g. OMX_ErrorHardware on the first input frame.
private fun isVideoEncoderFailure(e: ExportException): Boolean {
val codec = e.codecInfo ?: return false
return codec.isVideo && !codec.isDecoder
}

return buildResult(outputPath, 0)
private fun describe(e: ExportException): String {
val cause = e.cause
val causeDetails = when (cause) {
null -> null
is MediaCodec.CodecException ->
"CodecException(errorCode=${cause.errorCode}, transient=${cause.isTransient}, " +
"recoverable=${cause.isRecoverable}, diagnostic=${cause.diagnosticInfo}): ${cause.message}"
else -> "${cause.javaClass.simpleName}: ${cause.message}"
}
return listOfNotNull(ExportException.getErrorCodeName(e.errorCode), e.message, causeDetails).joinToString(" | ")
}


Expand Down
3 changes: 2 additions & 1 deletion ios/HybridMediaToolkit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,8 @@ private func makeMediaResult(_ raw: [String: Any]) -> MediaResult {
width: Double(raw["width"] as? Int ?? 0),
height: Double(raw["height"] as? Int ?? 0),
duration: Double(raw["duration"] as? Int ?? 0),
mime: raw["mime"] as? String ?? ""
mime: raw["mime"] as? String ?? "",
encoder: nil
)
}

Expand Down
2 changes: 1 addition & 1 deletion nitrogen/generated/android/c++/JHybridMediaToolkitSpec.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions nitrogen/generated/android/c++/JMediaResult.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading