linkedin / LiTr

Lightweight hardware accelerated video/audio transcoder for Android.
BSD 2-Clause "Simplified" License
598 stars 83 forks source link

Transcode H264 video to HEVC #150

Open arianaa30 opened 2 years ago

arianaa30 commented 2 years ago

I would like to simply transcode a H264 mp4 file into HEVC. Is there any explicit example how to do this?

izzytwosheds commented 2 years ago

Yes. All you need to do is to pass in a correctly configured MediaFormat. Something like MediaFormat mediaFormat = new MediaFormat(); mediaFormat.setString(MediaFormat.KEY_MIME, "video/hevc"); mediaFormat.setInteger(MediaFormat.KEY_WIDTH, 1280); mediaFormat.setInteger(MediaFormat.KEY_HEIGHT, 720); mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 5000000); mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5); mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 30); mediaFormat.setInteger(KEY_ROTATION, 0);

arianaa30 commented 2 years ago

Can I have an exact example? I would like to only convert to HEVC (with the same quality) and leave everything unchanged. So I guess the resulting size should get almost halved.

On Mon, Dec 20, 2021 at 7:23 PM Izzat Bahadirov @.***> wrote:

Yes. All you need to do is to pass in a correctly configured MediaFormat. Something like MediaFormat mediaFormat = new MediaFormat(); mediaFormat.setString(MediaFormat.KEY_MIME, "video/hevc"); mediaFormat.setInteger(MediaFormat.KEY_WIDTH, 1280); mediaFormat.setInteger(MediaFormat.KEY_HEIGHT, 720); mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 5000000); mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5); mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 30); mediaFormat.setInteger(KEY_ROTATION, 0);

— Reply to this email directly, view it on GitHub https://github.com/linkedin/LiTr/issues/150#issuecomment-998393137, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABKBFTIHIP4TCKEAKTGDEGLUR7JKBANCNFSM5KOZTLMQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

-- Cheers

izzytwosheds commented 2 years ago

LiTr expects all those parameters to be set and passed in MediaFormat. Things like resolution, rotation, frame rate, key frame interval should be easy to extract from the source video using MediaExtractor or MediaMetadataRetriever, you can simply pass them into MediaFormat so that source and target match. Mime type you set yourself. Bitmap is a little trickier, it is not always readily available through metadata. LiTr has TranscoderUtil.estimateVideoTrackBitrate method that should help with that. You would get that value for source H.264 file and multiply by 0.6 to get target bitrate.

arianaa30 commented 2 years ago

Oops; that's so complex. I like to showcase a hevc encoded file with the same quality would have half size. How should I set the parameters?

On Tue, Dec 21, 2021, 10:53 PM Izzat Bahadirov @.***> wrote:

LiTr expects all those parameters to be set and passed in MediaFormat. Things like resolution, rotation, frame rate, key frame interval should be easy to extract from the source video using MediaExtractor or MediaMetadataRetriever, you can simply pass them into MediaFormat so that source and target match. Mime type you set yourself. Bitmap is a little trickier, it is not always readily available through metadata. LiTr has TranscoderUtil.estimateVideoTrackBitrate method that should help with that. You would get that value for source H.264 file and multiply by 0.6 to get target bitrate.

— Reply to this email directly, view it on GitHub https://github.com/linkedin/LiTr/issues/150#issuecomment-999034136, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABKBFTPLZQ7M6TEM53ACORTUSDHZVANCNFSM5KOZTLMQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

izzytwosheds commented 2 years ago

This is not very complex. Android provides APIs to extract metadata from a video. So you can use them to get those values and pass them to LiTr, so that target video parameters match source video parameters. All except MIME type and bitrate, which define which codec should be used and how. We will think about maybe adding a utility that helps create MediaFormat for common cases like this.

clort81 commented 2 years ago

Low hanging, high value fruit to be coded into an app here.

izzytwosheds commented 2 years ago

@clort81 good point. Technically, LiTr already supports that, all one has to do is to set "video/hevc" MIME type in target video media format. I guess, value would be in some sort of helper util or a preset to handle typical use cases - standard video resolutions, audio bitrates, popular codecs, etc. Suggestions welcome.