0

I want to encode videos using media3 transformers API, it works correctly but output bitrate bigger than input bitrate. Although I am using the reducing frame rate, resolution, bitrate but it not works correctly, and it shows in video metada 1024x576 with 90 rotation. How can I fix that? Thanks in advance.

 val mediaItemSp = MediaItem.fromUri(testUri).buildUpon()
 .setClippingConfiguration(
 MediaItem.ClippingConfiguration.Builder()
 .setStartPositionMs(mStartPosition.toLong())
 .setEndPositionMs(mEndPosition.toLong()).build()
 ).build()
 val inputMediaItem = mediaItemSp
 val overlayef: OverlayEffect? = myOverlayEf(myContext)
 var targetBitrate: Int? = null
 var targetFrameRate: Int? = null
 var editedMediaItem = EditedMediaItem.Builder(inputMediaItem).setRemoveAudio(false)
 .build()
 var videoEncoderSettings = VideoEncoderSettings.Builder()
 .build()
 if (actualFrame != null) {
 if (actualFrame >= 30) {
 targetFrameRate = 30
 } else {
 targetFrameRate = null
 }
 }
 if (actualBitrate != null) {
 if (actualBitrate >= 2000000) {
 targetBitrate = 1800000
 }
 if (actualBitrate >= 1500000 && actualBitrate < 2000000) {
 targetBitrate = 1500000
 }
 if (actualBitrate >= 1000000 && actualBitrate < 1500000) {
 targetBitrate = 1000000
 }
 if (actualBitrate < 990000) {
 targetBitrate = null
 }
 }
 if (actualBitrate == null && actualFrame == null) {
 targetFrameRate = null
 targetBitrate = null
 }
 if (targetBitrate != null) {
 videoEncoderSettings = VideoEncoderSettings.Builder()
 .setBitrateMode(BITRATE_MODE_CBR)
 .setBitrate(targetBitrate)
 .build()
 }
 if (targetFrameRate != null) {
 if (overlayef != null && actualFrame != null) {
 editedMediaItem = EditedMediaItem.Builder(inputMediaItem).setRemoveAudio(false)
 .setEffects(
 Effects(
 listOf(),
 listOf(
 Presentation.createForWidthAndHeight(
 576, 1024, LAYOUT_SCALE_TO_FIT
 ),
 overlayef,
 FrameDropEffect.createSimpleFrameDropEffect(
 actualFrame.toFloat(),
 targetFrameRate.toFloat()
 )))).build()
 }
 } else {
 if (overlayef != null && actualFrame != null) {
 editedMediaItem = EditedMediaItem.Builder(inputMediaItem).setRemoveAudio(false)
 .setEffects(
 Effects(
 listOf(),
 listOf(
 Presentation.createForWidthAndHeight(
 576, 1024, LAYOUT_SCALE_TO_FIT
 ),
 overlayef,
 ))).build()
 }
 }
 val compositionBuilder = Composition.Builder(EditedMediaItemSequence(editedMediaItem))
 Log.e("getTheVideoMData", "actual bitrate $actualBitrate")
 Log.e("getTheVideoMData", "actual frame rate $actualFrame")
 Log.e("getTheVideoMData", "target bitrate $targetBitrate")
 Log.e("getTheVideoMData", "target frame rate targetFrameRate")

but when I add

.setBitrateMode(BITRATE_MODE_CBR)

the log shows

VideoEncoder error: format=Format(null, null, null, video/avc, avc1.4D401F, -1, null, [1024, 576, 29.97003, ColorInfo(BT709, Limited range, SDR SMPTE 170M, false, 8bit Luma, 8bit Chroma)], [-1, -1]), colorInfo=ColorInfo(BT709, Limited range, SDR SMPTE 170M, false, 8bit Luma, 8bit Chroma) and my log getTheVideoMData
E actual bitrate 1138922
E actual frame rate 30
E target bitrate 1000000
E target frame rate 30

marc_s
760k186 gold badges1.4k silver badges1.5k bronze badges
asked Sep 6, 2024 at 18:55
7
  • The provided information is not enough to diagnose the problem. What is videoEncoderSettings? What was the bitrate of the original video? Commented Sep 8, 2024 at 7:11
  • Sorry, I updated my question and added the video encoder settings, the original video's bitrate was 1.3 mbit, but with 720x1280 but with 576x1024 after transforming it raised to 1.6 mbit sometimes it can up to 2.5 mbit. i used a lot method with video encoder settings. And actually I also checked all of the HDR modes but not worked any of them. Commented Sep 9, 2024 at 17:01
  • The bitrate calculation in your code produces a very small number (124K). This is a typical bitrate for audio, not video. The recommended bitrate for a 480p video is 2.5 Mbps and for 720p is 5 Mbps. I suspect that the transformer could override your bitrate setting. Commented Sep 10, 2024 at 7:35
  • i also used bitrate between 1000000 and 2000000 but why the input videos bitrate arount 1 mbit but the output over 2 mbit with lower resolutions? Commented Sep 10, 2024 at 16:29
  • I also modified mycode Commented Sep 10, 2024 at 17:06

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.