Java Code Examples for android.media.MediaExtractor#seekTo()
The following examples show how to use
android.media.MediaExtractor#seekTo() .
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: AudioComposer.java From Mp4Composer-android with MIT License | 6 votes |
AudioComposer(@NonNull MediaExtractor mediaExtractor, int trackIndex, @NonNull MuxRender muxRender, long trimStartMs, long trimEndMs, @NonNull Logger logger) { this.mediaExtractor = mediaExtractor; this.trackIndex = trackIndex; this.muxRender = muxRender; this.trimStartUs = TimeUnit.MILLISECONDS.toMicros(trimStartMs); this.trimEndUs = trimEndMs == -1 ? trimEndMs : TimeUnit.MILLISECONDS.toMicros(trimEndMs); this.logger = logger; final MediaFormat actualOutputFormat = this.mediaExtractor.getTrackFormat(this.trackIndex); this.muxRender.setOutputFormat(this.sampleType, actualOutputFormat); bufferSize = actualOutputFormat.containsKey(MediaFormat.KEY_MAX_INPUT_SIZE) ? actualOutputFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE) : (64 * 1024); buffer = ByteBuffer.allocateDirect(bufferSize).order(ByteOrder.nativeOrder()); mediaExtractor.seekTo(trimStartUs, MediaExtractor.SEEK_TO_PREVIOUS_SYNC); }
Example 2
Source File: VideoUtil.java From VideoProcessor with Apache License 2.0 | 5 votes |
public static void seekToLastFrame(MediaExtractor extractor, int trackIndex, int durationMs) { int seekToDuration = durationMs * 1000; if (extractor.getSampleTrackIndex() != trackIndex) { extractor.selectTrack(trackIndex); } extractor.seekTo(seekToDuration, MediaExtractor.SEEK_TO_PREVIOUS_SYNC); while (seekToDuration > 0 && extractor.getSampleTrackIndex() != trackIndex) { seekToDuration -= 10000; extractor.seekTo(seekToDuration, MediaExtractor.SEEK_TO_PREVIOUS_SYNC); } }
Example 3
Source File: VideoThumbnailsExtractor.java From mollyim-android with GNU General Public License v3.0 | 4 votes |
private static void doExtract(final @NonNull MediaExtractor extractor, final @NonNull MediaCodec decoder, final @NonNull OutputSurface outputSurface, final int outputWidth, int outputHeight, long duration, int thumbnailCount, final @NonNull Callback callback) throws TranscodingException { final int TIMEOUT_USEC = 10000; final ByteBuffer[] decoderInputBuffers = decoder.getInputBuffers(); final MediaCodec.BufferInfo info = new MediaCodec.BufferInfo(); int samplesExtracted = 0; int thumbnailsCreated = 0; Log.i(TAG, "doExtract started"); final ByteBuffer pixelBuf = ByteBuffer.allocateDirect(outputWidth * outputHeight * 4); pixelBuf.order(ByteOrder.LITTLE_ENDIAN); boolean outputDone = false; boolean inputDone = false; while (!outputDone) { if (!inputDone) { int inputBufIndex = decoder.dequeueInputBuffer(TIMEOUT_USEC); if (inputBufIndex >= 0) { final ByteBuffer inputBuf = decoderInputBuffers[inputBufIndex]; final int sampleSize = extractor.readSampleData(inputBuf, 0); if (sampleSize < 0 || samplesExtracted >= thumbnailCount) { decoder.queueInputBuffer(inputBufIndex, 0, 0, 0L, MediaCodec.BUFFER_FLAG_END_OF_STREAM); inputDone = true; Log.i(TAG, "input done"); } else { final long presentationTimeUs = extractor.getSampleTime(); decoder.queueInputBuffer(inputBufIndex, 0, sampleSize, presentationTimeUs, 0 /*flags*/); samplesExtracted++; extractor.seekTo(duration * samplesExtracted / thumbnailCount, MediaExtractor.SEEK_TO_CLOSEST_SYNC); Log.i(TAG, "seek to " + duration * samplesExtracted / thumbnailCount + ", actual " + extractor.getSampleTime()); } } } int outputBufIndex = decoder.dequeueOutputBuffer(info, TIMEOUT_USEC); if (outputBufIndex >= 0) { if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) { outputDone = true; } final boolean shouldRender = (info.size != 0) /*&& (info.presentationTimeUs >= duration * decodeCount / thumbnailCount)*/; decoder.releaseOutputBuffer(outputBufIndex, shouldRender); if (shouldRender) { outputSurface.awaitNewImage(); outputSurface.drawImage(); if (thumbnailsCreated < thumbnailCount) { pixelBuf.rewind(); GLES20.glReadPixels(0, 0, outputWidth, outputHeight, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, pixelBuf); final Bitmap bitmap = Bitmap.createBitmap(outputWidth, outputHeight, Bitmap.Config.ARGB_8888); pixelBuf.rewind(); bitmap.copyPixelsFromBuffer(pixelBuf); if (!callback.publishProgress(thumbnailsCreated, bitmap)) { break; } Log.i(TAG, "publishProgress for frame " + thumbnailsCreated + " at " + info.presentationTimeUs + " (target " + duration * thumbnailsCreated / thumbnailCount + ")"); } thumbnailsCreated++; } } } Log.i(TAG, "doExtract finished"); }
Example 4
Source File: MediaPlayerGrabber.java From VIA-AI with MIT License | 4 votes |
private void decodeFrames(final MediaCodec decoder, MediaExtractor extractor, MediaFormat mediaFormat) { boolean sawInputEOS = false; sawOutputEOS = false; decoder.configure(mediaFormat, null, null, 0); decoder.start(); if(!Helper.isUpperThanAPI21()) { inputByteBuffers = decoder.getInputBuffers(); outputByteBuffers = decoder.getOutputBuffers(); } while (!sawOutputEOS && !stopDecode) { if (!sawInputEOS) { int inputBufferId = decoder.dequeueInputBuffer(DEFAULT_TIMEOUT_US); if (inputBufferId >= 0) { ByteBuffer inputBuffer = null; if (Helper.isUpperThanAPI21()) { inputBuffer = decoder.getInputBuffer(inputBufferId); } else { inputBuffer = inputByteBuffers[inputBufferId]; } int sampleSize = extractor.readSampleData(inputBuffer, 0); if (sampleSize < 0) { decoder.queueInputBuffer(inputBufferId, 0, 0, 0L, 0); sawInputEOS = false; extractor.seekTo(0, MediaExtractor.SEEK_TO_PREVIOUS_SYNC); } else { long presentationTimeUs = extractor.getSampleTime(); decoder.queueInputBuffer(inputBufferId, 0, sampleSize, presentationTimeUs, 0); extractor.advance(); } } } if(displayThread==null) { displayThread = new Thread(new Runnable() { @Override public void run() { while (!sawOutputEOS && !stopDisplay) { frameDisplay(decoder); } } }); displayThread.start(); } } }
Example 5
Source File: DownloadRedditVideoService.java From Infinity-For-Reddit with GNU Affero General Public License v3.0 | 4 votes |
private boolean muxVideoAndAudio(String videoFilePath, String audioFilePath, String outputFilePath) { try { File file = new File(outputFilePath); file.createNewFile(); MediaExtractor videoExtractor = new MediaExtractor(); videoExtractor.setDataSource(videoFilePath); MediaExtractor audioExtractor = new MediaExtractor(); audioExtractor.setDataSource(audioFilePath); MediaMuxer muxer = new MediaMuxer(outputFilePath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); videoExtractor.selectTrack(0); MediaFormat videoFormat = videoExtractor.getTrackFormat(0); int videoTrack = muxer.addTrack(videoFormat); audioExtractor.selectTrack(0); MediaFormat audioFormat = audioExtractor.getTrackFormat(0); int audioTrack = muxer.addTrack(audioFormat); boolean sawEOS = false; int offset = 100; int sampleSize = 2048 * 1024; ByteBuffer videoBuf = ByteBuffer.allocate(sampleSize); ByteBuffer audioBuf = ByteBuffer.allocate(sampleSize); MediaCodec.BufferInfo videoBufferInfo = new MediaCodec.BufferInfo(); MediaCodec.BufferInfo audioBufferInfo = new MediaCodec.BufferInfo(); videoExtractor.seekTo(0, MediaExtractor.SEEK_TO_CLOSEST_SYNC); audioExtractor.seekTo(0, MediaExtractor.SEEK_TO_CLOSEST_SYNC); muxer.start(); while (!sawEOS) { videoBufferInfo.offset = offset; videoBufferInfo.size = videoExtractor.readSampleData(videoBuf, offset); if (videoBufferInfo.size < 0 || audioBufferInfo.size < 0) { sawEOS = true; videoBufferInfo.size = 0; } else { videoBufferInfo.presentationTimeUs = videoExtractor.getSampleTime(); videoBufferInfo.flags = videoExtractor.getSampleFlags(); muxer.writeSampleData(videoTrack, videoBuf, videoBufferInfo); videoExtractor.advance(); } } boolean sawEOS2 = false; while (!sawEOS2) { audioBufferInfo.offset = offset; audioBufferInfo.size = audioExtractor.readSampleData(audioBuf, offset); if (videoBufferInfo.size < 0 || audioBufferInfo.size < 0) { sawEOS2 = true; audioBufferInfo.size = 0; } else { audioBufferInfo.presentationTimeUs = audioExtractor.getSampleTime(); audioBufferInfo.flags = audioExtractor.getSampleFlags(); muxer.writeSampleData(audioTrack, audioBuf, audioBufferInfo); audioExtractor.advance(); } } try { muxer.stop(); muxer.release(); } catch (IllegalStateException ignore) {} } catch (IOException e) { e.printStackTrace(); return false; } return true; }
Example 6
Source File: VideoController.java From VideoCompressor with Apache License 2.0 | 4 votes |
@TargetApi(16) private long readAndWriteTrack(MediaExtractor extractor, MP4Builder mediaMuxer, MediaCodec.BufferInfo info, long start, long end, File file, boolean isAudio) throws Exception { int trackIndex = selectTrack(extractor, isAudio); if (trackIndex >= 0) { extractor.selectTrack(trackIndex); MediaFormat trackFormat = extractor.getTrackFormat(trackIndex); int muxerTrackIndex = mediaMuxer.addTrack(trackFormat, isAudio); int maxBufferSize = trackFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE); boolean inputDone = false; if (start > 0) { extractor.seekTo(start, MediaExtractor.SEEK_TO_PREVIOUS_SYNC); } else { extractor.seekTo(0, MediaExtractor.SEEK_TO_PREVIOUS_SYNC); } ByteBuffer buffer = ByteBuffer.allocateDirect(maxBufferSize); long startTime = -1; while (!inputDone) { boolean eof = false; int index = extractor.getSampleTrackIndex(); if (index == trackIndex) { info.size = extractor.readSampleData(buffer, 0); if (info.size < 0) { info.size = 0; eof = true; } else { info.presentationTimeUs = extractor.getSampleTime(); if (start > 0 && startTime == -1) { startTime = info.presentationTimeUs; } if (end < 0 || info.presentationTimeUs < end) { info.offset = 0; info.flags = extractor.getSampleFlags(); if (mediaMuxer.writeSampleData(muxerTrackIndex, buffer, info, isAudio)) { // didWriteData(messageObject, file, false, false); } extractor.advance(); } else { eof = true; } } } else if (index == -1) { eof = true; } if (eof) { inputDone = true; } } extractor.unselectTrack(trackIndex); return startTime; } return -1; }
Example 7
Source File: MediaController.java From react-native-video-helper with MIT License | 4 votes |
@TargetApi(16) private long readAndWriteTrack(MediaExtractor extractor, MP4Builder mediaMuxer, MediaCodec.BufferInfo info, long start, long end, File file, boolean isAudio) throws Exception { int trackIndex = selectTrack(extractor, isAudio); if (trackIndex >= 0) { extractor.selectTrack(trackIndex); MediaFormat trackFormat = extractor.getTrackFormat(trackIndex); int muxerTrackIndex = mediaMuxer.addTrack(trackFormat, isAudio); int maxBufferSize = trackFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE); boolean inputDone = false; if (start > 0) { extractor.seekTo(start, MediaExtractor.SEEK_TO_PREVIOUS_SYNC); } else { extractor.seekTo(0, MediaExtractor.SEEK_TO_PREVIOUS_SYNC); } ByteBuffer buffer = ByteBuffer.allocateDirect(maxBufferSize); long startTime = -1; while (!inputDone) { boolean eof = false; int index = extractor.getSampleTrackIndex(); if (index == trackIndex) { info.size = extractor.readSampleData(buffer, 0); if (info.size < 0) { info.size = 0; eof = true; } else { info.presentationTimeUs = extractor.getSampleTime(); if (start > 0 && startTime == -1) { startTime = info.presentationTimeUs; } if (end < 0 || info.presentationTimeUs < end) { info.offset = 0; info.flags = extractor.getSampleFlags(); if (mediaMuxer.writeSampleData(muxerTrackIndex, buffer, info, isAudio)) { // didWriteData(messageObject, file, false, false); } extractor.advance(); } else { eof = true; } } } else if (index == -1) { eof = true; } if (eof) { inputDone = true; } } extractor.unselectTrack(trackIndex); return startTime; } return -1; }
Example 8
Source File: VideoUtil.java From VideoProcessor with Apache License 2.0 | 4 votes |
static long appendVideoTrack(MediaExtractor extractor, MediaMuxer mediaMuxer, int muxerVideoTrackIndex, Integer startTimeUs, Integer endTimeUs, long baseMuxerFrameTimeUs, int bitrate, int iFrameInterval, boolean isFirst, boolean isLast) throws Exception { int videoTrack = selectTrack(extractor, false); extractor.selectTrack(videoTrack); if (startTimeUs == null) { startTimeUs = 0; } extractor.seekTo(startTimeUs, MediaExtractor.SEEK_TO_CLOSEST_SYNC); MediaFormat videoFormat = extractor.getTrackFormat(videoTrack); //初始化编码器 int resultWidth = videoFormat.getInteger(MediaFormat.KEY_WIDTH); int resultHeight = videoFormat.getInteger(MediaFormat.KEY_HEIGHT); AtomicBoolean decodeDone = new AtomicBoolean(false); VideoAppendEncodeThread encodeThread = new VideoAppendEncodeThread(extractor, mediaMuxer, bitrate, resultWidth, resultHeight, iFrameInterval, videoTrack, decodeDone, baseMuxerFrameTimeUs, isFirst, isLast, muxerVideoTrackIndex); VideoDecodeThread decodeThread = new VideoDecodeThread(encodeThread, extractor, startTimeUs == null ? null : startTimeUs / 1000, endTimeUs == null ? null : endTimeUs / 1000, null, null, null, false,videoTrack, decodeDone); decodeThread.start(); encodeThread.start(); try { decodeThread.join(); encodeThread.join(); } catch (InterruptedException e) { e.printStackTrace(); } try { extractor.release(); } catch (Exception e2) { CL.e(e2); } if (encodeThread.getException() != null) { throw encodeThread.getException(); } else if (decodeThread.getException() != null) { throw decodeThread.getException(); } return encodeThread.getLastFrametimeUs(); }
Example 9
Source File: AudioUtil.java From VideoProcessor with Apache License 2.0 | 4 votes |
/** * 需要改变音频速率的情况下,需要先解码->改变速率->编码 */ public static void decodeToPCM(VideoProcessor.MediaSource audioSource, String outPath, Integer startTimeUs, Integer endTimeUs) throws IOException { MediaExtractor extractor = new MediaExtractor(); audioSource.setDataSource(extractor); int audioTrack = VideoUtil.selectTrack(extractor, true); extractor.selectTrack(audioTrack); if (startTimeUs == null) { startTimeUs = 0; } extractor.seekTo(startTimeUs, MediaExtractor.SEEK_TO_CLOSEST_SYNC); MediaFormat oriAudioFormat = extractor.getTrackFormat(audioTrack); int maxBufferSize; if (oriAudioFormat.containsKey(MediaFormat.KEY_MAX_INPUT_SIZE)) { maxBufferSize = oriAudioFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE); } else { maxBufferSize = 100 * 1000; } ByteBuffer buffer = ByteBuffer.allocateDirect(maxBufferSize); MediaCodec.BufferInfo info = new MediaCodec.BufferInfo(); //调整音频速率需要重解码音频帧 MediaCodec decoder = MediaCodec.createDecoderByType(oriAudioFormat.getString(MediaFormat.KEY_MIME)); decoder.configure(oriAudioFormat, null, null, 0); decoder.start(); boolean decodeDone = false; boolean decodeInputDone = false; final int TIMEOUT_US = 2500; File pcmFile = new File(outPath); FileChannel writeChannel = new FileOutputStream(pcmFile).getChannel(); try { while (!decodeDone) { if (!decodeInputDone) { boolean eof = false; int decodeInputIndex = decoder.dequeueInputBuffer(TIMEOUT_US); if (decodeInputIndex >= 0) { long sampleTimeUs = extractor.getSampleTime(); if (sampleTimeUs == -1) { eof = true; } else if (sampleTimeUs < startTimeUs) { extractor.advance(); continue; } else if (endTimeUs != null && sampleTimeUs > endTimeUs) { eof = true; } if (eof) { decodeInputDone = true; decoder.queueInputBuffer(decodeInputIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM); } else { info.size = extractor.readSampleData(buffer, 0); info.presentationTimeUs = sampleTimeUs; info.flags = extractor.getSampleFlags(); ByteBuffer inputBuffer = decoder.getInputBuffer(decodeInputIndex); inputBuffer.put(buffer); CL.it(TAG, "audio decode queueInputBuffer " + info.presentationTimeUs / 1000); decoder.queueInputBuffer(decodeInputIndex, 0, info.size, info.presentationTimeUs, info.flags); extractor.advance(); } } } while (!decodeDone) { int outputBufferIndex = decoder.dequeueOutputBuffer(info, TIMEOUT_US); if (outputBufferIndex == MediaCodec.INFO_TRY_AGAIN_LATER) { break; } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) { MediaFormat newFormat = decoder.getOutputFormat(); CL.it(TAG, "audio decode newFormat = " + newFormat); } else if (outputBufferIndex < 0) { //ignore CL.et(TAG, "unexpected result from audio decoder.dequeueOutputBuffer: " + outputBufferIndex); } else { if (info.flags == MediaCodec.BUFFER_FLAG_END_OF_STREAM) { decodeDone = true; } else { ByteBuffer decodeOutputBuffer = decoder.getOutputBuffer(outputBufferIndex); CL.it(TAG, "audio decode saveFrame " + info.presentationTimeUs / 1000); writeChannel.write(decodeOutputBuffer); } decoder.releaseOutputBuffer(outputBufferIndex, false); } } } } finally { writeChannel.close(); extractor.release(); decoder.stop(); decoder.release(); } }
Example 10
Source File: AudioUtil.java From VideoProcessor with Apache License 2.0 | 4 votes |
/** * 不需要改变音频速率的情况下,直接读写就可 */ public static long writeAudioTrack(MediaExtractor extractor, MediaMuxer mediaMuxer, int muxerAudioTrackIndex, Integer startTimeUs, Integer endTimeUs, long baseMuxerFrameTimeUs, VideoProgressListener listener) throws IOException { int audioTrack = VideoUtil.selectTrack(extractor, true); extractor.selectTrack(audioTrack); if (startTimeUs == null) { startTimeUs = 0; } extractor.seekTo(startTimeUs, MediaExtractor.SEEK_TO_CLOSEST_SYNC); MediaFormat audioFormat = extractor.getTrackFormat(audioTrack); long durationUs = audioFormat.getLong(MediaFormat.KEY_DURATION); int maxBufferSize = audioFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE); ByteBuffer buffer = ByteBuffer.allocateDirect(maxBufferSize); MediaCodec.BufferInfo info = new MediaCodec.BufferInfo(); long lastFrametimeUs = baseMuxerFrameTimeUs; while (true) { long sampleTimeUs = extractor.getSampleTime(); if (sampleTimeUs == -1) { break; } if (sampleTimeUs < startTimeUs) { extractor.advance(); continue; } if (endTimeUs != null && sampleTimeUs > endTimeUs) { break; } if (listener != null) { float progress = (sampleTimeUs - startTimeUs) / (float) (endTimeUs == null ? durationUs : endTimeUs - startTimeUs); progress = progress < 0 ? 0 : progress; progress = progress > 1 ? 1 : progress; listener.onProgress(progress); } info.presentationTimeUs = sampleTimeUs - startTimeUs + baseMuxerFrameTimeUs; info.flags = extractor.getSampleFlags(); info.size = extractor.readSampleData(buffer, 0); if (info.size < 0) { break; } CL.i("writeAudioSampleData,time:" + info.presentationTimeUs / 1000f); mediaMuxer.writeSampleData(muxerAudioTrackIndex, buffer, info); lastFrametimeUs = info.presentationTimeUs; extractor.advance(); } return lastFrametimeUs; }
Example 11
Source File: VideoController.java From VideoCompressor with Apache License 2.0 | 4 votes |
@TargetApi(16) private long readAndWriteTrack(MediaExtractor extractor, MP4Builder mediaMuxer, MediaCodec.BufferInfo info, long start, long end, File file, boolean isAudio) throws Exception { int trackIndex = selectTrack(extractor, isAudio); if (trackIndex >= 0) { extractor.selectTrack(trackIndex); MediaFormat trackFormat = extractor.getTrackFormat(trackIndex); int muxerTrackIndex = mediaMuxer.addTrack(trackFormat, isAudio); int maxBufferSize = trackFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE); boolean inputDone = false; if (start > 0) { extractor.seekTo(start, MediaExtractor.SEEK_TO_PREVIOUS_SYNC); } else { extractor.seekTo(0, MediaExtractor.SEEK_TO_PREVIOUS_SYNC); } ByteBuffer buffer = ByteBuffer.allocateDirect(maxBufferSize); long startTime = -1; while (!inputDone) { boolean eof = false; int index = extractor.getSampleTrackIndex(); if (index == trackIndex) { info.size = extractor.readSampleData(buffer, 0); if (info.size < 0) { info.size = 0; eof = true; } else { info.presentationTimeUs = extractor.getSampleTime(); if (start > 0 && startTime == -1) { startTime = info.presentationTimeUs; } if (end < 0 || info.presentationTimeUs < end) { info.offset = 0; info.flags = extractor.getSampleFlags(); if (mediaMuxer.writeSampleData(muxerTrackIndex, buffer, info, isAudio)) { // didWriteData(messageObject, file, false, false); } extractor.advance(); } else { eof = true; } } } else if (index == -1) { eof = true; } if (eof) { inputDone = true; } } extractor.unselectTrack(trackIndex); return startTime; } return -1; }
Example 12
Source File: MediaController.java From talk-android with MIT License | 4 votes |
@TargetApi(16) private long readAndWriteTrack(MediaExtractor extractor, MP4Builder mediaMuxer, MediaCodec.BufferInfo info, long start, long end, File file, boolean isAudio) throws Exception { int trackIndex = selectTrack(extractor, isAudio); if (trackIndex >= 0) { extractor.selectTrack(trackIndex); MediaFormat trackFormat = extractor.getTrackFormat(trackIndex); int muxerTrackIndex = mediaMuxer.addTrack(trackFormat, isAudio); int maxBufferSize = trackFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE); boolean inputDone = false; if (start > 0) { extractor.seekTo(start, MediaExtractor.SEEK_TO_PREVIOUS_SYNC); } else { extractor.seekTo(0, MediaExtractor.SEEK_TO_PREVIOUS_SYNC); } ByteBuffer buffer = ByteBuffer.allocateDirect(maxBufferSize); long startTime = -1; while (!inputDone) { boolean eof = false; int index = extractor.getSampleTrackIndex(); if (index == trackIndex) { info.size = extractor.readSampleData(buffer, 0); if (info.size < 0) { info.size = 0; eof = true; } else { info.presentationTimeUs = extractor.getSampleTime(); if (start > 0 && startTime == -1) { startTime = info.presentationTimeUs; } if (end < 0 || info.presentationTimeUs < end) { info.offset = 0; info.flags = extractor.getSampleFlags(); if (mediaMuxer.writeSampleData(muxerTrackIndex, buffer, info, isAudio)) { // didWriteData(messageObject, file, false, false); } extractor.advance(); } else { eof = true; } } } else if (index == -1) { eof = true; } if (eof) { inputDone = true; } } extractor.unselectTrack(trackIndex); return startTime; } return -1; }
Example 13
Source File: MediaController.java From SiliCompressor with Apache License 2.0 | 4 votes |
@TargetApi(16) private long readAndWriteTrack(MediaExtractor extractor, MP4Builder mediaMuxer, MediaCodec.BufferInfo info, long start, long end, File file, boolean isAudio) throws Exception { int trackIndex = selectTrack(extractor, isAudio); if (trackIndex >= 0) { extractor.selectTrack(trackIndex); MediaFormat trackFormat = extractor.getTrackFormat(trackIndex); int muxerTrackIndex = mediaMuxer.addTrack(trackFormat, isAudio); int maxBufferSize = trackFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE); boolean inputDone = false; if (start > 0) { extractor.seekTo(start, MediaExtractor.SEEK_TO_PREVIOUS_SYNC); } else { extractor.seekTo(0, MediaExtractor.SEEK_TO_PREVIOUS_SYNC); } ByteBuffer buffer = ByteBuffer.allocateDirect(maxBufferSize); long startTime = -1; while (!inputDone) { boolean eof = false; int index = extractor.getSampleTrackIndex(); if (index == trackIndex) { info.size = extractor.readSampleData(buffer, 0); if (info.size < 0) { info.size = 0; eof = true; } else { info.presentationTimeUs = extractor.getSampleTime(); if (start > 0 && startTime == -1) { startTime = info.presentationTimeUs; } if (end < 0 || info.presentationTimeUs < end) { info.offset = 0; info.flags = extractor.getSampleFlags(); if (mediaMuxer.writeSampleData(muxerTrackIndex, buffer, info, isAudio)) { // didWriteData(messageObject, file, false, false); } extractor.advance(); } else { eof = true; } } } else if (index == -1) { eof = true; } if (eof) { inputDone = true; } } extractor.unselectTrack(trackIndex); return startTime; } return -1; }
Example 14
Source File: VideoRecoder.java From deltachat-android with GNU General Public License v3.0 | 4 votes |
@TargetApi(16) private long readAndWriteTrack(MediaExtractor extractor, MP4Builder mediaMuxer, MediaCodec.BufferInfo info, long start, long end, File file, boolean isAudio) throws Exception { int trackIndex = selectTrack(extractor, isAudio); if (trackIndex >= 0) { extractor.selectTrack(trackIndex); MediaFormat trackFormat = extractor.getTrackFormat(trackIndex); int muxerTrackIndex = mediaMuxer.addTrack(trackFormat, isAudio); int maxBufferSize = trackFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE); boolean inputDone = false; if (start > 0) { extractor.seekTo(start, MediaExtractor.SEEK_TO_PREVIOUS_SYNC); } else { extractor.seekTo(0, MediaExtractor.SEEK_TO_PREVIOUS_SYNC); } ByteBuffer buffer = ByteBuffer.allocateDirect(maxBufferSize); long startTime = -1; checkConversionCanceled(); long lastTimestamp = -100; while (!inputDone) { checkConversionCanceled(); boolean eof = false; int index = extractor.getSampleTrackIndex(); if (index == trackIndex) { info.size = extractor.readSampleData(buffer, 0); if (info.size >= 0) { info.presentationTimeUs = extractor.getSampleTime(); } else { info.size = 0; eof = true; } if (info.size > 0 && !eof) { if (start > 0 && startTime == -1) { startTime = info.presentationTimeUs; } if (end < 0 || info.presentationTimeUs < end) { if (info.presentationTimeUs > lastTimestamp) { info.offset = 0; info.flags = extractor.getSampleFlags(); if (mediaMuxer.writeSampleData(muxerTrackIndex, buffer, info, isAudio)) { //didWriteData(messageObject, file, false, false); } } lastTimestamp = info.presentationTimeUs; } else { eof = true; } } if (!eof) { extractor.advance(); } } else if (index == -1) { eof = true; } else { extractor.advance(); } if (eof) { inputDone = true; } } extractor.unselectTrack(trackIndex); return startTime; } return -1; }
Example 15
Source File: VideoResampler.java From AndroidVideoSamples with Apache License 2.0 | 4 votes |
private void feedClipToEncoder( SamplerClip clip ) { mLastSampleTime = 0; MediaCodec decoder = null; MediaExtractor extractor = setupExtractorForClip(clip); if(extractor == null ) { return; } int trackIndex = getVideoTrackIndex(extractor); extractor.selectTrack( trackIndex ); MediaFormat clipFormat = extractor.getTrackFormat( trackIndex ); if ( clip.getStartTime() != -1 ) { extractor.seekTo( clip.getStartTime() * 1000, MediaExtractor.SEEK_TO_PREVIOUS_SYNC ); clip.setStartTime( extractor.getSampleTime() / 1000 ); } try { decoder = MediaCodec.createDecoderByType( MediaHelper.MIME_TYPE_AVC ); mOutputSurface = new OutputSurface(); decoder.configure( clipFormat, mOutputSurface.getSurface(), null, 0 ); decoder.start(); resampleVideo( extractor, decoder, clip ); } finally { if ( mOutputSurface != null ) { mOutputSurface.release(); } if ( decoder != null ) { decoder.stop(); decoder.release(); } if ( extractor != null ) { extractor.release(); extractor = null; } } }