Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

passing bitrate while muxing audio video ( replacing audio of video with new audio ) #206

Open
Pulkitt3 opened this issue Jul 20, 2022 · 3 comments

Comments

@Pulkitt3
Copy link

we are mixing audio and video with litr and replacing the existing audio with the new audio but the video size is very large and we want to compress the size of the video. How can we pass the video bitrate during track mixing? You already have a demo for that please help.

@izzytwosheds
Copy link
Contributor

When creating a TrackTransform for video track, pass in a target MediaFormat with intended resolution/bitrate. That should transcode the track to whatever size you want. You can estimate the size as bitrate * video duration. Play around with resolution and bitrate - low bitrate at high resolution will not look good. Let me know how it works out or if you have more questions.

@Pulkitt3
Copy link
Author

Can you please help where I can pass bitrate in the below method?

private fun videoAudioCompress() {
var sourceMedia = SourceMedia()
var sourceAudioMedia = SourceMedia()
updateSourceMedia(sourceMedia, Variables.uriPath)
outputUri?.let { updateSourceMedia(sourceAudioMedia, it) }

    val targetFile = File(
        TransformationUtil.getTargetFileDirectory(),
        "transcoded_"+GenerateRandom(999) + TransformationUtil.getDisplayName(
            this,
            sourceMedia.uri
        )
    )


    val targetAudioFile = File(
        TransformationUtil.getTargetFileDirectory(),
        "transcoded_"+GenerateRandom(999) + TransformationUtil.getDisplayName(
            this,
            sourceAudioMedia.uri
        )
    )


    targetMedia = TargetMedia()
    targetAudioMedia = TargetMedia()
    val transcodingConfigPresenter = TranscodingConfigPresenter(
        targetMedia!!
    )

    targetMedia?.setTargetFile(targetFile)
    targetMedia?.setTracks(sourceMedia.tracks)
    targetAudioMedia?.setTargetFile(targetAudioFile)
    targetAudioMedia?.setTracks(sourceAudioMedia.tracks)



    var transformationState = TransformationState()
    transformationState.setState(TransformationState.STATE_IDLE)
    transformationState.setStats(null)

    var mediaTransformer: MediaTransformer? = null
    mediaTransformer = MediaTransformer(getApplicationContext())
    var transformationPresenter = TransformationPresenter(this, mediaTransformer)
    var trimConfig = TrimConfig()


    if (sourceMedia != null &&
        targetMedia != null &&
        targetMedia?.includedTrackCount!! > 0 /*&&
            (transformationState.state != transformationState.STATE_RUNNING)*/) {
        transformationPresenter.muxVideoAndAudio(
            sourceMedia,
            sourceAudioMedia,
            targetMedia!!,
            transformationState,
            onFileCompress
        )
    }
}

@izzytwosheds
Copy link
Contributor

To replace an audio track and transcode a video track you have to form a list of two TrackTransforms:

  • one will take a video track from video source file and transcode it to lower resolution/bitrate in target file.
  • other will take an audio track from audio source file and use it "as is" (it can also transcode)

Both will need to know which track to take (index) from the source file and which track to put it to in target file.

Here is what it should look like:

// common target
val mediaTarget = MediaMuxerMediaTarget(outputFilePath, 2, 0, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4)

val videoMediaSource = MediaExtractorMediaSource(context, videoSourceUri)
val audioMediaSource = MediaExtractorMediaSource(context, audioSourceUri)

// video track config
val targetVideoFormat = MediaFormat.createVideoFormat("video/avc", 1280, 720) // assuming 720p landscape video
    .apply{
        setInteger(MediaFormat.KEY_BIT_RATE, 5_000_000)
        setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 3)
        setInteger(MediaFormat.KEY_FRAME_RATE, 30)
        setInteger(KEY_ROTATION, 0)
    }

// video track transform
val videoTrackTransform = TrackTransform.Builder(videoMediaSource, 0, mediaTarget) // assuming that video track index in source file is 0
    .setTargetTrack(0)
    .setDecoder(MediaCodecDecoder())
    .setEncoder(MediaCodecEncoder())
    .setRenderer(GlVideoRenderer())
    .setTargetFormat(targetVideoFormat)
    .build()

// audio track transform
val audioTrackTransform = TrackTransform.Builder(audioMediaSource, 0, mediaTarget) // assuming that audio track index in source file is 0
    .setTargetTrack(1)
    .setTargetFormat(null)
    .build()

mediaTransformer.transform(
    UUID.randomUUID.toString(),
    mutableListOf(videoTrackTransform, audioTrackTransform),
    listener,
    MediaTransformer. GRANULARITY_DEFAULT)

Feel free to make modifications. Let me know how it works out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants