Java Code Examples for android.media.MediaExtractor#getTrackCount()

The following examples show how to use android.media.MediaExtractor#getTrackCount() . 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: 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 2
Source File: ExtractMpegFramesTest_egl14.java    From Android-MediaCodec-Examples 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 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 3
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 4
Source File: VideoController.java    From VideoCompressor with Apache License 2.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 android-transcoder 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 6
Source File: MediaController.java    From react-native-video-helper with MIT License 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 7
Source File: MediaController.java    From Telegram 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 8
Source File: ExtractMpegFramesTest.java    From Android-MediaCodec-Examples 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 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 9
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 10
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 11
Source File: AudioTransCoder.java    From SimpleVideoEditor with Apache License 2.0 5 votes vote down vote up
private void prepare() throws IOException {
    extractor = new MediaExtractor();
    extractor.setDataSource(mInputFile.getAbsolutePath());
    int numTracks = extractor.getTrackCount();
    for (int i = 0; i < numTracks; i++) {
        MediaFormat format = extractor.getTrackFormat(i);
        String mine = format.getString(MediaFormat.KEY_MIME);
        if (!TextUtils.isEmpty(mine) && mine.startsWith("audio")) {
            extractor.selectTrack(i);
            if (mDurationMs == 0) {
                try {
                    mDurationMs = format.getLong(MediaFormat.KEY_DURATION) / 1000;
                } catch (Exception e) {
                    e.printStackTrace();
                    MediaPlayer mediaPlayer = new MediaPlayer();
                    mediaPlayer.setDataSource(mInputFile.getAbsolutePath());
                    mediaPlayer.prepare();
                    mDurationMs = mediaPlayer.getDuration();
                    mediaPlayer.release();
                }
            }

            if (mDurationMs == 0) {
                throw new IllegalStateException("We can not get duration info from input file: " + mInputFile);
            }

            decoder = MediaCodec.createDecoderByType(mine);
            decoder.configure(format, null, null, 0);
            decoder.start();
            break;
        }
    }
}
 
Example 12
Source File: VideoBgmAddAction.java    From SimpleVideoEditor with Apache License 2.0 5 votes vote down vote up
private String getBgmMime() throws IOException {
    String mine = null;

    MediaExtractor extractor = new MediaExtractor();
    extractor.setDataSource(mBgmFile.getAbsolutePath());
    for (int i = 0; i < extractor.getTrackCount(); i++) {
        MediaFormat mediaFormat = extractor.getTrackFormat(i);
        if (mediaFormat.getString(MediaFormat.KEY_MIME).startsWith("audio")) {
            mine = mediaFormat.getString(MediaFormat.KEY_MIME);
            break;
        }
    }

    return mine;
}
 
Example 13
Source File: VideoUtil.java    From VideoProcessor with Apache License 2.0 5 votes vote down vote up
public static 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 14
Source File: BaseAudioDecoder.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void initMediaComponents() throws Exception {
    if(targetSampleRate <= 0){
        throw new InstantiationException("Target sample rate of " + targetSampleRate + " is unsupported");
    }

    extractor = new MediaExtractor();
    Context contextRef = contextWeakReference.get();
    if(contextRef == null){
        throw new InstantiationException("Context reference was null");
    }
    extractor.setDataSource(contextRef, audioSource, null);
    MediaFormat format = null;
    String mime = null;

    // Select the first audio track we find.
    int numTracks = extractor.getTrackCount();
    for (int i = 0; i < numTracks; ++i) {
        MediaFormat f = extractor.getTrackFormat(i);
        String m = f.getString(MediaFormat.KEY_MIME);
        if (m.startsWith("audio/")) {
            format = f;
            mime = m;
            extractor.selectTrack(i);
            break;
        }
    }

    if (mime == null) {
        throw new Exception("The audio file " + audioSource.getPath() + " doesn't contain an audio track.");
    }

    decoder = MediaCodec.createDecoderByType(mime);
    decoder.configure(format, null, null, 0);
}
 
Example 15
Source File: MediaCodecDecodeController.java    From AndroidVideoSamples with Apache License 2.0 5 votes vote down vote up
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 16
Source File: Mp4Provider.java    From AAVT with Apache License 2.0 5 votes vote down vote up
private boolean extractMedia(){
    if(mPath==null||!new File(mPath).exists()){
        //文件不存在
        return false;
    }
    try {
        MediaMetadataRetriever mMetRet=new MediaMetadataRetriever();
        mMetRet.setDataSource(mPath);
        mVideoTotalTime=Long.valueOf(mMetRet.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
        mExtractor=new MediaExtractor();
        mExtractor.setDataSource(mPath);
        int trackCount=mExtractor.getTrackCount();
        for (int i=0;i<trackCount;i++){
            MediaFormat format=mExtractor.getTrackFormat(i);
            String mime=format.getString(MediaFormat.KEY_MIME);
            if(mime.startsWith("audio")){
                mAudioDecodeTrack=i;
            }else if(mime.startsWith("video")){
                mVideoDecodeTrack=i;
                int videoRotation=0;
                String rotation=mMetRet.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
                if(rotation!=null){
                    videoRotation=Integer.valueOf(rotation);
                }
                if(videoRotation%180!=0){
                    mVideoSize.y=format.getInteger(MediaFormat.KEY_WIDTH);
                    mVideoSize.x=format.getInteger(MediaFormat.KEY_HEIGHT);
                }else{
                    mVideoSize.x=format.getInteger(MediaFormat.KEY_WIDTH);
                    mVideoSize.y=format.getInteger(MediaFormat.KEY_HEIGHT);
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}
 
Example 17
Source File: AudioReader.java    From media-for-mobile with Apache License 2.0 5 votes vote down vote up
protected int getAndSelectAudioTrackIndex(MediaExtractor extractor) {
    for (int index = 0; index < extractor.getTrackCount(); ++index) {
        if (isAudioFormat(extractor.getTrackFormat(index))) {
            extractor.selectTrack(index);
            return index;
        }
    }
    return -1;
}
 
Example 18
Source File: AudioTrackConverter.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
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 19
Source File: MainActivity.java    From Android with Apache License 2.0 4 votes vote down vote up
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 vote down vote up
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();
    }
  }
}