android.media.MediaExtractor Java Examples

The following examples show how to use android.media.MediaExtractor. 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: MediaController.java    From SiliCompressor with Apache License 2.0 7 votes vote down vote up
@TargetApi(16)
private int selectTrack(MediaExtractor extractor, boolean audio) {
    int numTracks = extractor.getTrackCount();
    for (int i = 0; i < numTracks; i++) {
        MediaFormat format = extractor.getTrackFormat(i);
        String mime = format.getString(MediaFormat.KEY_MIME);
        if (audio) {
            if (mime.startsWith("audio/")) {
                return i;
            }
        } else {
            if (mime.startsWith("video/")) {
                return i;
            }
        }
    }
    return -5;
}
 
Example #2
Source File: AudioUtil.java    From VideoProcessor with Apache License 2.0 6 votes vote down vote up
public static boolean isStereo(String aacPath) throws IOException {
    MediaExtractor extractor = new MediaExtractor();
    extractor.setDataSource(aacPath);
    MediaFormat format = null;
    int numTracks = extractor.getTrackCount();
    for (int i = 0; i < numTracks; i++) {
        format = extractor.getTrackFormat(i);
        String mime = format.getString(MediaFormat.KEY_MIME);
        if (mime.startsWith("audio/")) {
            break;
        }
    }
    extractor.release();
    if (format == null) {
        return false;
    }
    return format.getInteger(MediaFormat.KEY_CHANNEL_COUNT) > 1;
}
 
Example #3
Source File: MoviePlayer.java    From pause-resume-video-recording with Apache License 2.0 6 votes vote down vote up
/**
 * Selects the video track, if any.
 *
 * @return the track index, or -1 if no video track is found.
 */
private static int selectTrack(MediaExtractor extractor) {
    // Select the first video track we find, ignore the rest.
    int numTracks = extractor.getTrackCount();
    for (int i = 0; i < numTracks; i++) {
        MediaFormat format = extractor.getTrackFormat(i);
        String mime = format.getString(MediaFormat.KEY_MIME);
        if (mime.startsWith("video/")) {
            if (VERBOSE) {
                Log.d(TAG, "Extractor selected track " + i + " (" + mime + "): " + format);
            }
            return i;
        }
    }

    return -1;
}
 
Example #4
Source File: VideoRecoder.java    From deltachat-android with GNU General Public License v3.0 6 votes vote down vote up
@TargetApi(16)
private int selectTrack(MediaExtractor extractor, boolean audio) {
  int numTracks = extractor.getTrackCount();
  for (int i = 0; i < numTracks; i++) {
    MediaFormat format = extractor.getTrackFormat(i);
    String mime = format.getString(MediaFormat.KEY_MIME);
    if (audio) {
      if (mime.startsWith("audio/")) {
        return i;
      }
    } else {
      if (mime.startsWith("video/")) {
        return i;
      }
    }
  }
  return -5;
}
 
Example #5
Source File: MediaExtractorUtils.java    From Pix-Art-Messenger with GNU General Public License v3.0 6 votes vote down vote up
public static TrackResult getFirstVideoAndAudioTrack(MediaExtractor extractor) {
    TrackResult trackResult = new TrackResult();
    trackResult.mVideoTrackIndex = -1;
    trackResult.mAudioTrackIndex = -1;
    int trackCount = extractor.getTrackCount();
    for (int i = 0; i < trackCount; i++) {
        MediaFormat format = extractor.getTrackFormat(i);
        String mime = format.getString(MediaFormat.KEY_MIME);
        if (trackResult.mVideoTrackIndex < 0 && mime.startsWith("video/")) {
            trackResult.mVideoTrackIndex = i;
            trackResult.mVideoTrackMime = mime;
            trackResult.mVideoTrackFormat = format;
        } else if (trackResult.mAudioTrackIndex < 0 && mime.startsWith("audio/")) {
            trackResult.mAudioTrackIndex = i;
            trackResult.mAudioTrackMime = mime;
            trackResult.mAudioTrackFormat = format;
        }
        if (trackResult.mVideoTrackIndex >= 0 && trackResult.mAudioTrackIndex >= 0) break;
    }
    if (trackResult.mVideoTrackIndex < 0 || trackResult.mAudioTrackIndex < 0) {
        throw new IllegalArgumentException("extractor does not contain video and/or audio tracks.");
    }
    return trackResult;
}
 
Example #6
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 #7
Source File: MediaMoviePlayer.java    From AudioVideoPlayerSample with Apache License 2.0 6 votes vote down vote up
/**
 * @param media_extractor
 * @param trackIndex
 * @return
 */
protected MediaCodec internalStartVideo(final MediaExtractor media_extractor, final int trackIndex) {
	if (DEBUG) Log.v(TAG, "internalStartVideo:");
	MediaCodec codec = null;
	if (trackIndex >= 0) {
        final MediaFormat format = media_extractor.getTrackFormat(trackIndex);
        final String mime = format.getString(MediaFormat.KEY_MIME);
		try {
			codec = MediaCodec.createDecoderByType(mime);
			codec.configure(format, mOutputSurface, null, 0);
	        codec.start();
		} catch (final IOException e) {
			Log.w(TAG, e);
			codec = null;
		}
    	if (DEBUG) Log.v(TAG, "internalStartVideo:codec started");
	}
	return codec;
}
 
Example #8
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 #9
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 #10
Source File: VideoDecoder.java    From rtmp-rtsp-stream-client-java with Apache License 2.0 6 votes vote down vote up
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 #11
Source File: MediaMoviePlayer.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * search first track index matched specific MIME
 * @param extractor
 * @param mimeType "video/" or "audio/"
 * @return track index, -1 if not found
 */
protected static final int selectTrack(final MediaExtractor extractor, final String mimeType) {
    final int numTracks = extractor.getTrackCount();
    MediaFormat format;
    String mime;
    for (int i = 0; i < numTracks; i++) {
        format = extractor.getTrackFormat(i);
        mime = format.getString(MediaFormat.KEY_MIME);
        if (mime.startsWith(mimeType)) {
            if (DEBUG) {
                Log.d(TAG_STATIC, "Extractor selected track " + i + " (" + mime + "): " + format);
            }
            return i;
        }
    }
    return -1;
}
 
Example #12
Source File: MediaMoviePlayer.java    From AudioVideoPlayerSample with Apache License 2.0 6 votes vote down vote up
/**
 * search first track index matched specific MIME
 * @param extractor
 * @param mimeType "video/" or "audio/"
 * @return track index, -1 if not found
 */
protected static final int selectTrack(final MediaExtractor extractor, final String mimeType) {
    final int numTracks = extractor.getTrackCount();
    MediaFormat format;
    String mime;
    for (int i = 0; i < numTracks; i++) {
        format = extractor.getTrackFormat(i);
        mime = format.getString(MediaFormat.KEY_MIME);
        if (mime.startsWith(mimeType)) {
            if (DEBUG) {
                Log.d(TAG_STATIC, "Extractor selected track " + i + " (" + mime + "): " + format);
            }
            return i;
        }
    }
    return -1;
}
 
Example #13
Source File: AudioUtil.java    From VideoProcessor with Apache License 2.0 6 votes vote down vote up
/**
 * 调整aac音量
 *
 * @param volume [0,100]
 * @throws IOException
 */
public static void adjustAacVolume(Context context, VideoProcessor.MediaSource aacSource, String outPath, int volume
        , @Nullable VideoProgressListener listener) throws IOException {
    String name = "temp_aac_"+System.currentTimeMillis();
    File pcmFile = new File(VideoUtil.getVideoCacheDir(context), name + ".pcm");
    File pcmFile2 = new File(VideoUtil.getVideoCacheDir(context), name + "_2.pcm");
    File wavFile = new File(VideoUtil.getVideoCacheDir(context), name + ".wav");

    AudioUtil.decodeToPCM(aacSource, pcmFile.getAbsolutePath(), null, null);
    AudioUtil.adjustPcmVolume(pcmFile.getAbsolutePath(), pcmFile2.getAbsolutePath(), volume);
    MediaExtractor extractor = new MediaExtractor();
    aacSource.setDataSource(extractor);
    int trackIndex = VideoUtil.selectTrack(extractor, true);
    MediaFormat aacFormat = extractor.getTrackFormat(trackIndex);
    int sampleRate = aacFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE);
    int oriChannelCount = aacFormat.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
    int channelConfig = AudioFormat.CHANNEL_IN_MONO;
    if (oriChannelCount == 2) {
        channelConfig = AudioFormat.CHANNEL_IN_STEREO;
    }
    new PcmToWavUtil(sampleRate, channelConfig, oriChannelCount, AudioFormat.ENCODING_PCM_16BIT).pcmToWav(pcmFile2.getAbsolutePath(), wavFile.getAbsolutePath());
    AudioUtil.encodeWAVToAAC(wavFile.getPath(), outPath, aacFormat, listener);
}
 
Example #14
Source File: MediaMoviePlayer.java    From AudioVideoPlayerSample with Apache License 2.0 6 votes vote down vote up
/**
	 * @param codec
	 * @param extractor
	 * @param inputBuffers
	 * @param presentationTimeUs
	 * @param isAudio
	 */
	protected boolean internal_process_input(final MediaCodec codec, final MediaExtractor extractor, final ByteBuffer[] inputBuffers, final long presentationTimeUs, final boolean isAudio) {
//		if (DEBUG) Log.v(TAG, "internalProcessInput:presentationTimeUs=" + presentationTimeUs);
		boolean result = true;
		while (mIsRunning) {
            final int inputBufIndex = codec.dequeueInputBuffer(TIMEOUT_USEC);
            if (inputBufIndex == MediaCodec.INFO_TRY_AGAIN_LATER)
            	break;
            if (inputBufIndex >= 0) {
                final int size = extractor.readSampleData(inputBuffers[inputBufIndex], 0);
                if (size > 0) {
                	codec.queueInputBuffer(inputBufIndex, 0, size, presentationTimeUs, 0);
                }
            	result = extractor.advance();	// return false if no data is available
                break;
            }
		}
		return result;
	}
 
Example #15
Source File: MoviePlayer.java    From grafika with Apache License 2.0 6 votes vote down vote up
/**
 * Selects the video track, if any.
 *
 * @return the track index, or -1 if no video track is found.
 */
private static int selectTrack(MediaExtractor extractor) {
    // Select the first video track we find, ignore the rest.
    int numTracks = extractor.getTrackCount();
    for (int i = 0; i < numTracks; i++) {
        MediaFormat format = extractor.getTrackFormat(i);
        String mime = format.getString(MediaFormat.KEY_MIME);
        if (mime.startsWith("video/")) {
            if (VERBOSE) {
                Log.d(TAG, "Extractor selected track " + i + " (" + mime + "): " + format);
            }
            return i;
        }
    }

    return -1;
}
 
Example #16
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 #17
Source File: MediaVideoPlayer.java    From AudioVideoPlayerSample with Apache License 2.0 6 votes vote down vote up
/**
 * @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 #18
Source File: MediaExtractorUtils.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public static TrackResult getFirstVideoAndAudioTrack(MediaExtractor extractor) {
    TrackResult trackResult = new TrackResult();
    trackResult.mVideoTrackIndex = -1;
    trackResult.mAudioTrackIndex = -1;
    int trackCount = extractor.getTrackCount();
    for (int i = 0; i < trackCount; i++) {
        MediaFormat format = extractor.getTrackFormat(i);
        String mime = format.getString(MediaFormat.KEY_MIME);
        if (trackResult.mVideoTrackIndex < 0 && mime.startsWith("video/")) {
            trackResult.mVideoTrackIndex = i;
            trackResult.mVideoTrackMime = mime;
            trackResult.mVideoTrackFormat = format;
        } else if (trackResult.mAudioTrackIndex < 0 && mime.startsWith("audio/")) {
            trackResult.mAudioTrackIndex = i;
            trackResult.mAudioTrackMime = mime;
            trackResult.mAudioTrackFormat = format;
        }
        if (trackResult.mVideoTrackIndex >= 0 && trackResult.mAudioTrackIndex >= 0) break;
    }
    if (trackResult.mVideoTrackIndex < 0 || trackResult.mAudioTrackIndex < 0) {
        throw new IllegalArgumentException("extractor does not contain video and/or audio tracks.");
    }
    return trackResult;
}
 
Example #19
Source File: VideoUtil.java    From VideoProcessor with Apache License 2.0 6 votes vote down vote up
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 #20
Source File: VideoPlayer.java    From media-for-mobile with Apache License 2.0 6 votes vote down vote up
public void open(String path) throws IOException {
    close();

    pause = false;
    stop = false;
    seekRequestedToMs = -1;
    currentPresentationTimeMs = 0;

    extractor = new MediaExtractor();

    extractor.setDataSource(path);

    if (initWithTrackOfInterest("video/") == false) {
        throw new IOException("Can't open video file. Unsupported video format.");
    }

    if (codec == null) {
        throw new IOException("Can't open video file. Unsupported video format.");
    }

    pause();

    videoThread = new VideoThread();
    videoThread.start();
}
 
Example #21
Source File: MediaMoviePlayer.java    From AudioVideoPlayerSample with Apache License 2.0 6 votes vote down vote up
/**
 * @param media_extractor
 * @param trackIndex
 * @return
 */
protected MediaCodec internalStartAudio(final MediaExtractor media_extractor, final int trackIndex) {
	if (DEBUG) Log.v(TAG, "internalStartAudio:");
	MediaCodec codec = null;
	if (trackIndex >= 0) {
        final MediaFormat format = media_extractor.getTrackFormat(trackIndex);
        final String mime = format.getString(MediaFormat.KEY_MIME);
		try {
			codec = MediaCodec.createDecoderByType(mime);
			codec.configure(format, null, null, 0);
	        codec.start();
	    	if (DEBUG) Log.v(TAG, "internalStartAudio:codec started");
	    	//
	        final ByteBuffer[] buffers = codec.getOutputBuffers();
	        int sz = buffers[0].capacity();
	        if (sz <= 0)
	        	sz = mAudioInputBufSize;
	        if (DEBUG) Log.v(TAG, "AudioOutputBufSize:" + sz);
	        mAudioOutTempBuf = new byte[sz];
		} catch (final IOException e) {
			Log.w(TAG, e);
			codec = null;
		}
	}
	return codec;
}
 
Example #22
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 #23
Source File: Track.java    From K-Sonic with MIT License 6 votes vote down vote up
public void initStream() throws Exception {
    mLock.lock();
    try {
        mExtractor = new MediaExtractor();
        if (!TextUtils.isEmpty(mPath)) {
            mExtractor.setDataSource(mPath);
        } else if (mUri != null) {
            mExtractor.setDataSource(mContext, mUri, null);
        } else {
            throw new IOException();
        }
    } catch (Exception e) {//IOException
        throw e;
    } finally {
        mLock.unlock();
    }
    initConfig();
}
 
Example #24
Source File: AudioRecordThread.java    From PhotoMovie with Apache License 2.0 6 votes vote down vote up
private int selectTrack(MediaExtractor extractor, boolean audio) {
    int numTracks = extractor.getTrackCount();
    for (int i = 0; i < numTracks; i++) {
        MediaFormat format = extractor.getTrackFormat(i);
        String mime = format.getString(MediaFormat.KEY_MIME);
        if (audio) {
            if (mime.startsWith("audio/")) {
                return i;
            }
        } else {
            if (mime.startsWith("video/")) {
                return i;
            }
        }
    }
    return -5;
}
 
Example #25
Source File: MediaHelper.java    From AndroidVideoSamples with Apache License 2.0 6 votes vote down vote up
@TargetApi( Build.VERSION_CODES.JELLY_BEAN )
public static int GetMediaFormatPropertyInteger( Uri uri, String key, int defaultValue ) {
   int value = defaultValue;

   MediaExtractor extractor = new MediaExtractor();
   try {
      extractor.setDataSource( uri.toString() );
   } catch ( IOException e ) {
      e.printStackTrace();
      return value;
   }
   
   MediaFormat format = GetTrackFormat( extractor, MIME_TYPE_AVC );
   extractor.release();

   if ( format.containsKey( key ) ) {
      value = format.getInteger( key );
   }

   return value;
}
 
Example #26
Source File: MediaMoviePlayer.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
	 * @param codec
	 * @param extractor
	 * @param inputBuffers
	 * @param presentationTimeUs
	 * @param isAudio
	 */
	protected boolean internal_process_input(final MediaCodec codec, final MediaExtractor extractor, final ByteBuffer[] inputBuffers, final long presentationTimeUs, final boolean isAudio) {
//		if (DEBUG) Log.v(TAG, "internal_process_input:presentationTimeUs=" + presentationTimeUs);
		boolean result = true;
		while (mIsRunning) {
            final int inputBufIndex = codec.dequeueInputBuffer(TIMEOUT_USEC);
            if (inputBufIndex == MediaCodec.INFO_TRY_AGAIN_LATER)
            	break;
            if (inputBufIndex >= 0) {
                final int size = extractor.readSampleData(inputBuffers[inputBufIndex], 0);
                if (size > 0) {
                	codec.queueInputBuffer(inputBufIndex, 0, size, presentationTimeUs, 0);
                }
            	result = extractor.advance();	// return false if no data is available
                break;
            }
		}
		return result;
	}
 
Example #27
Source File: MediaController.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
public static int findTrack(MediaExtractor extractor, boolean audio) {
    int numTracks = extractor.getTrackCount();
    for (int i = 0; i < numTracks; i++) {
        MediaFormat format = extractor.getTrackFormat(i);
        String mime = format.getString(MediaFormat.KEY_MIME);
        if (audio) {
            if (mime.startsWith("audio/")) {
                return i;
            }
        } else {
            if (mime.startsWith("video/")) {
                return i;
            }
        }
    }
    return -5;
}
 
Example #28
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 #29
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 #30
Source File: MediaVideoPlayer.java    From AudioVideoPlayerSample with Apache License 2.0 6 votes vote down vote up
/**
 * search first track index matched specific MIME
 * @param extractor
 * @param mimeType "video/" or "audio/"
 * @return track index, -1 if not found
 */
protected static final int selectTrack(final MediaExtractor extractor, final String mimeType) {
    final int numTracks = extractor.getTrackCount();
    MediaFormat format;
    String mime;
    for (int i = 0; i < numTracks; i++) {
        format = extractor.getTrackFormat(i);
        mime = format.getString(MediaFormat.KEY_MIME);
        if (mime.startsWith(mimeType)) {
            if (DEBUG) {
                Log.d(TAG_STATIC, "Extractor selected track " + i + " (" + mime + "): " + format);
            }
            return i;
        }
    }
    return -1;
}