[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-26 (世界標準時間)。"],[],[],null,["# Transformations\n\nTranscode between formats\n-------------------------\n\nYou can specify the output audio and video formats you want to produce when\nbuilding Transformer. For example, the following code shows how\nto configure Transformer to output H.264/AVC video and AAC audio: \n\n### Kotlin\n\n```kotlin\nTransformer.Builder(context)\n .setVideoMimeType(MimeTypes.VIDEO_H264)\n .setAudioMimeType(MimeTypes.AUDIO_AAC)\n .build()\n```\n\n### Java\n\n```java\nnew Transformer.Builder(context)\n .setVideoMimeType(MimeTypes.VIDEO_H264)\n .setAudioMimeType(MimeTypes.AUDIO_AAC)\n .build();\n```\n\n\u003cbr /\u003e\n\nIf the input media format already matches the configurations for audio\nor video, Transformer automatically switches to *transmuxing*, that is, copying\nthe compressed samples from the input container to the output container without\nmodification. This avoids the computational cost and potential quality loss of\ndecoding and re-encoding in the same format.\n\nRemove audio or video\n---------------------\n\nRemove audio or video using `EditedMediaItem.Builder`, for example: \n\n### Kotlin\n\n```kotlin\nEditedMediaItem.Builder(inputMediaItem).setRemoveAudio(true).build()\n```\n\n### Java\n\n```java\nnew EditedMediaItem.Builder(inputMediaItem).setRemoveAudio(true).build();\n```\n\n\u003cbr /\u003e\n\nTrim a clip\n-----------\n\nYou can remove any media outside specified start and end timestamps by setting\nthe clipping configuration on the input media item. For example, to produce a\nclip containing only the media between 10 seconds and 20 seconds: \n\n### Kotlin\n\n```kotlin\nval inputMediaItem = MediaItem.Builder()\n .setUri(uri)\n .setClippingConfiguration(\n ClippingConfiguration.Builder()\n .setStartPositionMs(10_000)\n .setEndPositionMs(20_000)\n .build())\n .build()\n```\n\n### Java\n\n```java\nMediaItem inputMediaItem =\n new MediaItem.Builder()\n .setUri(uri)\n .setClippingConfiguration(\n new MediaItem.ClippingConfiguration.Builder()\n .setStartPositionMs(10_000)\n .setEndPositionMs(20_000)\n .build())\n .build();\n```\n\n\u003cbr /\u003e\n\nOptimizing trims\n----------------\n\nTo reduce the latency of trimming the beginning of a video, enable trim\noptimization. \n\n### Kotlin\n\n```kotlin\nTransformer.Builder(context)\n .experimentalSetTrimOptimizationEnabled(true)\n .build()\n```\n\n### Java\n\n```java\nnew Transformer.Builder(context)\n .experimentalSetTrimOptimizationEnabled(true)\n .build();\n```\n\n\u003cbr /\u003e\n\nThis speeds up the export by decoding and re-encoding as little of the video as\npossible, then stitching the re-encoded data with the rest of the original\nvideo. The optimization relies on being able to stitch part of the input file\nwith newly-encoded output, which means that the encoder's output format and the\ninput format must be compatible. So, for example, if the file was originally\nproduced on a device with a different encoder implementation then it's likely\nthat it won't be possible to apply the optimization.\nFor the optimization to succeed, the encoder provided to Transformer via the\n`EncoderFactory` must have a level and profile compatible with the input format.\n\nThis optimization only works with single-asset MP4 input with no effects except\nno op video effects and rotations divisible by 90 degrees. If the optimization\nfails, Transformer automatically falls back to normal export, and reports the\noutcome of the optimization in `ExportResult.OptimizationResult`.\n\nWe are validating this functionality and expect it to become non-experimental\nin a later release.\n\nVideo edits\n-----------\n\n`EditedMediaItems` have lists of audio processors and video effects to apply in\norder. The library includes video effect implementations for common use cases,\nor you can write custom effects and pass them in when building edited media\nitems.\n\nYou can rescale media, which can be useful to save on processing resources or\nbandwidth when dealing with very high resolution input, such as 4k or 8k video.\nFor example, to scale proportionally to 480 pixels high: \n\n### Kotlin\n\n```kotlin\nEditedMediaItem.Builder(MediaItem.fromUri(uri))\n .setEffects(Effects(\n /* audioProcessors= */ listOf(),\n /* videoEffects= */ listOf(Presentation.createForHeight(480))\n )).build()\n```\n\n### Java\n\n```java\nnew EditedMediaItem.Builder(MediaItem.fromUri(uri))\n .setEffects(new Effects(\n /* audioProcessors= */ ImmutableList.of(),\n /* videoEffects= */ ImmutableList.of(Presentation.createForHeight(480))))\n .build();\n```\n\n\u003cbr /\u003e\n\nAlternatively, you can scale by a given factor, for example, to halve the size: \n\n### Kotlin\n\n```kotlin\nval editedMediaItem = EditedMediaItem.Builder(MediaItem.fromUri(uri))\n .setEffects(Effects(\n /* audioProcessors= */ listOf(),\n /* videoEffects= */ listOf(\n ScaleAndRotateTransformation.Builder().setScale(.5f, .5f).build())\n )).build()\n```\n\n### Java\n\n```java\nnew EditedMediaItem.Builder(MediaItem.fromUri(uri))\n .setEffects(new Effects(\n /* audioProcessors= */ ImmutableList.of(),\n /* videoEffects= */ ImmutableList.of(\n new ScaleAndRotateTransformation.Builder().setScale(.5f, .5f).build())))\n .build();\n```\n\n\u003cbr /\u003e\n\nYou can configure rotation in the same way: \n\n### Kotlin\n\n```kotlin\nEditedMediaItem.Builder(MediaItem.fromUri(uri))\n .setEffects(Effects(\n /* audioProcessors= */ listOf(),\n /* videoEffects= */ listOf(\n ScaleAndRotateTransformation.Builder()\n .setRotationDegrees(90f)\n .build())\n )).build()\n```\n\n### Java\n\n```java\nnew EditedMediaItem.Builder(MediaItem.fromUri(uri))\n .setEffects(new Effects(\n /* audioProcessors= */ ImmutableList.of(),\n /* videoEffects= */ ImmutableList.of(\n new ScaleAndRotateTransformation.Builder().setRotationDegrees(90f).build())))\n .build();\n```\n\n\u003cbr /\u003e\n\n### Custom video effects\n\nThe `Effects` constructor accepts a list of audio and video effects to apply.\nInternally, Transformer's effects framework converts the list of video effects\ninto a sequence of GL shader programs that are applied in order. In some cases,\nthe effects framework is able to apply multiple effects with one shader program.\nFor example, one shader program can apply multiple consecutive matrix\ntransformations, which improves efficiency and quality.\n\nVideo effects are also supported for preview in ExoPlayer, using\n`ExoPlayer.setVideoEffects`. For an example of how to use this API, check out\nthe [effect demo app](https://github.com/androidx/media/tree/release/demos/effect).\n\nThe [demo app](/media/media3/transformer/demo-application) includes examples of custom video effects.\n\nAudio edits\n-----------\n\nAudio effects are implemented by applying a sequence of `AudioProcessor`\ninstances to raw (PCM) audio. ExoPlayer supports passing audio processors to the\n`DefaultAudioSink.Builder`, which allows previewing audio edits."]]