Media3 Transformer 正在积极开发中,我们期待收到您的反馈意见!欢迎在
问题跟踪器中提供反馈、提交功能请求和错误报告。关注
ExoPlayer 博客,了解最新动态。
多重素材资源修改
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
使用 Transformer,您可以组合多个媒体资源,例如视频、
来创建 Composition
。
导出乐曲
应用转换
(如特效或剪辑修改)MediaItem
,则应创建一个
EditedMediaItem
来表示已应用转换的资源。
然后,可将 EditedMediaItem
对象串联在一起以创建
EditedMediaItemSequence
。
例如,您可以创建一个 EditedMediaItemSequence
,在其中修改两个
视频。EditedMediaItemSequence
中的项会依序排序,
确保它们在时间上不重叠
Composition
是一个或多个 EditedMediaItemSequence
的组合
对象的操作。Composition
中的所有 EditedMediaItemSequence
对象都是混合的
,以便您组合视频素材资源和音频素材资源。
Composition
对象可以使用 Transformer 进行导出。
这是一个创建和导出视频资源的示例,该资源包含两个
编辑过的视频剪辑,叠加在音轨上:
Kotlin
val transformer = ... // Set up Transformer instance
val video1 = EditedMediaItem.Builder(
MediaItem.fromUri(video1Uri))
.build()
val video2 = EditedMediaItem.Builder(
MediaItem.fromUri(video2Uri))
.build()
val videoSequence = EditedMediaItemSequence(
video1, video2)
val backgroundAudio = EditedMediaItem.Builder(
MediaItem.fromUri(audioUri))
.build()
val backgroundAudioSequence = EditedMediaItemSequence(
ImmutableList.of(backgroundAudio),
/* isLooping= */ true) // Loop audio track through duration of videoSequence
val composition = Composition.Builder(
videoSequence,
backgroundAudioSequence)
.build()
val filePath = ... // Provide file path to save Composition
transformer.start(composition, filePath)
Java
Transformer transformer = ... // Set up Transformer instance
EditedMediaItem video1 = new EditedMediaItem.Builder(
MediaItem.fromUri(video1Uri))
.build();
EditedMediaItem video2 = new EditedMediaItem.Builder(
MediaItem.fromUri(video2Uri))
.build();
EditedMediaItemSequence videoSequence = new EditedMediaItemSequence(
video1, video2);
EditedMediaItem backgroundAudio = new EditedMediaItem.Builder(
MediaItem.fromUri(audioUri))
.build();
EditedMediaItemSequence backgroundAudioSequence = new EditedMediaItemSequence(
ImmutableList.of(backgroundAudio),
/* isLooping= */ true); // Loop audio track through duration of videoSequence
String filePath = ... // Provide file path to save Composition
Composition composition = new Composition.Builder(
videoSequence,
backgroundAudioSequence)
.build();
transformer.start(composition, filePath);
支持的用例示例
这里仅列出了 Transformer API 的
组合支持:
- 依序组合音频、图片和视频素材资源。不过,所有项目
必须具有相同的轨道。例如,您不能将序列
其中包含一个纯音频文件,后跟一个视频文件。
- 向视频素材资源添加背景音频。
- 向组合添加效果。
- 将 HDR 输入色调映射到 SDR,可生成更优质的 SDR 输出。
当前限制
乐曲中的序列必须满足
Transformer.start()
。
此外,使用
乐曲:
- 开始播放带有偏移量的
EditedMediaItemSequence
。
- 淡入淡出视频或音轨
功能请求
如果您对 Transformer API 有任何功能请求,请在
Media3 GitHub 代码库。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[[["易于理解","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"]],["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Multi-asset editing\n\nUsing Transformer, you can combine multiple media assets, such as videos,\nimages, and audio files to create a `Composition`.\n\nExporting a Composition\n-----------------------\n\nTo apply [transformations](/media/media3/transformer/transformations)\n(such as effects or trimming edits) to a `MediaItem`, you should create an\n[`EditedMediaItem`](/reference/androidx/media3/transformer/EditedMediaItem)\nto represent the asset that has the transformations applied to it.\n\n`EditedMediaItem` objects can then be concatenated together to create an\n[`EditedMediaItemSequence`](/reference/androidx/media3/transformer/EditedMediaItemSequence).\nFor example, you can create an `EditedMediaItemSequence` with two edited\nvideos. Items inside an `EditedMediaItemSequence` are ordered sequentially and\ndon't overlap in time.\n\nA `Composition` is the combination of one or more `EditedMediaItemSequence`\nobjects. All `EditedMediaItemSequence` objects in the `Composition` are mixed\ntogether, allowing you to combine video and audio assets.\n\n`Composition` objects can be exported using Transformer.\n\nHere is an example of creating and exporting a video asset that consists of two\nedited video clips, overlaid with an audio track: \n\n### Kotlin\n\n```kotlin\nval transformer = ... // Set up Transformer instance\n\nval video1 = EditedMediaItem.Builder(\n MediaItem.fromUri(video1Uri))\n .build()\n\nval video2 = EditedMediaItem.Builder(\n MediaItem.fromUri(video2Uri))\n .build()\n\nval videoSequence = EditedMediaItemSequence(\n video1, video2)\n\nval backgroundAudio = EditedMediaItem.Builder(\n MediaItem.fromUri(audioUri))\n .build()\n\nval backgroundAudioSequence = EditedMediaItemSequence(\n ImmutableList.of(backgroundAudio),\n /* isLooping= */ true) // Loop audio track through duration of videoSequence\n\nval composition = Composition.Builder(\n videoSequence,\n backgroundAudioSequence)\n .build()\n\nval filePath = ... // Provide file path to save Composition\n\ntransformer.start(composition, filePath)\n```\n\n### Java\n\n```java\nTransformer transformer = ... // Set up Transformer instance\n\nEditedMediaItem video1 = new EditedMediaItem.Builder(\n MediaItem.fromUri(video1Uri))\n .build();\n\nEditedMediaItem video2 = new EditedMediaItem.Builder(\n MediaItem.fromUri(video2Uri))\n .build();\n\nEditedMediaItemSequence videoSequence = new EditedMediaItemSequence(\n video1, video2);\n\nEditedMediaItem backgroundAudio = new EditedMediaItem.Builder(\n MediaItem.fromUri(audioUri))\n .build();\n\nEditedMediaItemSequence backgroundAudioSequence = new EditedMediaItemSequence(\n ImmutableList.of(backgroundAudio),\n /* isLooping= */ true); // Loop audio track through duration of videoSequence\n\nString filePath = ... // Provide file path to save Composition\n\nComposition composition = new Composition.Builder(\n videoSequence,\n backgroundAudioSequence)\n .build();\n\ntransformer.start(composition, filePath);\n```\n\n\u003cbr /\u003e\n\nExamples of supported use cases\n-------------------------------\n\nThis is a non-exhaustive list of use cases that the Transformer API\nsupports with Compositions:\n\n- Sequentially combining audio, image, and video assets. However, all items in a sequence must have the same tracks. For example, you cannot have a sequence that contains an audio only file, followed by a video file.\n- Adding background audio to a video asset.\n- Adding effects to a Composition.\n- Tone mapping HDR input to SDR to generate better visual quality SDR output.\n\nCurrent limitations\n-------------------\n\nSequences within a Composition must meet the conditions outlined in\n[`Transformer.start()`](/reference/androidx/media3/transformer/Transformer#start(androidx.media3.transformer.Composition,java.lang.String)).\nFurthermore, the following operations are not yet supported when working with\nCompositions:\n\n- Starting playback of an `EditedMediaItemSequence` with an offset.\n- Crossfading video or audio tracks\n\nFeature requests\n----------------\n\nIf you have any feature requests for the Transformer API, file an issue on the\n[Media3 GitHub repository](https://github.com/androidx/media/issues)."]]