diff --git a/build.gradle b/build.gradle index 16c8b34..0945454 100644 --- a/build.gradle +++ b/build.gradle @@ -1,15 +1,14 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.2.71' - ext.kotlin_version = '1.2.51' + ext.kotlin_version = '1.4.30' repositories { google() mavenCentral() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.2.1' + classpath 'com.android.tools.build:gradle:4.1.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 086017f..cdacd12 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Mon Sep 09 22:52:51 CST 2019 +#Fri Mar 12 14:34:38 CST 2021 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip diff --git a/libvideostudio/src/main/java/io/github/kenneycode/videostudio/Util/MediaClipper.kt b/libvideostudio/src/main/java/io/github/kenneycode/videostudio/Util/MediaClipper.kt index a6b8596..7ae28d5 100644 --- a/libvideostudio/src/main/java/io/github/kenneycode/videostudio/Util/MediaClipper.kt +++ b/libvideostudio/src/main/java/io/github/kenneycode/videostudio/Util/MediaClipper.kt @@ -45,18 +45,23 @@ class MediaClipper { MediaExtractor().apply { setDataSource(inputPath) val extractorTrackIndex = getTrackIndex(this, mediaType) + val extractorAudioTrackIndex = getTrackIndex(this, MEDIA_TYPE.AUDIO) selectTrack(extractorTrackIndex) seekTo(startTimeUs, MediaExtractor.SEEK_TO_CLOSEST_SYNC) val videoFormat = getTrackFormat(extractorTrackIndex) + val audioFormat = getTrackFormat(extractorAudioTrackIndex) videoFormat.setInteger(MediaFormat.KEY_DURATION, (endTimeUs - startTimeUs).toInt()) val mediaMuxer = MediaMuxer(outputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4) + val rotation = videoFormat.getInteger(MediaFormat.KEY_ROTATION) + mediaMuxer.setOrientationHint(rotation) val muxerTrackIndex = mediaMuxer.addTrack(videoFormat) + val muxerAudioTrackIndex = mediaMuxer.addTrack(audioFormat) mediaMuxer.start() val byteBuffer = ByteBuffer.allocate(videoFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE)) while (true) { val sampleSize = readSampleData(byteBuffer, 0) if (sampleSize < 0 || sampleTime > endTimeUs) { - Log.i("debugggg", "sampleSize = $sampleSize, sampleTime = $sampleTime, endTimeUs = $endTimeUs") + unselectTrack(extractorTrackIndex) break } val bufferInfo = MediaCodec.BufferInfo().apply { @@ -68,6 +73,27 @@ class MediaClipper { mediaMuxer.writeSampleData(muxerTrackIndex, byteBuffer, bufferInfo) advance() } + //音频 + selectTrack(extractorAudioTrackIndex) + seekTo(startTimeUs, MediaExtractor.SEEK_TO_CLOSEST_SYNC) + while (true) { + val sampleSize = readSampleData(byteBuffer, 0) + if (sampleSize < 0 || sampleTime > endTimeUs) { + unselectTrack(extractorAudioTrackIndex) + break + } + + val bufferInfo = MediaCodec.BufferInfo().apply { + offset = 0 + size = sampleSize + flags = sampleFlags + presentationTimeUs = sampleTime + } + + mediaMuxer.writeSampleData(muxerAudioTrackIndex, byteBuffer, bufferInfo) + advance() + } + release() mediaMuxer.stop() mediaMuxer.release()