Java Code Examples for android.media.MediaExtractor#SAMPLE_FLAG_SYNC

The following examples show how to use android.media.MediaExtractor#SAMPLE_FLAG_SYNC . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AudioTrackTranscoder.java    From EZFilter with MIT License 6 votes vote down vote up
private int drainExtractor(long timeoutUs) {
    if (mIsExtractorEOS) return DRAIN_STATE_NONE;
    int trackIndex = mExtractor.getSampleTrackIndex();
    if (trackIndex >= 0 && trackIndex != mTrackIndex) {
        return DRAIN_STATE_NONE;
    }

    int result = mDecoder.dequeueInputBuffer(timeoutUs);
    if (result < 0) return DRAIN_STATE_NONE;
    if (trackIndex < 0) {
        mIsExtractorEOS = true;
        mDecoder.queueInputBuffer(result, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
        return DRAIN_STATE_NONE;
    }

    int sampleSize = mExtractor.readSampleData(CodecUtil.getInputBuffer(mDecoder, result), 0);
    boolean isKeyFrame = (mExtractor.getSampleFlags() & MediaExtractor.SAMPLE_FLAG_SYNC) != 0;
    mDecoder.queueInputBuffer(result, 0, sampleSize, mExtractor.getSampleTime(), isKeyFrame ? MediaCodec.BUFFER_FLAG_SYNC_FRAME : 0);
    mExtractor.advance();
    return DRAIN_STATE_CONSUMED;
}
 
Example 2
Source File: VideoTrackTranscoder.java    From android-transcoder with Apache License 2.0 6 votes vote down vote up
private int drainExtractor(long timeoutUs) {
    if (mIsExtractorEOS) return DRAIN_STATE_NONE;
    int trackIndex = mExtractor.getSampleTrackIndex();
    if (trackIndex >= 0 && trackIndex != mTrackIndex) {
        return DRAIN_STATE_NONE;
    }
    int result = mDecoder.dequeueInputBuffer(timeoutUs);
    if (result < 0) return DRAIN_STATE_NONE;
    if (trackIndex < 0) {
        mIsExtractorEOS = true;
        mDecoder.queueInputBuffer(result, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
        return DRAIN_STATE_NONE;
    }
    int sampleSize = mExtractor.readSampleData(mDecoderInputBuffers[result], 0);
    boolean isKeyFrame = (mExtractor.getSampleFlags() & MediaExtractor.SAMPLE_FLAG_SYNC) != 0;
    mDecoder.queueInputBuffer(result, 0, sampleSize, mExtractor.getSampleTime(), isKeyFrame ? MediaCodec.BUFFER_FLAG_SYNC_FRAME : 0);
    mExtractor.advance();
    return DRAIN_STATE_CONSUMED;
}
 
Example 3
Source File: AudioTrackTranscoder.java    From android-transcoder with Apache License 2.0 6 votes vote down vote up
private int drainExtractor(long timeoutUs) {
    if (mIsExtractorEOS) return DRAIN_STATE_NONE;
    int trackIndex = mExtractor.getSampleTrackIndex();
    if (trackIndex >= 0 && trackIndex != mTrackIndex) {
        return DRAIN_STATE_NONE;
    }

    final int result = mDecoder.dequeueInputBuffer(timeoutUs);
    if (result < 0) return DRAIN_STATE_NONE;
    if (trackIndex < 0) {
        mIsExtractorEOS = true;
        mDecoder.queueInputBuffer(result, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
        return DRAIN_STATE_NONE;
    }

    final int sampleSize = mExtractor.readSampleData(mDecoderBuffers.getInputBuffer(result), 0);
    final boolean isKeyFrame = (mExtractor.getSampleFlags() & MediaExtractor.SAMPLE_FLAG_SYNC) != 0;
    mDecoder.queueInputBuffer(result, 0, sampleSize, mExtractor.getSampleTime(), isKeyFrame ? MediaCodec.BUFFER_FLAG_SYNC_FRAME : 0);
    mExtractor.advance();
    return DRAIN_STATE_CONSUMED;
}
 
Example 4
Source File: VideoTrackTranscoder.java    From Pix-Art-Messenger with GNU General Public License v3.0 6 votes vote down vote up
private int drainExtractor(long timeoutUs) {
    if (mIsExtractorEOS) return DRAIN_STATE_NONE;
    int trackIndex = mExtractor.getSampleTrackIndex();
    if (trackIndex >= 0 && trackIndex != mTrackIndex) {
        return DRAIN_STATE_NONE;
    }
    int result = mDecoder.dequeueInputBuffer(timeoutUs);
    if (result < 0) return DRAIN_STATE_NONE;
    if (trackIndex < 0) {
        mIsExtractorEOS = true;
        mDecoder.queueInputBuffer(result, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
        return DRAIN_STATE_NONE;
    }
    int sampleSize = mExtractor.readSampleData(mDecoderInputBuffers[result], 0);
    boolean isKeyFrame = (mExtractor.getSampleFlags() & MediaExtractor.SAMPLE_FLAG_SYNC) != 0;
    mDecoder.queueInputBuffer(result, 0, sampleSize, mExtractor.getSampleTime(), isKeyFrame ? MediaCodec.BUFFER_FLAG_SYNC_FRAME : 0);
    mExtractor.advance();
    return DRAIN_STATE_CONSUMED;
}
 
Example 5
Source File: AudioTrackTranscoder.java    From Pix-Art-Messenger with GNU General Public License v3.0 6 votes vote down vote up
private int drainExtractor(long timeoutUs) {
    if (mIsExtractorEOS) return DRAIN_STATE_NONE;
    int trackIndex = mExtractor.getSampleTrackIndex();
    if (trackIndex >= 0 && trackIndex != mTrackIndex) {
        return DRAIN_STATE_NONE;
    }

    final int result = mDecoder.dequeueInputBuffer(timeoutUs);
    if (result < 0) return DRAIN_STATE_NONE;
    if (trackIndex < 0) {
        mIsExtractorEOS = true;
        mDecoder.queueInputBuffer(result, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
        return DRAIN_STATE_NONE;
    }

    final int sampleSize = mExtractor.readSampleData(mDecoderBuffers.getInputBuffer(result), 0);
    final boolean isKeyFrame = (mExtractor.getSampleFlags() & MediaExtractor.SAMPLE_FLAG_SYNC) != 0;
    mDecoder.queueInputBuffer(result, 0, sampleSize, mExtractor.getSampleTime(), isKeyFrame ? MediaCodec.BUFFER_FLAG_SYNC_FRAME : 0);
    mExtractor.advance();
    return DRAIN_STATE_CONSUMED;
}
 
Example 6
Source File: VideoTrackTranscoder.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private int drainExtractor(long timeoutUs) {
    if (mIsExtractorEOS) return DRAIN_STATE_NONE;
    int trackIndex = mExtractor.getSampleTrackIndex();
    if (trackIndex >= 0 && trackIndex != mTrackIndex) {
        return DRAIN_STATE_NONE;
    }
    int result = mDecoder.dequeueInputBuffer(timeoutUs);
    if (result < 0) return DRAIN_STATE_NONE;
    if (trackIndex < 0) {
        mIsExtractorEOS = true;
        mDecoder.queueInputBuffer(result, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
        return DRAIN_STATE_NONE;
    }
    int sampleSize = mExtractor.readSampleData(mDecoderInputBuffers[result], 0);
    boolean isKeyFrame = (mExtractor.getSampleFlags() & MediaExtractor.SAMPLE_FLAG_SYNC) != 0;
    mDecoder.queueInputBuffer(result, 0, sampleSize, mExtractor.getSampleTime(), isKeyFrame ? MediaCodec.BUFFER_FLAG_SYNC_FRAME : 0);
    mExtractor.advance();
    return DRAIN_STATE_CONSUMED;
}
 
Example 7
Source File: AudioTrackTranscoder.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private int drainExtractor(long timeoutUs) {
    if (mIsExtractorEOS) return DRAIN_STATE_NONE;
    int trackIndex = mExtractor.getSampleTrackIndex();
    if (trackIndex >= 0 && trackIndex != mTrackIndex) {
        return DRAIN_STATE_NONE;
    }

    final int result = mDecoder.dequeueInputBuffer(timeoutUs);
    if (result < 0) return DRAIN_STATE_NONE;
    if (trackIndex < 0) {
        mIsExtractorEOS = true;
        mDecoder.queueInputBuffer(result, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
        return DRAIN_STATE_NONE;
    }

    final int sampleSize = mExtractor.readSampleData(mDecoderBuffers.getInputBuffer(result), 0);
    final boolean isKeyFrame = (mExtractor.getSampleFlags() & MediaExtractor.SAMPLE_FLAG_SYNC) != 0;
    mDecoder.queueInputBuffer(result, 0, sampleSize, mExtractor.getSampleTime(), isKeyFrame ? MediaCodec.BUFFER_FLAG_SYNC_FRAME : 0);
    mExtractor.advance();
    return DRAIN_STATE_CONSUMED;
}
 
Example 8
Source File: VideoTrackTranscoder.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private int drainExtractor(long timeoutUs) {
    if (mIsExtractorEOS) return DRAIN_STATE_NONE;
    int trackIndex = mExtractor.getSampleTrackIndex();
    if (trackIndex >= 0 && trackIndex != mTrackIndex) {
        return DRAIN_STATE_NONE;
    }
    int result = mDecoder.dequeueInputBuffer(timeoutUs);
    if (result < 0) return DRAIN_STATE_NONE;
    if (trackIndex < 0) {
        mIsExtractorEOS = true;
        mDecoder.queueInputBuffer(result, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
        return DRAIN_STATE_NONE;
    }
    int sampleSize = mExtractor.readSampleData(mDecoderInputBuffers[result], 0);
    boolean isKeyFrame = (mExtractor.getSampleFlags() & MediaExtractor.SAMPLE_FLAG_SYNC) != 0;
    mDecoder.queueInputBuffer(result, 0, sampleSize, mExtractor.getSampleTime(), isKeyFrame ? MediaCodec.BUFFER_FLAG_SYNC_FRAME : 0);
    mExtractor.advance();
    return DRAIN_STATE_CONSUMED;
}
 
Example 9
Source File: AudioTrackTranscoder.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private int drainExtractor(long timeoutUs) {
    if (mIsExtractorEOS) return DRAIN_STATE_NONE;
    int trackIndex = mExtractor.getSampleTrackIndex();
    if (trackIndex >= 0 && trackIndex != mTrackIndex) {
        return DRAIN_STATE_NONE;
    }

    final int result = mDecoder.dequeueInputBuffer(timeoutUs);
    if (result < 0) return DRAIN_STATE_NONE;
    if (trackIndex < 0) {
        mIsExtractorEOS = true;
        mDecoder.queueInputBuffer(result, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
        return DRAIN_STATE_NONE;
    }

    final int sampleSize = mExtractor.readSampleData(mDecoderBuffers.getInputBuffer(result), 0);
    final boolean isKeyFrame = (mExtractor.getSampleFlags() & MediaExtractor.SAMPLE_FLAG_SYNC) != 0;
    mDecoder.queueInputBuffer(result, 0, sampleSize, mExtractor.getSampleTime(), isKeyFrame ? MediaCodec.BUFFER_FLAG_SYNC_FRAME : 0);
    mExtractor.advance();
    return DRAIN_STATE_CONSUMED;
}
 
Example 10
Source File: VideoTrackTranscoder.java    From EZFilter with MIT License 6 votes vote down vote up
private int drainExtractor() {
    if (mIsExtractorEOS) return DRAIN_STATE_NONE;
    int trackIndex = mExtractor.getSampleTrackIndex();
    if (trackIndex >= 0 && trackIndex != mTrackIndex) {
        return DRAIN_STATE_NONE;
    }

    int result = mDecoder.dequeueInputBuffer(0);
    if (result < 0) return DRAIN_STATE_NONE;
    if (trackIndex < 0) {
        mIsExtractorEOS = true;
        mDecoder.queueInputBuffer(result, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
        return DRAIN_STATE_NONE;
    }

    int sampleSize = mExtractor.readSampleData(CodecUtil.getInputBuffer(mDecoder, result), 0);
    boolean isKeyFrame = (mExtractor.getSampleFlags() & MediaExtractor.SAMPLE_FLAG_SYNC) != 0;
    mDecoder.queueInputBuffer(result, 0, sampleSize, mExtractor.getSampleTime(),
            isKeyFrame ? MediaCodec.BUFFER_FLAG_SYNC_FRAME : 0);
    mExtractor.advance();
    return DRAIN_STATE_CONSUMED;
}
 
Example 11
Source File: VideoComposer.java    From GPUVideo-android with MIT License 6 votes vote down vote up
private int drainExtractor() {
    if (isExtractorEOS) return DRAIN_STATE_NONE;
    int trackIndex = mediaExtractor.getSampleTrackIndex();
    if (trackIndex >= 0 && trackIndex != this.trackIndex) {
        return DRAIN_STATE_NONE;
    }
    int result = decoder.dequeueInputBuffer(0);
    if (result < 0) return DRAIN_STATE_NONE;
    if (trackIndex < 0) {
        isExtractorEOS = true;
        decoder.queueInputBuffer(result, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
        return DRAIN_STATE_NONE;
    }
    int sampleSize = mediaExtractor.readSampleData(decoderInputBuffers[result], 0);
    boolean isKeyFrame = (mediaExtractor.getSampleFlags() & MediaExtractor.SAMPLE_FLAG_SYNC) != 0;
    decoder.queueInputBuffer(result, 0, sampleSize, mediaExtractor.getSampleTime() / timeScale, isKeyFrame ? MediaCodec.BUFFER_FLAG_SYNC_FRAME : 0);
    mediaExtractor.advance();
    return DRAIN_STATE_CONSUMED;
}
 
Example 12
Source File: RemixAudioComposer.java    From Mp4Composer-android with MIT License 6 votes vote down vote up
private int drainExtractor(long timeoutUs) {
    if (isExtractorEOS) return DRAIN_STATE_NONE;
    int trackIndex = extractor.getSampleTrackIndex();
    if (trackIndex >= 0 && trackIndex != this.trackIndex) {
        return DRAIN_STATE_NONE;
    }

    final int result = decoder.dequeueInputBuffer(timeoutUs);
    if (result < 0) return DRAIN_STATE_NONE;
    if (trackIndex < 0) {
        isExtractorEOS = true;
        decoder.queueInputBuffer(result, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
        return DRAIN_STATE_NONE;
    }

    final int sampleSize = extractor.readSampleData(decoder.getInputBuffer(result), 0);
    final boolean isKeyFrame = (extractor.getSampleFlags() & MediaExtractor.SAMPLE_FLAG_SYNC) != 0;
    decoder.queueInputBuffer(result, 0, sampleSize, extractor.getSampleTime(), isKeyFrame ? MediaCodec.BUFFER_FLAG_KEY_FRAME : 0);
    extractor.advance();
    numTracks ++ ;
    return DRAIN_STATE_CONSUMED;
}
 
Example 13
Source File: VideoComposer.java    From Mp4Composer-android with MIT License 6 votes vote down vote up
private int drainExtractor() {
    if (isExtractorEOS) return DRAIN_STATE_NONE;
    int trackIndex = mediaExtractor.getSampleTrackIndex();
    if (trackIndex >= 0 && trackIndex != this.trackIndex) {
        return DRAIN_STATE_NONE;
    }
    int result = decoder.dequeueInputBuffer(0);
    if (result < 0) return DRAIN_STATE_NONE;
    if (trackIndex < 0) {
        isExtractorEOS = true;
        decoder.queueInputBuffer(result, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
        return DRAIN_STATE_NONE;
    }
    int sampleSizeCompat = mediaExtractor.readSampleData(decoder.getInputBuffer(result), 0);
    boolean isKeyFrame = (mediaExtractor.getSampleFlags() & MediaExtractor.SAMPLE_FLAG_SYNC) != 0;
    decoder.queueInputBuffer(result, 0, sampleSizeCompat, (long)(mediaExtractor.getSampleTime() / timeScale), isKeyFrame ? MediaCodec.BUFFER_FLAG_KEY_FRAME : 0);
    mediaExtractor.advance();
    return DRAIN_STATE_CONSUMED;
}
 
Example 14
Source File: RemixAudioComposer.java    From GPUVideo-android with MIT License 6 votes vote down vote up
private int drainExtractor(long timeoutUs) {
    if (isExtractorEOS) return DRAIN_STATE_NONE;
    int trackIndex = extractor.getSampleTrackIndex();
    if (trackIndex >= 0 && trackIndex != this.trackIndex) {
        return DRAIN_STATE_NONE;
    }

    final int result = decoder.dequeueInputBuffer(timeoutUs);
    if (result < 0) return DRAIN_STATE_NONE;
    if (trackIndex < 0) {
        isExtractorEOS = true;
        decoder.queueInputBuffer(result, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
        return DRAIN_STATE_NONE;
    }

    final int sampleSize = extractor.readSampleData(decoderBuffers.getInputBuffer(result), 0);
    final boolean isKeyFrame = (extractor.getSampleFlags() & MediaExtractor.SAMPLE_FLAG_SYNC) != 0;
    decoder.queueInputBuffer(result, 0, sampleSize, extractor.getSampleTime(), isKeyFrame ? MediaCodec.BUFFER_FLAG_SYNC_FRAME : 0);
    extractor.advance();
    return DRAIN_STATE_CONSUMED;
}
 
Example 15
Source File: VideoUtil.java    From VideoProcessor with Apache License 2.0 5 votes vote down vote up
public static Pair<Integer, Integer> getVideoFrameCount(String input) throws IOException {
    MediaExtractor extractor = new MediaExtractor();
    extractor.setDataSource(input);
    int trackIndex = VideoUtil.selectTrack(extractor, false);
    extractor.selectTrack(trackIndex);
    int keyFrameCount = 0;
    int frameCount = 0;
    while (true) {
        int flags = extractor.getSampleFlags();
        if (flags > 0 && (flags & MediaExtractor.SAMPLE_FLAG_SYNC) != 0) {
            keyFrameCount++;
        }
        long sampleTime = extractor.getSampleTime();
        if (sampleTime < 0) {
            break;
        }
        frameCount++;
        extractor.advance();
    }
    extractor.release();
    return new Pair<>(keyFrameCount, frameCount);
}
 
Example 16
Source File: VideoUtil.java    From VideoProcessor with Apache License 2.0 5 votes vote down vote up
/**
 * 用于制作全关键帧视频时计算比特率应该为多少
 *
 * @return
 */
public static int getBitrateForAllKeyFrameVideo(VideoProcessor.MediaSource input) throws IOException {
    MediaExtractor extractor = new MediaExtractor();
    input.setDataSource(extractor);
    int trackIndex = VideoUtil.selectTrack(extractor, false);
    extractor.selectTrack(trackIndex);
    int keyFrameCount = 0;
    int frameCount = 0;
    while (true) {
        int flags = extractor.getSampleFlags();
        if (flags > 0 && (flags & MediaExtractor.SAMPLE_FLAG_SYNC) != 0) {
            keyFrameCount++;
        }
        long sampleTime = extractor.getSampleTime();
        if (sampleTime < 0) {
            break;
        }
        frameCount++;
        extractor.advance();
    }
    extractor.release();
    float bitrateMultiple = (frameCount - keyFrameCount) / (float) keyFrameCount + 1;
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    input.setDataSource(retriever);
    int oriBitrate = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE));
    retriever.release();
    if (frameCount == keyFrameCount) {
        return oriBitrate;
    }
    return (int) (bitrateMultiple * oriBitrate);
}
 
Example 17
Source File: VideoBgmAddAction.java    From SimpleVideoEditor with Apache License 2.0 5 votes vote down vote up
private long getSampleTime(MediaExtractor mediaExtractor, int track) {
    mediaExtractor.selectTrack(track);
    mediaExtractor.readSampleData(sourceBuffer, 0);
    //skip first I frame
    if (mediaExtractor.getSampleFlags() == MediaExtractor.SAMPLE_FLAG_SYNC) {
        mediaExtractor.advance();
    }
    mediaExtractor.readSampleData(sourceBuffer, 0);
    long firstVideoPTS = mediaExtractor.getSampleTime();
    mediaExtractor.advance();
    mediaExtractor.readSampleData(sourceBuffer, 0);
    long secondVideoPTS = mediaExtractor.getSampleTime();
    mediaExtractor.unselectTrack(track);
    return Math.abs(secondVideoPTS - firstVideoPTS);
}
 
Example 18
Source File: AudioComposer.java    From Mp4Composer-android with MIT License 5 votes vote down vote up
@SuppressLint("Assert")
public boolean stepPipeline() {
    if (isEOS) return false;
    int trackIndex = mediaExtractor.getSampleTrackIndex();
    if (trackIndex < 0) {
        buffer.clear();
        bufferInfo.set(0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
        muxRender.writeSampleData(sampleType, buffer, bufferInfo);
        isEOS = true;
        return true;
    }
    if (trackIndex != this.trackIndex) return false;

    buffer.clear();
    int sampleSize = mediaExtractor.readSampleData(buffer, 0);
    if (sampleSize > bufferSize) {
        logger.warning(TAG, "Sample size smaller than buffer size, resizing buffer: " + sampleSize);
        bufferSize = 2 * sampleSize;
        buffer = ByteBuffer.allocateDirect(bufferSize).order(ByteOrder.nativeOrder());
    }
    boolean isKeyFrame = (mediaExtractor.getSampleFlags() & MediaExtractor.SAMPLE_FLAG_SYNC) != 0;
    int flags = isKeyFrame ? MediaCodec.BUFFER_FLAG_KEY_FRAME : 0;

    if (mediaExtractor.getSampleTime() >= trimStartUs && (mediaExtractor.getSampleTime() <= trimEndUs || trimEndUs == -1)) {
        bufferInfo.set(0, sampleSize, mediaExtractor.getSampleTime(), flags);
        muxRender.writeSampleData(sampleType, buffer, bufferInfo);
    }

    writtenPresentationTimeUs = mediaExtractor.getSampleTime();
    mediaExtractor.advance();
    return true;
}
 
Example 19
Source File: VideoProcessor.java    From VideoProcessor with Apache License 2.0 4 votes vote down vote up
/**
 * 对视频先检查,如果不是全关键帧,先处理成所有帧都是关键帧,再逆序
 */
public static void reverseVideo(Context context, MediaSource input, String output, boolean reverseAudio, @Nullable VideoProgressListener listener) throws Exception {
    File tempFile = new File(context.getCacheDir(), System.currentTimeMillis() + ".temp");
    File temp2File = new File(context.getCacheDir(), System.currentTimeMillis() + ".temp2");
    try {
        MediaExtractor extractor = new MediaExtractor();
        input.setDataSource(extractor);
        int trackIndex = VideoUtil.selectTrack(extractor, false);
        extractor.selectTrack(trackIndex);
        int keyFrameCount = 0;
        int frameCount = 0;
        List<Long> frameTimeStamps = new ArrayList<>();
        while (true) {
            int flags = extractor.getSampleFlags();
            if (flags > 0 && (flags & MediaExtractor.SAMPLE_FLAG_SYNC) != 0) {
                keyFrameCount++;
            }
            long sampleTime = extractor.getSampleTime();
            if (sampleTime < 0) {
                break;
            }
            frameTimeStamps.add(sampleTime);
            frameCount++;
            extractor.advance();
        }
        extractor.release();

        if (frameCount == keyFrameCount || frameCount == keyFrameCount + 1) {
            reverseVideoNoDecode(input, output, reverseAudio, frameTimeStamps, listener);
        } else {
            VideoMultiStepProgress stepProgress = new VideoMultiStepProgress(new float[]{0.45f, 0.1f, 0.45f}, listener);
            stepProgress.setCurrentStep(0);
            float bitrateMultiple = (frameCount - keyFrameCount) / (float) keyFrameCount + 1;
            MediaMetadataRetriever retriever = new MediaMetadataRetriever();
            input.setDataSource(retriever);
            int oriBitrate = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE));
            int duration = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
            try {
                processor(context)
                        .input(input)
                        .output(tempFile.getAbsolutePath())
                        .bitrate((int) (oriBitrate * bitrateMultiple))
                        .iFrameInterval(0)
                        .progressListener(stepProgress)
                        .process();
            } catch (MediaCodec.CodecException e) {
                CL.e(e);
                /** Nexus5上-1代表全关键帧*/
                processor(context)
                        .input(input)
                        .output(tempFile.getAbsolutePath())
                        .bitrate((int) (oriBitrate * bitrateMultiple))
                        .iFrameInterval(-1)
                        .progressListener(stepProgress)
                        .process();
            }
            stepProgress.setCurrentStep(1);
            reverseVideoNoDecode(new MediaSource(tempFile.getAbsolutePath()), temp2File.getAbsolutePath(), reverseAudio, null, stepProgress);
            int oriIFrameInterval = (int) (keyFrameCount / (duration / 1000f));
            oriIFrameInterval = oriIFrameInterval == 0 ? 1 : oriIFrameInterval;
            stepProgress.setCurrentStep(2);
            processor(context)
                    .input(temp2File.getAbsolutePath())
                    .output(output)
                    .bitrate(oriBitrate)
                    .iFrameInterval(oriIFrameInterval)
                    .progressListener(stepProgress)
                    .process();
        }
    } finally {
        tempFile.delete();
        temp2File.delete();
    }
}
 
Example 20
Source File: PassthroughTranscoder.java    From LiTr with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public int processNextFrame() {
    if (lastResult == RESULT_EOS_REACHED) {
        // we are done
        return lastResult;
    }

    // TranscoderJob expects the first result to be RESULT_OUTPUT_MEDIA_FORMAT_CHANGED, so that it can start the mediaMuxer
    if (!targetTrackAdded) {
        targetFormat = mediaSource.getTrackFormat(sourceTrack);
        if (duration > 0) {
            targetFormat.setLong(MediaFormat.KEY_DURATION, duration);
        }

        targetTrack = mediaMuxer.addTrack(targetFormat, targetTrack);
        targetTrackAdded = true;

        int bufferSize = targetFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
        outputBuffer = ByteBuffer.allocate(bufferSize);

        lastResult = RESULT_OUTPUT_MEDIA_FORMAT_CHANGED;
        return lastResult;
    }

    int selectedTrack = mediaSource.getSampleTrackIndex();
    if (selectedTrack != NO_SELECTED_TRACK && selectedTrack != sourceTrack) {
        lastResult = RESULT_FRAME_PROCESSED;
        return lastResult;
    }

    lastResult = RESULT_FRAME_PROCESSED;

    int bytesRead = mediaSource.readSampleData(outputBuffer, 0);
    if (bytesRead > 0) {
        int outputFlags = 0;
        long sampleTime = mediaSource.getSampleTime();
        int inputFlags = mediaSource.getSampleFlags();

        if ((inputFlags & MediaExtractor.SAMPLE_FLAG_SYNC) != 0) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                outputFlags = MediaCodec.BUFFER_FLAG_KEY_FRAME;
            } else {
                outputFlags = MediaCodec.BUFFER_FLAG_SYNC_FRAME;
            }
        }
        if (duration > 0) {
            progress = ((float) sampleTime) / duration;
        }
        outputBufferInfo.set(0, bytesRead, sampleTime, outputFlags);
        mediaMuxer.writeSampleData(targetTrack, outputBuffer, outputBufferInfo);
        mediaSource.advance();
    } else {
        outputBuffer.clear();
        progress = 1.0f;
        lastResult = RESULT_EOS_REACHED;
        Log.d(TAG, "Reach EoS on input stream");
    }

    return lastResult;
}