Skip to content

Commit

Permalink
use letterbox aspect ratio fit: produce a video the size of the devic…
Browse files Browse the repository at this point in the history
…e screen, with letterbox scaling instead of center crop
  • Loading branch information
mzorz committed Sep 7, 2019
1 parent 2cc61ba commit b25545e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,11 @@ class PhotoEditor private constructor(builder: Builder) :

Mp4Composer(videoInputPath, videoOutputPath)
.with(context)
.size(width, height)
// .size(width, height)
// IMPORTANT: as we aim at a WYSIWYG UX, we need to produce a video of size equal to that of the phone
// screen, given the user may be seeing a letterbox landscape video and placing emoji / text around
// the black parts of the screen.
.size(widthParent, heightParent)
.fillMode(FillMode.PRESERVE_ASPECT_FIT)
.filter(GlFilterGroup(filterCollection))
.listener(object : Mp4Composer.Listener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class VideoPlayingBasicHandling : Fragment(), SurfaceFragmentHandler, VideoPlaye

override fun onSurfaceTextureSizeChanged(texture: SurfaceTexture, width: Int, height: Int) {
if (currentExternalUri != null && videoHeight > 0 && videoWidth > 0) {
updateTextureViewSizeForCropping(width, height)
// updateTextureViewSizeForCropping(width, height)
updateTextureViewSizeForLetterbox(videoWidth.toInt(), videoHeight.toInt())
}
}

Expand Down Expand Up @@ -158,7 +159,8 @@ class VideoPlayingBasicHandling : Fragment(), SurfaceFragmentHandler, VideoPlaye
currentExternalUri?.let {
textureView.setTransform(originalMatrix)
calculateVideoSize(it)
updateTextureViewSizeForCropping(textureView.measuredWidth, textureView.measuredHeight)
// updateTextureViewSizeForCropping(textureView.measuredWidth, textureView.measuredHeight)
updateTextureViewSizeForLetterbox(videoWidth.toInt(), videoHeight.toInt())
mediaPlayer = MediaPlayer().apply {
setDataSource(context!!, currentExternalUri!!)
setSurface(s)
Expand Down Expand Up @@ -241,6 +243,38 @@ class VideoPlayingBasicHandling : Fragment(), SurfaceFragmentHandler, VideoPlaye
textureView.setTransform(matrix)
}

private fun updateTextureViewSizeForLetterbox(videoWidth: Int, videoHeight: Int) {
val viewWidth = textureView.getWidth()
val viewHeight = textureView.getHeight()
val aspectRatio = videoHeight.toDouble() / videoWidth

val newWidth: Int
val newHeight: Int
if (viewHeight > (viewWidth * aspectRatio).toInt()) {
// limited by narrow width; restrict height
newWidth = viewWidth
newHeight = (viewWidth * aspectRatio).toInt()
} else {
// limited by short height; restrict width
newWidth = (viewHeight / aspectRatio).toInt()
newHeight = viewHeight
}
val xoff = (viewWidth - newWidth) / 2
val yoff = (viewHeight - newHeight) / 2
Log.v(
TAG, "video=" + videoWidth + "x" + videoHeight +
" view=" + viewWidth + "x" + viewHeight +
" newView=" + newWidth + "x" + newHeight +
" off=" + xoff + "," + yoff
)

val txform = Matrix()
textureView.getTransform(txform)
txform.setScale(newWidth.toFloat() / viewWidth, newHeight.toFloat() / viewHeight)
txform.postTranslate(xoff.toFloat(), yoff.toFloat())
textureView.setTransform(txform)
}

companion object {
private val instance = VideoPlayingBasicHandling()

Expand Down

0 comments on commit b25545e

Please sign in to comment.