Java Code Examples for android.media.MediaExtractor#selectTrack()
The following examples show how to use
android.media.MediaExtractor#selectTrack() .
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: MediaVideoPlayer.java From AudioVideoPlayerSample with Apache License 2.0 | 6 votes |
/** * @param sourceFile * @return first video track index, -1 if not found */ protected int internalPrepareVideo(final String sourceFile) { int trackIndex = -1; mVideoMediaExtractor = new MediaExtractor(); try { mVideoMediaExtractor.setDataSource(sourceFile); trackIndex = selectTrack(mVideoMediaExtractor, "video/"); if (trackIndex >= 0) { mVideoMediaExtractor.selectTrack(trackIndex); final MediaFormat format = mVideoMediaExtractor.getTrackFormat(trackIndex); mVideoWidth = format.getInteger(MediaFormat.KEY_WIDTH); mVideoHeight = format.getInteger(MediaFormat.KEY_HEIGHT); mDuration = format.getLong(MediaFormat.KEY_DURATION); if (DEBUG) Log.v(TAG, String.format("format:size(%d,%d),duration=%d,bps=%d,framerate=%f,rotation=%d", mVideoWidth, mVideoHeight, mDuration, mBitrate, mFrameRate, mRotation)); } } catch (final IOException e) { Log.w(TAG, e); } return trackIndex; }
Example 2
Source File: MediaMoviePlayer.java From AudioVideoPlayerSample with Apache License 2.0 | 6 votes |
/** * @param sourceFile * @return first video track index, -1 if not found */ protected int internalPrepareVideo(final String sourceFile) { int trackIndex = -1; mVideoMediaExtractor = new MediaExtractor(); try { mVideoMediaExtractor.setDataSource(sourceFile); trackIndex = selectTrack(mVideoMediaExtractor, "video/"); if (trackIndex >= 0) { mVideoMediaExtractor.selectTrack(trackIndex); final MediaFormat format = mVideoMediaExtractor.getTrackFormat(trackIndex); mVideoWidth = format.getInteger(MediaFormat.KEY_WIDTH); mVideoHeight = format.getInteger(MediaFormat.KEY_HEIGHT); mDuration = format.getLong(MediaFormat.KEY_DURATION); if (DEBUG) Log.v(TAG, String.format("format:size(%d,%d),duration=%d,bps=%d,framerate=%f,rotation=%d", mVideoWidth, mVideoHeight, mDuration, mBitrate, mFrameRate, mRotation)); } } catch (final IOException e) { Log.w(TAG, e); } return trackIndex; }
Example 3
Source File: VideoUtil.java From VideoProcessor with Apache License 2.0 | 6 votes |
public static float getAveFrameRate(VideoProcessor.MediaSource mediaSource) throws IOException { MediaExtractor extractor = new MediaExtractor(); mediaSource.setDataSource(extractor); int trackIndex = VideoUtil.selectTrack(extractor, false); extractor.selectTrack(trackIndex); long lastSampleTimeUs = 0; int frameCount = 0; while (true) { long sampleTime = extractor.getSampleTime(); if (sampleTime < 0) { break; } else { lastSampleTimeUs = sampleTime; } frameCount++; extractor.advance(); } extractor.release(); return frameCount / (lastSampleTimeUs / 1000f / 1000f); }
Example 4
Source File: VideoDecoder.java From rtmp-rtsp-stream-client-java with Apache License 2.0 | 6 votes |
public boolean initExtractor(String filePath) throws IOException { decoding = false; videoExtractor = new MediaExtractor(); videoExtractor.setDataSource(filePath); for (int i = 0; i < videoExtractor.getTrackCount() && !mime.startsWith("video/"); i++) { videoFormat = videoExtractor.getTrackFormat(i); mime = videoFormat.getString(MediaFormat.KEY_MIME); if (mime.startsWith("video/")) { videoExtractor.selectTrack(i); } else { videoFormat = null; } } if (videoFormat != null) { width = videoFormat.getInteger(MediaFormat.KEY_WIDTH); height = videoFormat.getInteger(MediaFormat.KEY_HEIGHT); duration = videoFormat.getLong(MediaFormat.KEY_DURATION); return true; //video decoder not supported } else { mime = ""; videoFormat = null; return false; } }
Example 5
Source File: VideoTrackConverter.java From mollyim-android with GNU General Public License v3.0 | 5 votes |
private static int getAndSelectVideoTrackIndex(@NonNull MediaExtractor extractor) { for (int index = 0; index < extractor.getTrackCount(); ++index) { if (VERBOSE) { Log.d(TAG, "format for track " + index + " is " + MediaConverter.getMimeTypeFor(extractor.getTrackFormat(index))); } if (isVideoFormat(extractor.getTrackFormat(index))) { extractor.selectTrack(index); return index; } } return -1; }
Example 6
Source File: AudioUtil.java From VideoProcessor with Apache License 2.0 | 5 votes |
/** * 去掉视频的音轨 */ public static void removeAudioTrack(String videoPath, String outPath) throws IOException { MediaExtractor videoExtractor = new MediaExtractor(); videoExtractor.setDataSource(videoPath); try { int videoTrack = VideoUtil.selectTrack(videoExtractor, false); videoExtractor.selectTrack(videoTrack); MediaFormat videoFormat = videoExtractor.getTrackFormat(videoTrack); MediaMuxer mediaMuxer = new MediaMuxer(outPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); int muxerVideoTrackIndex = mediaMuxer.addTrack(videoFormat); mediaMuxer.start(); MediaCodec.BufferInfo info = new MediaCodec.BufferInfo(); //写视频 int maxBufferSize = videoFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE); ByteBuffer videoBuffer = ByteBuffer.allocateDirect(maxBufferSize); while (true) { long sampleTimeUs = videoExtractor.getSampleTime(); if (sampleTimeUs == -1) { break; } int flags = videoExtractor.getSampleFlags(); int size = videoExtractor.readSampleData(videoBuffer, 0); info.presentationTimeUs = sampleTimeUs; info.flags = flags; info.size = size; mediaMuxer.writeSampleData(muxerVideoTrackIndex, videoBuffer, info); videoExtractor.advance(); } mediaMuxer.stop(); mediaMuxer.release(); } finally { videoExtractor.release(); } }
Example 7
Source File: MediaAudioFileEncoder.java From PLDroidShortVideo with Apache License 2.0 | 5 votes |
@Override protected void prepare() throws IOException { if (DEBUG) Log.v(TAG, "prepare:"); mTrackIndex = -1; mMuxerStarted = mIsEOS = false; mMediaExtractor = new MediaExtractor(); mMediaExtractor.setDataSource(mFilepath); MediaMuxerWrapper muxer = mWeakMuxer.get(); //分离出音轨和视轨 Log.d(TAG, "getTrackCount: " + mMediaExtractor.getTrackCount()); for (int i = 0; i < mMediaExtractor.getTrackCount(); i++) { MediaFormat format = mMediaExtractor.getTrackFormat(i); String mime = format.getString(MediaFormat.KEY_MIME); if (mime.startsWith(AUDIO)) { int maxInputSize = format.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE); mInputBuffer = ByteBuffer.allocate(maxInputSize); mMediaExtractor.selectTrack(i); mTrackIndex = muxer.addTrack(format); break; } } if (mListener != null) { try { mListener.onPrepared(this); } catch (final Exception e) { Log.e(TAG, "prepare:", e); } } }
Example 8
Source File: MediaCodecDecodeController.java From AndroidVideoSamples with Apache License 2.0 | 5 votes |
private void setupExtractor() { mExtractor = new MediaExtractor(); try { mExtractor.setDataSource( mUri.toString() ); } catch ( IOException e ) { e.printStackTrace(); } int videoIndex = 0; for ( int trackIndex = 0; trackIndex < mExtractor.getTrackCount(); trackIndex++ ) { MediaFormat format = mExtractor.getTrackFormat( trackIndex ); String mime = format.getString( MediaFormat.KEY_MIME ); if ( mime != null ) { if ( mime.equals( "video/avc" ) ) { mExtractor.selectTrack( trackIndex ); videoIndex = trackIndex; break; } } } mDecoder = MediaCodec.createDecoderByType( "video/avc" ); mDecoder.configure( mExtractor.getTrackFormat( videoIndex ), mSurface, null, 0 ); mDecoder.start(); mInfo = new BufferInfo(); mInputBuffers = mDecoder.getInputBuffers(); mOutputBuffers = mDecoder.getOutputBuffers(); }
Example 9
Source File: VideoUtil.java From VideoProcessor with Apache License 2.0 | 5 votes |
/** * 用于制作全关键帧视频时计算比特率应该为多少 * * @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 10
Source File: TranscodeVideoUtil.java From SimpleVideoEdit with Apache License 2.0 | 5 votes |
private int getAndSelectAudioTrackIndex(MediaExtractor extractor) { for (int index = 0; index < extractor.getTrackCount(); ++index) { if (VERBOSE) { Log.d(TAG, "format for track " + index + " is " + getMimeTypeFor(extractor.getTrackFormat(index))); } if (isAudioFormat(extractor.getTrackFormat(index))) { extractor.selectTrack(index); return index; } } return -1; }
Example 11
Source File: TranscodeVideoUtil.java From SimpleVideoEdit with Apache License 2.0 | 5 votes |
private int getAndSelectVideoTrackIndex(MediaExtractor extractor) { for (int index = 0; index < extractor.getTrackCount(); ++index) { if (VERBOSE) { Log.d(TAG, "format for track " + index + " is " + getMimeTypeFor(extractor.getTrackFormat(index))); } if (isVideoFormat(extractor.getTrackFormat(index))) { extractor.selectTrack(index); return index; } } return -1; }
Example 12
Source File: MediaMoviePlayer.java From libcommon with Apache License 2.0 | 5 votes |
/** * @param source * @return first video track index, -1 if not found */ @SuppressLint("NewApi") protected int internal_prepare_video(final Object source) throws IOException { int trackindex = -1; mVideoMediaExtractor = new MediaExtractor(); if (source instanceof String) { mVideoMediaExtractor.setDataSource((String)source); } else if (source instanceof AssetFileDescriptor) { if (BuildCheck.isAndroid7()) { mVideoMediaExtractor.setDataSource((AssetFileDescriptor)source); } else { mVideoMediaExtractor.setDataSource(((AssetFileDescriptor)source).getFileDescriptor()); } } else { // ここには来ないけど throw new IllegalArgumentException("unknown source type:source=" + source); } trackindex = selectTrack(mVideoMediaExtractor, "video/"); if (trackindex >= 0) { mVideoMediaExtractor.selectTrack(trackindex); final MediaFormat format = mVideoMediaExtractor.getTrackFormat(trackindex); mVideoWidth = format.getInteger(MediaFormat.KEY_WIDTH); mVideoHeight = format.getInteger(MediaFormat.KEY_HEIGHT); mDuration = format.getLong(MediaFormat.KEY_DURATION); if (DEBUG) Log.v(TAG, String.format("format:size(%d,%d),duration=%d,bps=%d,framerate=%f,rotation=%d", mVideoWidth, mVideoHeight, mDuration, mBitrate, mFrameRate, mRotation)); } return trackindex; }
Example 13
Source File: AudioTrackConverter.java From mollyim-android with GNU General Public License v3.0 | 5 votes |
private static int getAndSelectAudioTrackIndex(MediaExtractor extractor) { for (int index = 0; index < extractor.getTrackCount(); ++index) { if (VERBOSE) { Log.d(TAG, "format for track " + index + " is " + MediaConverter.getMimeTypeFor(extractor.getTrackFormat(index))); } if (isAudioFormat(extractor.getTrackFormat(index))) { extractor.selectTrack(index); return index; } } return -1; }
Example 14
Source File: MediaMoviePlayer.java From libcommon with Apache License 2.0 | 4 votes |
/** * @param source * @return first audio track index, -1 if not found */ @SuppressLint("NewApi") protected int internal_prepare_audio(final Object source) throws IOException { int trackindex = -1; mAudioMediaExtractor = new MediaExtractor(); if (source instanceof String) { mAudioMediaExtractor.setDataSource((String)source); } else if (source instanceof AssetFileDescriptor) { if (BuildCheck.isAndroid7()) { mVideoMediaExtractor.setDataSource((AssetFileDescriptor)source); } else { mVideoMediaExtractor.setDataSource(((AssetFileDescriptor)source).getFileDescriptor()); } } else { // ここには来ないけど throw new IllegalArgumentException("unknown source type:source=" + source); } trackindex = selectTrack(mAudioMediaExtractor, "audio/"); if (trackindex >= 0) { mAudioMediaExtractor.selectTrack(trackindex); final MediaFormat format = mAudioMediaExtractor.getTrackFormat(trackindex); mAudioChannels = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT); mAudioSampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE); final int min_buf_size = AudioTrack.getMinBufferSize(mAudioSampleRate, (mAudioChannels == 1 ? AudioFormat.CHANNEL_OUT_MONO : AudioFormat.CHANNEL_OUT_STEREO), AudioFormat.ENCODING_PCM_16BIT); final int max_input_size = format.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE); mAudioInputBufSize = min_buf_size > 0 ? min_buf_size * 4 : max_input_size; if (mAudioInputBufSize > max_input_size) mAudioInputBufSize = max_input_size; final int frameSizeInBytes = mAudioChannels * 2; mAudioInputBufSize = (mAudioInputBufSize / frameSizeInBytes) * frameSizeInBytes; if (DEBUG) Log.v(TAG, String.format("getMinBufferSize=%d,max_input_size=%d,mAudioInputBufSize=%d",min_buf_size, max_input_size, mAudioInputBufSize)); // mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, mAudioSampleRate, (mAudioChannels == 1 ? AudioFormat.CHANNEL_OUT_MONO : AudioFormat.CHANNEL_OUT_STEREO), AudioFormat.ENCODING_PCM_16BIT, mAudioInputBufSize, AudioTrack.MODE_STREAM); try { mAudioTrack.play(); } catch (final Exception e) { Log.e(TAG, "failed to start audio track playing", e); mAudioTrack.release(); mAudioTrack = null; } } return trackindex; }
Example 15
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 16
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 17
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 18
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; } } }
Example 19
Source File: MainActivity.java From Android with Apache License 2.0 | 4 votes |
protected boolean process() throws IOException { mMediaExtractor = new MediaExtractor(); mMediaExtractor.setDataSource(SDCARD_PATH+"/input.mp4"); int mVideoTrackIndex = -1; int framerate = 0; for(int i = 0; i < mMediaExtractor.getTrackCount(); i++) { MediaFormat format = mMediaExtractor.getTrackFormat(i); String mime = format.getString(MediaFormat.KEY_MIME); if(!mime.startsWith("video/")) { continue; } framerate = format.getInteger(MediaFormat.KEY_FRAME_RATE); mMediaExtractor.selectTrack(i); mMediaMuxer = new MediaMuxer(SDCARD_PATH+"/ouput.mp4", OutputFormat.MUXER_OUTPUT_MPEG_4); mVideoTrackIndex = mMediaMuxer.addTrack(format); mMediaMuxer.start(); } if(mMediaMuxer == null) { return false; } BufferInfo info = new BufferInfo(); info.presentationTimeUs = 0; ByteBuffer buffer = ByteBuffer.allocate(500*1024); while(true) { int sampleSize = mMediaExtractor.readSampleData(buffer, 0); if(sampleSize < 0) { break; } mMediaExtractor.advance(); info.offset = 0; info.size = sampleSize; info.flags = MediaCodec.BUFFER_FLAG_SYNC_FRAME; info.presentationTimeUs += 1000*1000/framerate; mMediaMuxer.writeSampleData(mVideoTrackIndex,buffer,info); } mMediaExtractor.release(); mMediaMuxer.stop(); mMediaMuxer.release(); return true; }
Example 20
Source File: VideoThumbnailsExtractor.java From mollyim-android with GNU General Public License v3.0 | 4 votes |
static void extractThumbnails(final @NonNull MediaInput input, final int thumbnailCount, final int thumbnailResolution, final @NonNull Callback callback) { MediaExtractor extractor = null; MediaCodec decoder = null; OutputSurface outputSurface = null; try { extractor = input.createExtractor(); MediaFormat mediaFormat = null; for (int index = 0; index < extractor.getTrackCount(); ++index) { if (extractor.getTrackFormat(index).getString(MediaFormat.KEY_MIME).startsWith("video/")) { extractor.selectTrack(index); mediaFormat = extractor.getTrackFormat(index); break; } } if (mediaFormat != null) { final String mime = mediaFormat.getString(MediaFormat.KEY_MIME); final int rotation = mediaFormat.containsKey(MediaFormat.KEY_ROTATION) ? mediaFormat.getInteger(MediaFormat.KEY_ROTATION) : 0; final int width = mediaFormat.getInteger(MediaFormat.KEY_WIDTH); final int height = mediaFormat.getInteger(MediaFormat.KEY_HEIGHT); final int outputWidth; final int outputHeight; if (width < height) { outputWidth = thumbnailResolution; outputHeight = height * outputWidth / width; } else { outputHeight = thumbnailResolution; outputWidth = width * outputHeight / height; } final int outputWidthRotated; final int outputHeightRotated; if ((rotation % 180 == 90)) { //noinspection SuspiciousNameCombination outputWidthRotated = outputHeight; //noinspection SuspiciousNameCombination outputHeightRotated = outputWidth; } else { outputWidthRotated = outputWidth; outputHeightRotated = outputHeight; } Log.i(TAG, "video: " + width + "x" + height + " " + rotation); Log.i(TAG, "output: " + outputWidthRotated + "x" + outputHeightRotated); outputSurface = new OutputSurface(outputWidthRotated, outputHeightRotated, true); decoder = MediaCodec.createDecoderByType(mime); decoder.configure(mediaFormat, outputSurface.getSurface(), null, 0); decoder.start(); long duration = 0; if (mediaFormat.containsKey(MediaFormat.KEY_DURATION)) { duration = mediaFormat.getLong(MediaFormat.KEY_DURATION); } else { Log.w(TAG, "Video is missing duration!"); } callback.durationKnown(duration); doExtract(extractor, decoder, outputSurface, outputWidthRotated, outputHeightRotated, duration, thumbnailCount, callback); } } catch (IOException | TranscodingException e) { Log.w(TAG, e); callback.failed(); } finally { if (outputSurface != null) { outputSurface.release(); } if (decoder != null) { decoder.stop(); decoder.release(); } if (extractor != null) { extractor.release(); } } }