Skip to content

Commit

Permalink
Merge pull request #233 from numandev1/fix/duration-video-metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
numandev1 authored Nov 9, 2023
2 parents 1af07ea + 7dbb17c commit 6064f5f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ class VideoMain(private val reactContext: ReactApplicationContext) {
val metaRetriever = MediaMetadataRetriever()
metaRetriever.setDataSource(srcPath)
val file = File(srcPath)
val sizeInKBs = (file.length() / 1024).toFloat()
val sizeInKBs = (file.length() / 1024).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)!!.toLong()
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.putString("size", sizeInKBs.toString())
params.putString("width", actualWidth.toString())
params.putString("height", actualHeight.toString())
params.putString("duration", (duration / 1000).toString())
params.putDouble("size", sizeInKBs)
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)
Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ PODS:
- React-Codegen
- React-RCTFabric
- ReactCommon/turbomodule/core
- react-native-compressor (1.8.13):
- react-native-compressor (1.8.15):
- hermes-engine
- NextLevelSessionExporter
- RCT-Folly (= 2021.07.22.00)
Expand Down Expand Up @@ -1381,7 +1381,7 @@ SPEC CHECKSUMS:
React-jsinspector: 194e32c6aab382d88713ad3dd0025c5f5c4ee072
React-logger: cebf22b6cf43434e471dc561e5911b40ac01d289
react-native-cameraroll: 5d9523136a929b58f092fd7f0a9a13367a4b46e3
react-native-compressor: 36560ed68379f0fe9b52b2ee8c1e3d9853dd86b8
react-native-compressor: fb94a06841de42572dfbefe5f7e26a89f47161dd
react-native-document-picker: c9ac93d7b511413f4a0ed61c92ff6c7b1bcf4f94
react-native-get-random-values: dee677497c6a740b71e5612e8dbd83e7539ed5bb
react-native-image-picker: 9b4b1d0096500050cbdabf8f4fd00b771065d983
Expand Down
14 changes: 8 additions & 6 deletions ios/Video/VideoMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,13 @@ class VideoCompressor {
}



func getVideoMetaData(_ filePath: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
do {
VideoCompressor.getAbsoluteVideoPath(filePath, options: [:]) { absoluteImagePath in
if absoluteImagePath.hasPrefix("file://") {
let absoluteImagePath = absoluteImagePath.replacingOccurrences(of: "file://", with: "")

let absoluteImagePath = URL(string: absoluteImagePath)!.path
let fileManager = FileManager.default
var isDir: ObjCBool = false

Expand All @@ -323,7 +325,7 @@ class VideoCompressor {

let attrs = try? fileManager.attributesOfItem(atPath: absoluteImagePath)
if let fileSize = attrs?[FileAttributeKey.size] as? UInt64 {
let fileSizeString = String(fileSize)
let fileSizeString = fileSize

var result: [String: Any] = [:]
let assetOptions: [String: Any] = [AVURLAssetPreferPreciseDurationAndTimingKey: true]
Expand All @@ -332,13 +334,13 @@ class VideoCompressor {
let size = avAsset.naturalSize
let _extension = (absoluteImagePath as NSString).pathExtension
let time = asset.duration
let seconds = Int(ceil(Double(time.value / Int64(time.timescale))))
let seconds = Double(time.value) / Double(time.timescale)

result["width"] = String(format: "%.2f", size.width)
result["height"] = String(format: "%.2f", size.height)
result["width"] = size.width
result["height"] = size.height
result["extension"] = _extension
result["size"] = fileSizeString
result["duration"] = String(seconds)
result["duration"] = seconds

var commonMetadata: [AVMetadataItem] = []
for key in self.metadatas {
Expand Down
8 changes: 7 additions & 1 deletion src/utils/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ type createVideoThumbnailType = (
}>;

type clearCacheType = (cacheDir?: string) => Promise<string>;
type getVideoMetaDataType = (filePath: string) => Promise<string>;
type getVideoMetaDataType = (filePath: string) => Promise<{
extension: string;
size: number;
duration: number;
width: number;
height: number;
}>;
type getRealPathType = (
path: string,
type: 'video' | 'image'
Expand Down

0 comments on commit 6064f5f

Please sign in to comment.