androidx / media

Jetpack Media3 support libraries for media use cases, including ExoPlayer, an extensible media player for Android
https://developer.android.com/media/media3
Apache License 2.0
1.6k stars 380 forks source link

ExportException: Unexpected runtime error when trimming and transforming video #1450

Closed PrinceVergil closed 3 months ago

PrinceVergil commented 3 months ago

Description: I am encountering an androidx.media3.transformer.ExportException: Unexpected runtime error when attempting to trim a video and then change its resolution and frame rate using the transformer library.

Steps to Reproduce:

  1. Create a MediaItem with clipping configuration to trim the video.
  2. Use EditedMediaItem to apply video effects (changing resolution).
  3. Build a Composition with the EditedMediaItem.
  4. Initialize a Transformer with the required settings.
  5. Start the transformation process.

Code Snippet:

val clippedMediaItem = MediaItem.Builder()
    .setClippingConfiguration(
        MediaItem.ClippingConfiguration.Builder()
            .setStartPositionMs(0)
            .setEndPositionMs(10000)
            .build()).build()

val editedMediaItem = EditedMediaItem.Builder(clippedMediaItem).setEffects(Effects(
    /* audioProcessors= */ listOf(),
    /* videoEffects= */ listOf(Presentation.createForWidthAndHeight(640, 480, Presentation.LAYOUT_SCALE_TO_FIT))
))
.build()

val composition: Composition =
    Composition.Builder(EditedMediaItemSequence(editedMediaItem)).build()

val transformer = Transformer.Builder(this)
    .setEncoderFactory(DefaultEncoderFactory.Builder(this)
        .setRequestedVideoEncoderSettings(
            VideoEncoderSettings.Builder()
                .setBitrate(2_000_000)
                .build()
        ).build()
    ).build()

transformer.start(composition, videoPath.replace(".mp4","_clipped.mp4"))

Expected Behavior: The video should be trimmed to the first 10 seconds and have its resolution changed to 640x480.

Actual Behavior: I receive a ExportException: Unexpected runtime error.

Notes:

Dependencies:

implementation(libs.androidx.media3.transformer)
implementation(libs.androidx.media3.effect)
implementation(libs.androidx.media3.common)
droid-girl commented 3 months ago

If this is the exact copy of your code, I think you are missing to setUri of your item in the clippedMediaItem:

val clippedMediaItem = MediaItem.Builder()
    .setUri(YOUR_URI) // here
    .setClippingConfiguration(
        MediaItem.ClippingConfiguration.Builder()
            .setStartPositionMs(0)
            .setEndPositionMs(10000)
            .build()).build()
PrinceVergil commented 3 months ago

Thanks @droid-girl it was silly mistake.

droid-girl commented 3 months ago

@calren maybe we should investigate if we can give a better error message here?

droid-girl commented 3 months ago

@PrinceVergil no problem. Happens to everyone

calren commented 3 months ago

will look into providing a better message for this use cases. @PrinceVergil thanks for reporting to help us make the API more robust!