Location>code7788 >text

FFmpeg development notes (fifty-seven) using Media3's Transformer processing video files

Popularity:395 ℃/2024-10-16 14:33:15
Following the audio/video player ExoPlayer, Google has launched Transformer, an audio/video converter, to flex its muscles in the field of audio/video processing. According to the official website of Android Developer: Jetpack Media3 is the new home of Android media library, which allows apps to present rich audio-visual experience.Media3 provides a simple architecture, able to carry out customization and reliability optimization based on the device features, which can solve the problem of fragmentation of the media section.

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.

implementation "androidx.media3:media3-transformer:1.4.0"
implementation "androidx.media3:media3-effect:1.4.0"
implementation "androidx.media3:media3-common:1.4.0"

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.

 clippingConfiguration =
  new ()
    .setStartPositionMs(10_000) // start at 10 seconds
    .setEndPositionMs(20_000) // end at 20 seconds
    .build();
MediaItem mediaItem = new ()
    .setUri(mVideoUri)
    .setClippingConfiguration(clippingConfiguration)
    .build();

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:

ScaleAndRotateTransformation rotateEffect =
  new ()
    //.setRotationDegrees(90f)
    .setScale(0.5f, 0.5f)
    .build();
Effects effects = new Effects(
    (),
    (rotateEffect)
);
EditedMediaItem editedMediaItem =
  new (mediaItem)
    .setEffects(effects)
    .build();

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:

Transformer transformer = new (this)
    .setVideoMimeType(MimeTypes.VIDEO_H265)
    .setAudioMimeType(MimeTypes.AUDIO_AAC)
    .addListener(new () {
        @Override
        public void onCompleted(Composition composition, ExportResult exportResult) {
            (mContext, "Conversion successful", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(Composition composition, ExportResult exportResult, ExportException exportException) {
            (mContext, "conversion failure", Toast.LENGTH_SHORT).show();
            (TAG, "exportException: "+());
        }
    })
    .build();

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:

String outputPath = mPath + () + ".mp4";
(editedMediaItem, outputPath);

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 seeFFmpeg Development in Action: From Zero Basics to Short Video OnlineA book.