Java Code Examples for android.media.MediaFormat#getString()
The following examples show how to use
android.media.MediaFormat#getString() .
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 |
@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: MediaVideoPlayer.java From AudioVideoPlayerSample with Apache License 2.0 | 6 votes |
/** * @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) { codec = null; Log.w(TAG, e); } if (DEBUG) Log.v(TAG, "internalStartVideo:codec started"); } return codec; }
Example 3
Source File: MediaMoviePlayer.java From AudioVideoPlayerSample with Apache License 2.0 | 6 votes |
/** * @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 4
Source File: AudioUtil.java From VideoProcessor with Apache License 2.0 | 6 votes |
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 5
Source File: MediaPlayer.java From MediaPlayer-Extended with Apache License 2.0 | 6 votes |
private int getTrackIndex(MediaExtractor mediaExtractor, String mimeType) { if(mediaExtractor == null) { return MediaCodecDecoder.INDEX_NONE; } for (int i = 0; i < mediaExtractor.getTrackCount(); ++i) { MediaFormat format = mediaExtractor.getTrackFormat(i); Log.d(TAG, format.toString()); String mime = format.getString(MediaFormat.KEY_MIME); if (mime.startsWith(mimeType)) { return i; } } return MediaCodecDecoder.INDEX_NONE; }
Example 6
Source File: VideoController.java From VideoCompressor with Apache License 2.0 | 6 votes |
@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: TranscoderUtils.java From LiTr with BSD 2-Clause "Simplified" License | 6 votes |
/** * Estimate target file size for a video with one video and one audio track * @param mediaSource source video * @param targetVideoFormat target video format * @param targetAudioFormat target audio format, null if not transformed * @return estimated size in bytes, zero if estimation fails */ public static long getEstimatedTargetVideoFileSize(@NonNull MediaSource mediaSource, @NonNull MediaFormat targetVideoFormat, @Nullable MediaFormat targetAudioFormat) { List<TrackTransform> trackTransforms = new ArrayList<>(mediaSource.getTrackCount()); for (int track = 0; track < mediaSource.getTrackCount(); track++) { MediaFormat sourceMediaFormat = mediaSource.getTrackFormat(track); // we are passing null for MediaTarget here, because MediaTarget is not used when estimating target size TrackTransform.Builder trackTransformBuilder = new TrackTransform.Builder(mediaSource, track, null); if (sourceMediaFormat.containsKey(MediaFormat.KEY_MIME)) { String mimeType = sourceMediaFormat.getString(MediaFormat.KEY_MIME); if (mimeType.startsWith("video")) { trackTransformBuilder.setTargetFormat(targetVideoFormat); } else if (mimeType.startsWith("audio")) { trackTransformBuilder.setTargetFormat(targetAudioFormat); } } trackTransforms.add(trackTransformBuilder.build()); } return getEstimatedTargetFileSize(trackTransforms); }
Example 8
Source File: ExtractMpegFramesTest_egl14.java From Android-MediaCodec-Examples with Apache License 2.0 | 6 votes |
/** * 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: TranscoderUtils.java From LiTr with BSD 2-Clause "Simplified" License | 5 votes |
/** * Estimates video track bitrate. On many devices bitrate value is not specified in {@link MediaFormat} for video track. * Since not all data required for accurate estimation is available, this method makes several assumptions: * - if multiple video tracks are present, average per-pixel bitrate is assumed to be the same for all tracks * - if either bitrate or duration are not specified for a track, its size is not accounted for * * @param mediaSource {@link MediaSource} which contains the video track * @param trackIndex index of video track * @return estimated bitrate in bits per second */ public static int estimateVideoTrackBitrate(@NonNull MediaSource mediaSource, int trackIndex) { MediaFormat videoTrackFormat = mediaSource.getTrackFormat(trackIndex); if (videoTrackFormat.containsKey(MediaFormat.KEY_BIT_RATE)) { return videoTrackFormat.getInteger(MediaFormat.KEY_BIT_RATE); } long unallocatedSize = mediaSource.getSize(); long totalPixels = 0; int trackCount = mediaSource.getTrackCount(); for (int track = 0; track < trackCount; track++) { MediaFormat trackFormat = mediaSource.getTrackFormat(track); if (trackFormat.containsKey(MediaFormat.KEY_MIME)) { if (trackFormat.containsKey(MediaFormat.KEY_BIT_RATE) && trackFormat.containsKey(MediaFormat.KEY_DURATION)) { int bitrate = trackFormat.getInteger(MediaFormat.KEY_BIT_RATE); long duration = trackFormat.getLong(MediaFormat.KEY_DURATION); unallocatedSize -= bitrate * TimeUnit.MICROSECONDS.toSeconds(duration) / 8; } else { String mimeType = trackFormat.getString(MediaFormat.KEY_MIME); if (mimeType.startsWith("video")) { totalPixels += trackFormat.getInteger(MediaFormat.KEY_WIDTH) * trackFormat.getInteger(MediaFormat.KEY_HEIGHT) * TimeUnit.MICROSECONDS.toSeconds(trackFormat.getLong(MediaFormat.KEY_DURATION)); } } } } long trackPixels = videoTrackFormat.getInteger(MediaFormat.KEY_WIDTH) * videoTrackFormat.getInteger(MediaFormat.KEY_HEIGHT) * TimeUnit.MICROSECONDS.toSeconds(videoTrackFormat.getLong(MediaFormat.KEY_DURATION)); long trackSize = unallocatedSize * trackPixels / totalPixels; return (int) (trackSize * 8 / TimeUnit.MICROSECONDS.toSeconds(videoTrackFormat.getLong(MediaFormat.KEY_DURATION))); }
Example 10
Source File: MediaCodecListCompat.java From phoenix with Apache License 2.0 | 5 votes |
private String findCoderForFormat(MediaFormat format, boolean findEncoder) { String mimeType = format.getString(MediaFormat.KEY_MIME); Iterator<MediaCodecInfo> iterator = new MediaCodecInfoIterator(); while (iterator.hasNext()) { MediaCodecInfo codecInfo = iterator.next(); if (codecInfo.isEncoder() != findEncoder) continue; if (Arrays.asList(codecInfo.getSupportedTypes()).contains(mimeType)) { return codecInfo.getName(); } } return null; }
Example 11
Source File: BaseEncoder.java From SoloPi with Apache License 2.0 | 5 votes |
/** * Must call in a worker handler thread! */ @Override public void prepare() throws IOException { if (Looper.myLooper() == null || Looper.myLooper() == Looper.getMainLooper()) { throw new IllegalStateException("should run in a HandlerThread"); } if (mEncoder != null) { throw new IllegalStateException("prepared!"); } MediaFormat format = createMediaFormat(); LogUtil.i("Encoder", "Create media format: " + format); String mimeType = format.getString(MediaFormat.KEY_MIME); final MediaCodec encoder = createEncoder(mimeType); try { if (this.mCallback != null) { // NOTE: MediaCodec maybe crash on some devices due to null callback encoder.setCallback( mCodecCallback); } encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); onEncoderConfigured(encoder); encoder.start(); } catch (MediaCodec.CodecException e) { LogUtil.e("Encoder", "Configure codec failure!\n with format" + format, e); throw e; } mEncoder = encoder; }
Example 12
Source File: MediaCodecListCompat.java From android-transcoder with Apache License 2.0 | 5 votes |
private String findCoderForFormat(MediaFormat format, boolean findEncoder) { String mimeType = format.getString(MediaFormat.KEY_MIME); Iterator<MediaCodecInfo> iterator = new MediaCodecInfoIterator(); while (iterator.hasNext()) { MediaCodecInfo codecInfo = iterator.next(); if (codecInfo.isEncoder() != findEncoder) continue; if (Arrays.asList(codecInfo.getSupportedTypes()).contains(mimeType)) { return codecInfo.getName(); } } return null; }
Example 13
Source File: VideoUtil.java From VideoProcessor with Apache License 2.0 | 5 votes |
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: Mp4Provider.java From AAVT with Apache License 2.0 | 5 votes |
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 15
Source File: Track.java From K-Sonic with MIT License | 5 votes |
public void initConfig() throws Exception { if (mCurrentState == STATE_PLAYBACK_COMPLETED) { mExtractor.seekTo(((long) jMsec * 1000), MediaExtractor.SEEK_TO_CLOSEST_SYNC); //归零 jMsec = 0; } mLock.lock(); try { MediaFormat oFormat = mExtractor.getTrackFormat(TRACK_NUM); //oFormat NullPointerException int sampleRate = oFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE); int channelCount = oFormat.getInteger(MediaFormat.KEY_CHANNEL_COUNT); //Deal with mono ,, Some models do not support if (isJMono = channelCount == 1) oFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, channelCount = 2); String mime = oFormat.getString(MediaFormat.KEY_MIME); //Deal with mime ,, Some models will mime identification into ffmpeg if ("audio/ffmpeg".equals(mime)) oFormat.setString(MediaFormat.KEY_MIME, mime = "audio/mpeg"); mDuration = oFormat.getLong(MediaFormat.KEY_DURATION); Log.v(TAG_TRACK, "Sample rate: " + sampleRate); Log.v(TAG_TRACK, "Mime type: " + mime); initDevice(sampleRate, channelCount); mExtractor.selectTrack(TRACK_NUM); mCodec = MediaCodec.createDecoderByType(mime); mCodec.configure(oFormat, null, null, 0); } catch (Exception e) {//IOException throw e; } finally { mLock.unlock(); } }
Example 16
Source File: TrackTranscoderFactory.java From LiTr with BSD 2-Clause "Simplified" License | 4 votes |
/** * Create a proper transcoder for a given source track and target media format. * * @param sourceTrack source track id * @param mediaSource {@link MediaExtractor} for reading data from the source * @param mediaTarget {@link MediaTarget} for writing data to the target * @param targetFormat {@link MediaFormat} with target video track parameters, null if writing "as is" * @return implementation of {@link TrackTranscoder} for a given track */ @NonNull public TrackTranscoder create(int sourceTrack, int targetTrack, @NonNull MediaSource mediaSource, @Nullable Decoder decoder, @Nullable Renderer renderer, @Nullable Encoder encoder, @NonNull MediaTarget mediaTarget, @Nullable MediaFormat targetFormat) throws TrackTranscoderException { if (targetFormat == null) { return new PassthroughTranscoder(mediaSource, sourceTrack, mediaTarget, targetTrack); } String trackMimeType = targetFormat.getString(MediaFormat.KEY_MIME); if (trackMimeType == null) { throw new TrackTranscoderException(TrackTranscoderException.Error.SOURCE_TRACK_MIME_TYPE_NOT_FOUND, targetFormat, null, null); } if (trackMimeType.startsWith("video") || trackMimeType.startsWith("audio")) { if (decoder == null) { throw new TrackTranscoderException(TrackTranscoderException.Error.DECODER_NOT_PROVIDED, targetFormat, null, null); } else if (encoder == null) { throw new TrackTranscoderException(TrackTranscoderException.Error.ENCODER_NOT_PROVIDED, targetFormat, null, null); } } // TODO move into statement above when audio renderer is implemented if (trackMimeType.startsWith("video") && renderer == null) { throw new TrackTranscoderException(TrackTranscoderException.Error.RENDERER_NOT_PROVIDED, targetFormat, null, null); } if (trackMimeType.startsWith("video")) { return new VideoTrackTranscoder(mediaSource, sourceTrack, mediaTarget, targetTrack, targetFormat, renderer, decoder, encoder); } else if (trackMimeType.startsWith("audio")) { Decoder audioDecoder = new MediaCodecDecoder(); Encoder audioEncoder = new MediaCodecEncoder(); Renderer audioRenderer = renderer == null ? new PassthroughSoftwareRenderer(audioEncoder) : renderer; return new AudioTrackTranscoder(mediaSource, sourceTrack, mediaTarget, targetTrack, targetFormat, audioRenderer, audioDecoder, audioEncoder); } else { Log.i(TAG, "Unsupported track mime type: " + trackMimeType + ", will use passthrough transcoder"); return new PassthroughTranscoder(mediaSource, sourceTrack, mediaTarget, targetTrack); } }
Example 17
Source File: BaseTransformationFragment.java From LiTr with BSD 2-Clause "Simplified" License | 4 votes |
@NonNull protected void updateSourceMedia(@NonNull SourceMedia sourceMedia, @NonNull Uri uri) { sourceMedia.uri = uri; sourceMedia.size = TranscoderUtils.getSize(getContext(), uri); try { MediaExtractor mediaExtractor = new MediaExtractor(); mediaExtractor.setDataSource(getContext(), uri, null); sourceMedia.tracks = new ArrayList<>(mediaExtractor.getTrackCount()); for (int track = 0; track < mediaExtractor.getTrackCount(); track++) { MediaFormat mediaFormat = mediaExtractor.getTrackFormat(track); String mimeType = mediaFormat.getString(MediaFormat.KEY_MIME); if (mimeType == null) { continue; } if (mimeType.startsWith("video")) { VideoTrackFormat videoTrack = new VideoTrackFormat(track, mimeType); videoTrack.width = getInt(mediaFormat, MediaFormat.KEY_WIDTH); videoTrack.height = getInt(mediaFormat, MediaFormat.KEY_HEIGHT); videoTrack.duration = getLong(mediaFormat, MediaFormat.KEY_DURATION); videoTrack.frameRate = getInt(mediaFormat, MediaFormat.KEY_FRAME_RATE); videoTrack.keyFrameInterval = getInt(mediaFormat, MediaFormat.KEY_I_FRAME_INTERVAL); videoTrack.rotation = getInt(mediaFormat, KEY_ROTATION, 0); videoTrack.bitrate = getInt(mediaFormat, MediaFormat.KEY_BIT_RATE); sourceMedia.tracks.add(videoTrack); } else if (mimeType.startsWith("audio")) { AudioTrackFormat audioTrack = new AudioTrackFormat(track, mimeType); audioTrack.channelCount = getInt(mediaFormat, MediaFormat.KEY_CHANNEL_COUNT); audioTrack.samplingRate = getInt(mediaFormat, MediaFormat.KEY_SAMPLE_RATE); audioTrack.duration = getLong(mediaFormat, MediaFormat.KEY_DURATION); audioTrack.bitrate = getInt(mediaFormat, MediaFormat.KEY_BIT_RATE); sourceMedia.tracks.add(audioTrack); } else { sourceMedia.tracks.add(new GenericTrackFormat(track, mimeType)); } } } catch (IOException ex) { Log.e(TAG, "Failed to extract sourceMedia", ex); } sourceMedia.notifyChange(); }
Example 18
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 19
Source File: TranscodeVideoUtil.java From SimpleVideoEdit with Apache License 2.0 | 4 votes |
private static String getMimeTypeFor(MediaFormat format) { return format.getString(MediaFormat.KEY_MIME); }
Example 20
Source File: MediaConverter.java From mollyim-android with GNU General Public License v3.0 | 4 votes |
static String getMimeTypeFor(MediaFormat format) { return format.getString(MediaFormat.KEY_MIME); }