HomeMobileFrequent media processing operations with Jetpack Media3 Transformer

Frequent media processing operations with Jetpack Media3 Transformer



Frequent media processing operations with Jetpack Media3 Transformer

Posted by Nevin Mital – Developer Relations Engineer, and Kristina Simakova – Engineering Supervisor

Android customers have demonstrated an growing want to create, personalize, and share video content material on-line, whether or not to protect their recollections or to make folks chuckle. As such, media enhancing is a cornerstone of many partaking Android apps, and traditionally builders have typically relied on exterior libraries to deal with operations equivalent to Trimming and Resizing. Whereas these options are highly effective, integrating and managing exterior library dependencies can introduce complexity and result in challenges with managing efficiency and high quality.

The Jetpack Media3 Transformer APIs supply a local Android answer that streamline media enhancing with quick efficiency, intensive customizability, and broad gadget compatibility. On this weblog put up, we’ll stroll by means of a few of the most typical enhancing operations with Transformer and talk about its efficiency.

Getting arrange with Transformer

To get began with Transformer, try our Getting Began documentation for particulars on find out how to add the dependency to your undertaking and a primary understanding of the workflow when utilizing Transformer. In a nutshell, you’ll:

    • Create one or many MediaItem cases out of your video file(s), then
    • Apply item-specific edits to them by constructing an EditedMediaItem for every MediaItem,
    • Create a Transformer occasion configured with settings relevant to the entire exported video,
    • and at last begin the export to avoid wasting your utilized edits to a file.

Apart: It’s also possible to use a CompositionPlayer to preview your edits earlier than exporting, however that is out of scope for this weblog put up, as this API continues to be a piece in progress. Please keep tuned for a future put up!

Right here’s what this appears to be like like in code:

val mediaItem = MediaItem.Builder().setUri(mediaItemUri).construct()
val editedMediaItem = EditedMediaItem.Builder(mediaItem).construct()
val transformer = 
  Transformer.Builder(context)
    .addListener(/* Add a Transformer.Listener occasion right here for completion occasions */)
    .construct()
transformer.begin(editedMediaItem, outputFilePath)

Transcoding, Trimming, Muting, and Resizing with the Transformer API

Let’s now check out 4 of the commonest single-asset media enhancing operations, beginning with Transcoding.

Transcoding is the method of re-encoding an enter file right into a specified output format. For this instance, we’ll request the output to have video in HEVC (H265) and audio in AAC. Beginning with the code above, listed below are the traces that change:

val transformer = 
  Transformer.Builder(context)
    .addListener(...)
    .setVideoMimeType(MimeTypes.VIDEO_H265)
    .setAudioMimeType(MimeTypes.AUDIO_AAC)
    .construct()

A lot of you could already be acquainted with FFmpeg, a well-liked open-source library for processing media recordsdata, so we’ll additionally embrace FFmpeg instructions for every instance to function a useful reference. Right here’s how one can carry out the identical transcoding with FFmpeg:

$ ffmpeg -i $inputVideoPath -c:v libx265 -c:a aac $outputFilePath

The subsequent operation we’ll strive is Trimming.

Particularly, we’ll set Transformer as much as trim the enter video from the three second mark to the 8 second mark, leading to a 5 second output video. Beginning once more from the code within the “Getting arrange” part above, listed below are the traces that change:

// Configure the trim operation by including a ClippingConfiguration to
// the media merchandise
val clippingConfiguration =
   MediaItem.ClippingConfiguration.Builder()
     .setStartPositionMs(3000)
     .setEndPositionMs(8000)
     .construct()
val mediaItem =
   MediaItem.Builder()
     .setUri(mediaItemUri)
     .setClippingConfiguration(clippingConfiguration)
     .construct()

// Transformer additionally has a trim optimization function we are able to allow.
// This may prioritize Transmuxing over Transcoding the place attainable.
// See extra about Transmuxing additional down on this put up.
val transformer = 
  Transformer.Builder(context)
    .addListener(...)
    .experimentalSetTrimOptimizationEnabled(true)
    .construct()

With FFmpeg:

$ ffmpeg -ss 00:00:03 -i $inputVideoPath -t 00:00:05 $outputFilePath

Subsequent, we are able to mute the audio within the exported video file.

val editedMediaItem = 
  EditedMediaItem.Builder(mediaItem)
    .setRemoveAudio(true)
    .construct()

The corresponding FFmpeg command:

$ ffmpeg -i $inputVideoPath -c copy -an $outputFilePath

And for our closing instance, we’ll strive resizing the enter video by scaling it right down to half its authentic top and width.

val scaleEffect = 
  ScaleAndRotateTransformation.Builder()
    .setScale(0.5f, 0.5f)
    .construct()
val editedMediaItem =
  EditedMediaItem.Builder(mediaItem)
    .setEffects(
      /* audio */ Results(emptyList(), 
      /* video */ listOf(scaleEffect))
    )
    .construct()

An FFmpeg command may seem like this:

$ ffmpeg -i $inputVideoPath -filter:v scale=w=trunc(iw/4)*2:h=trunc(ih/4)*2 $outputFilePath

In fact, you too can mix these operations to use a number of edits on the identical video, however hopefully these examples serve to reveal that the Transformer APIs make configuring these edits easy.

Transformer API Efficiency outcomes

Listed here are some benchmarking measurements for every of the 4 operations taken with the Stopwatch API, operating on a Pixel 9 Professional XL gadget:

(Notice that efficiency for operations like these can depend upon a wide range of causes, equivalent to the present load the gadget is beneath, so the numbers under must be taken as tough estimates.)

Enter video format: 10s 720p H264 video with AAC audio

  • Transcoding to H265 video and AAC audio: ~1300ms
  • Trimming video to 00:03-00:08: ~2300ms
  • Muting audio: ~200ms
  • Resizing video to half top and width: ~1200ms

Enter video format: 25s 360p VP8 video with Vorbis audio

  • Transcoding to H265 video and AAC audio: ~3400ms
  • Trimming video to 00:03-00:08: ~1700ms
  • Muting audio: ~1600ms
  • Resizing video to half top and width: ~4800ms

Enter video format: 4s 8k H265 video with AAC audio

  • Transcoding to H265 video and AAC audio: ~2300ms
  • Trimming video to 00:03-00:08: ~1800ms
  • Muting audio: ~2000ms
  • Resizing video to half top and width: ~3700ms

One method Transformer makes use of to hurry up enhancing operations is by prioritizing transmuxing for primary video edits the place attainable. Transmuxing refers back to the means of repackaging video streams with out re-encoding, which ensures high-quality output and considerably sooner processing occasions.

When not attainable, Transformer falls again to transcoding, a course of that entails first decoding video samples into uncooked knowledge, then re-encoding them for storage in a brand new container. Listed here are a few of these variations:

Transmuxing

    • Transformer’s most well-liked strategy when attainable – a fast transformation that preserves elementary streams.
    • Solely relevant to primary operations, equivalent to rotating, trimming, or container conversion.
    • No high quality loss or bitrate change.

Transmux

Transcoding

    • Transformer’s fallback strategy in instances when Transmuxing is not attainable – Includes decoding and re-encoding elementary streams.
    • Extra intensive modifications to the enter video are attainable.
    • Loss in high quality as a result of re-encoding, however can obtain a desired bitrate goal.

Transcode

We’re constantly implementing additional optimizations, such because the just lately launched experimentalSetTrimOptimizationEnabled setting that we used within the Trimming instance above.

A trim is normally carried out by re-encoding all of the samples within the file, however since encoded media samples are saved chronologically of their container, we are able to enhance effectivity by solely re-encoding the group of images (GOP) between the beginning level of the trim and the primary keyframes at/after the beginning level, then stream-copying the remainder.

Since we solely decode and encode a hard and fast portion of any file, the encoding latency is roughly fixed, no matter what the enter video period is. For lengthy movies, this improved latency is dramatic. The optimization depends on having the ability to sew a part of the enter file with newly-encoded output, which implies that the encoder’s output format and the enter format have to be appropriate.

If the optimization fails, Transformer robotically falls again to regular export.

What’s subsequent?

As a part of Media3, Transformer is a local answer with low integration complexity, is examined on and ensures compatibility with all kinds of units, and is customizable to suit your particular wants.

To dive deeper, you’ll be able to discover Media3 Transformer documentation, run our pattern apps, or learn to complement your media enhancing pipeline with Jetpack Media3. We’ve already seen app builders profit drastically from adopting Transformer, so we encourage you to strive them out your self to streamline your media enhancing workflows and improve your app’s efficiency!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments