Skip to content

Commit

Permalink
add rotate frame
Browse files Browse the repository at this point in the history
  • Loading branch information
sunchao committed Feb 21, 2019
1 parent 58d51df commit 0538fe0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,16 @@ class MainActivity : Activity() {
return@setOnClickListener
}

val mediaInfo = MediaInfo.getMediaInfo(videoPath!!)

AppLogger.d("ddd", "info width: ${mediaInfo.getWidth()}, height: ${mediaInfo.getHeight()}")

coder = VideoAudioCoder(videoPath!!, ENCODE_DEST_PATH + "bbb.mp4")
coder!!.setWithAudio(false)

coder!!.withScale(480, 720)
coder!!.withTrim(3000, 6000)
coder!!.withRotateFrame()

coder!!.setCallback(object : VideoAudioCoder.ResultCallback {
override fun onSucceed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class SeparateVideoCoder(private val path: String, private val mediaMuxer: Media
private var scaleWidth: Int? = null
private var scaleHeight: Int? = null

private var isRotateFrame: Boolean = false

private var callback: MediaExecuteCallback? = null
private var callbackHandler: Handler? = null

Expand Down Expand Up @@ -67,6 +69,10 @@ class SeparateVideoCoder(private val path: String, private val mediaMuxer: Media
AppLogger.d(TAG, "setting bitrate:$bitrate")
}

fun setRotateFrame(rotate: Boolean) {
isRotateFrame = rotate
}

fun prepare(trackFormat: MediaFormat): Boolean {

mediaInfo = MediaInfo.getMediaInfo(path)
Expand All @@ -79,7 +85,7 @@ class SeparateVideoCoder(private val path: String, private val mediaMuxer: Media
mediaInfo.setScale(scaleWidth!!, scaleHeight!!)
}

mediaMuxer.setOrientationHint(mediaInfo.getRotation())
// mediaMuxer.setOrientationHint(mediaInfo.getRotation())

isPrepared = initEncoder(trackFormat) && initDecoder(trackFormat)

Expand All @@ -96,6 +102,9 @@ class SeparateVideoCoder(private val path: String, private val mediaMuxer: Media

outputSurface = CodecOutputSurface2()

val rotation = mediaInfo.getRotation()
val isNeedRotateFrame = isRotateFrame && (rotation == 90 || rotation == 270)

//compatible setting,codec not support rotation format before android 21
mediaFormat.setInteger("rotation", 0)
mediaFormat.setInteger("rotation-degrees", 0)
Expand All @@ -114,7 +123,11 @@ class SeparateVideoCoder(private val path: String, private val mediaMuxer: Media
if (inputSurface != null && outputSurface != null) {

outputSurface!!.awaitNewImage()
outputSurface!!.drawImage(false)
if (isNeedRotateFrame) {
outputSurface!!.drawImage(false, 90)
} else {
outputSurface!!.drawImage(false)
}

inputSurface!!.setPresentationTime(bufferInfo.presentationTimeUs * 1000)
inputSurface!!.swapBuffers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class VideoAudioCoder(private val srcPath: String, private val dest: String) {
private var scaleWidth: Int? = null
private var scaleHeight: Int? = null

private var isRotateFrame: Boolean = false

private var videoExtractTrackIndex = -1
private var audioExtractTrackIndex = -1

Expand Down Expand Up @@ -190,6 +192,10 @@ class VideoAudioCoder(private val srcPath: String, private val dest: String) {
AppLogger.d(TAG, "setting trim start:$startMs end:$endMs")
}

fun withRotateFrame(){
isRotateFrame = true
}

fun setVideoBitrate(bitrate: Int) {
this.bitrate = bitrate
AppLogger.d(TAG, "setting bitrate:$bitrate")
Expand Down Expand Up @@ -242,6 +248,7 @@ class VideoAudioCoder(private val srcPath: String, private val dest: String) {
videoCoder = SeparateVideoCoder(srcPath, mediaMuxer, mediaExtractor)
videoCoder.withScale(scaleWidth, scaleHeight)
videoCoder.withTrim(startMs, endMs)
videoCoder.setRotateFrame(isRotateFrame)
// videoCoder.setRotate(isRotate)
bitrate?.let {
videoCoder.setBitrate(it)
Expand Down

0 comments on commit 0538fe0

Please sign in to comment.