From 98c963585ae212355e5aa20c676913a0f32b72a5 Mon Sep 17 00:00:00 2001 From: fansan Date: Fri, 12 Mar 2021 14:48:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7gradle=E5=92=8Ckotlin=20versi?= =?UTF-8?q?on=EF=BC=8CMediaMuxer=E6=B7=BB=E5=8A=A0=E5=8E=9F=E8=A7=86?= =?UTF-8?q?=E9=A2=91=E6=97=8B=E8=BD=AC=E8=A7=92=E5=BA=A6=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E5=92=8C=E9=9F=B3=E9=A2=91=E8=BD=A8=E9=81=93=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 5 ++-- gradle/wrapper/gradle-wrapper.properties | 4 +-- .../videostudio/Util/MediaClipper.kt | 28 ++++++++++++++++++- 3 files changed, 31 insertions(+), 6 deletions(-) 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()