The Google Photos library API does not return the duration of a video. API provides a lot of misc information like camera model and other stuff, but missing very basic things, like video duration, archive status and file size
Asked
Active
Viewed 241 times
1 Answers
0
If you have access to the file uri you could try something like this:
Android | Kotlin
fun getFileDuration(context: Context, uri: Uri): Int {
try {
val metadataRetriever = MediaMetadataRetriever()
metadataRetriever.setDataSource(context, uri)
val durationStr = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
if (durationStr != null) return Integer.parseInt(durationStr)
} catch (e: Exception) {
e.printStackTrace()
Log.w("getFileDuration", "Error getting duration of file with uri $uri with ${e.message}")
}
return 0
}
iOS | Swift
func getFileDuration(_ uri: URL) -> Int {
let asset = AVURLAsset(url: uri)
let audioDuration = asset.duration.seconds * 1000
return Int(audioDuration)
}

lubilis
- 3,942
- 4
- 31
- 54
-
How can I get in iOS? – Vishal Dec 13 '21 at 17:30
-
@Vishal I updated my answer to include both Android & iOS example – lubilis Dec 13 '21 at 17:35
-
Okay thanks, I've tried with productUrl and baseUrl, but still it is return 0. – Vishal Dec 13 '21 at 18:18
-
My answer is valid only if you have a uri for a file saved locally on the device. For external urls using google photo apis, this could help: https://stackoverflow.com/questions/56046574/retrieve-file-size-for-videos-stored-on-google-photos – lubilis Dec 13 '21 at 18:23
-
Okay let me review that stuff. – Vishal Dec 13 '21 at 18:27