Skip to content

getVideoMetaData doesn't take into account video rotation #332

Open
@andrejpavlovic

Description

@andrejpavlovic

Current behavior

On Android, getVideoMetaData does not take into account video orientation. A portrait video has the same width and height as a landscape one.

Expected behavior

Width and height should be reversed for portrait images vs landscape ones.

Platform

  • Android
  • iOS

React Native Version

0.76.6

React Native Compressor Version

1.10.3

Reproducible Steps And Demo

Looking at the code here

fun getVideoMetaData(filePath: String, promise: Promise) {
var filePath: String? = filePath
try {
filePath = Utils.getRealPath(filePath, reactContext)
val uri = Uri.parse(filePath)
val srcPath = uri.path
val metaRetriever = MediaMetadataRetriever()
metaRetriever.setDataSource(srcPath)
val file = File(srcPath)
val sizeInBytes = (file.length()).toDouble()
val actualHeight = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)!!.toInt()
val actualWidth = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)!!.toInt()
val duration = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)!!.toDouble()
val creationTime = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DATE)
val extension = filePath!!.substring(filePath.lastIndexOf(".") + 1)
val params = Arguments.createMap()
params.putDouble("size", sizeInBytes)
params.putInt("width", actualWidth)
params.putInt("height", actualHeight)
params.putDouble("duration", duration / 1000)
params.putString("extension", extension)
params.putString("creationTime", creationTime.toString())
promise.resolve(params)
} catch (e: Exception) {
promise.reject(e)
}
}
}

It seems that use of METADATA_KEY_VIDEO_ROTATION in there somewhere is necessary, similar to

// Handle rotation values and swapping height and width if needed
rotation = when (rotation) {
90, 270 -> {
val tempHeight = newHeight
newHeight = newWidth
newWidth = tempHeight
0
}
180 -> 0
else -> rotation
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions