Skip to content

Commit

Permalink
[fix] mp4 wrong duration display on some device
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangdg committed Feb 13, 2023
1 parent 80169b4 commit 72f01ac
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import android.content.ContentValues
import android.content.Context
import android.media.MediaCodec
import android.media.MediaFormat
import android.media.MediaMetadataRetriever
import android.media.MediaMuxer
import android.os.Environment
import android.os.Handler
Expand Down Expand Up @@ -260,7 +261,8 @@ class Mp4Muxer(
values.put(MediaStore.Video.Media.DATA, path)
values.put(MediaStore.Video.Media.DISPLAY_NAME, file.name)
values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4")
values.put(MediaStore.Video.Media.SIZE, file.totalSpace)
values.put(MediaStore.Video.Media.SIZE, file.length())
values.put(MediaStore.Video.Media.DURATION, getLocalVideoDuration(file.path))
if (MediaUtils.isAboveQ()) {
val relativePath = "${Environment.DIRECTORY_DCIM}${File.separator}Camera"
val dateExpires = (System.currentTimeMillis() + DateUtils.DAY_IN_MILLIS) / 1000
Expand All @@ -273,6 +275,17 @@ class Mp4Muxer(

fun isMuxerStarter() = mVideoTrackerIndex != -1 && (mAudioTrackerIndex != -1 || isVideoOnly)

private fun getLocalVideoDuration(filePath: String?): Long {
return try {
val mmr = MediaMetadataRetriever()
mmr.setDataSource(filePath)
mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)?.toLong() ?:0L
} catch (e: Exception) {
e.printStackTrace()
0L
}
}

companion object {
private const val TAG = "Mp4Muxer"
}
Expand Down

0 comments on commit 72f01ac

Please sign in to comment.