Transformer, the transformation component in the Media3 architecture, can be used to edit and process audio and video, including converting encodings between different formats and modifying media content, such as editing clips from longer videos, or applying customized filter effects, as well as other audio and video editing operations.
Google has also provided the official Transformer code application examples, sample source code hosting address /androidx/media/tree/release/demos, hosting page to open after visiting the transformer directory, you can find the Transformer component of the actual use of the module code.
Because the Transformer is included in Media3, so it is quite demanding on the running environment, see the previous article "Using Media3's Exoplayer to play network video" for specific environment requirements. After you have prepared the development environment, you should introduce the ExoPlayer library of Media3 as described below, the detailed steps are as follows.
I. Modification of the module
Inside the dependencies node, add the following package-guide statement to add all the relevant libraries used by the Transformer.
Second, the event page code to increase the Transformer processing code
First create a time processing effect for audio and video, for example, the following code builds a media project that clips video clips, ready to clip the 10th through 20th seconds of a video file individually.
Next, create spatial processing effects for audio and video, including rotating and scaling the video frame, etc., and apply the specified spatial effects to the media items from the previous step. The creation code is as follows:
Then build the converter object according to the following code, specify the output video format as H265 and the output audio format as AAC, and listen to the end event and failure event of the conversion operation. The build code is as follows:
Finally, fill in the save path of the output file and call the start method of the converter object to start the audio/video conversion action. The conversion code is as follows:
Compile and run the app, you can select the video file on the real machine and perform the corresponding editing and processing operations.
For more details on FFmpeg development see