Search in sources :

Example 21 with MediaFormat

use of android.media.MediaFormat in project Pix-Art-Messenger by kriztan.

the class MediaExtractorUtils method getFirstVideoAndAudioTrack.

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;
}
Also used : MediaFormat(android.media.MediaFormat)

Example 22 with MediaFormat

use of android.media.MediaFormat in project Pix-Art-Messenger by kriztan.

the class AudioTrackTranscoder method setup.

@Override
public void setup() {
    mExtractor.selectTrack(mTrackIndex);
    try {
        mEncoder = MediaCodec.createEncoderByType(mOutputFormat.getString(MediaFormat.KEY_MIME));
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    mEncoder.configure(mOutputFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    mEncoder.start();
    mEncoderStarted = true;
    mEncoderBuffers = new MediaCodecBufferCompatWrapper(mEncoder);
    final MediaFormat inputFormat = mExtractor.getTrackFormat(mTrackIndex);
    try {
        mDecoder = MediaCodec.createDecoderByType(inputFormat.getString(MediaFormat.KEY_MIME));
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    mDecoder.configure(inputFormat, null, null, 0);
    mDecoder.start();
    mDecoderStarted = true;
    mDecoderBuffers = new MediaCodecBufferCompatWrapper(mDecoder);
    mAudioChannel = new AudioChannel(mDecoder, mEncoder, mOutputFormat);
}
Also used : MediaFormat(android.media.MediaFormat) MediaCodecBufferCompatWrapper(net.ypresto.androidtranscoder.compat.MediaCodecBufferCompatWrapper) IOException(java.io.IOException)

Example 23 with MediaFormat

use of android.media.MediaFormat in project Pix-Art-Messenger by kriztan.

the class MediaTranscoderEngine method setupTrackTranscoders.

private void setupTrackTranscoders(MediaFormatStrategy formatStrategy) {
    MediaExtractorUtils.TrackResult trackResult = MediaExtractorUtils.getFirstVideoAndAudioTrack(mExtractor);
    MediaFormat videoOutputFormat = formatStrategy.createVideoOutputFormat(trackResult.mVideoTrackFormat);
    MediaFormat audioOutputFormat = formatStrategy.createAudioOutputFormat(trackResult.mAudioTrackFormat);
    if (videoOutputFormat == null && audioOutputFormat == null) {
        throw new InvalidOutputFormatException("MediaFormatStrategy returned pass-through for both video and audio. No transcoding is necessary.");
    }
    QueuedMuxer queuedMuxer = new QueuedMuxer(mMuxer, new QueuedMuxer.Listener() {

        @Override
        public void onDetermineOutputFormat() {
            MediaFormatValidator.validateVideoOutputFormat(mVideoTrackTranscoder.getDeterminedFormat());
            MediaFormatValidator.validateAudioOutputFormat(mAudioTrackTranscoder.getDeterminedFormat());
        }
    });
    if (videoOutputFormat == null) {
        mVideoTrackTranscoder = new PassThroughTrackTranscoder(mExtractor, trackResult.mVideoTrackIndex, queuedMuxer, QueuedMuxer.SampleType.VIDEO);
    } else {
        mVideoTrackTranscoder = new VideoTrackTranscoder(mExtractor, trackResult.mVideoTrackIndex, videoOutputFormat, queuedMuxer);
    }
    mVideoTrackTranscoder.setup();
    if (audioOutputFormat == null) {
        mAudioTrackTranscoder = new PassThroughTrackTranscoder(mExtractor, trackResult.mAudioTrackIndex, queuedMuxer, QueuedMuxer.SampleType.AUDIO);
    } else {
        mAudioTrackTranscoder = new AudioTrackTranscoder(mExtractor, trackResult.mAudioTrackIndex, audioOutputFormat, queuedMuxer);
    }
    mAudioTrackTranscoder.setup();
    mExtractor.selectTrack(trackResult.mVideoTrackIndex);
    mExtractor.selectTrack(trackResult.mAudioTrackIndex);
}
Also used : MediaFormat(android.media.MediaFormat) MediaExtractorUtils(net.ypresto.androidtranscoder.utils.MediaExtractorUtils)

Example 24 with MediaFormat

use of android.media.MediaFormat in project Pix-Art-Messenger by kriztan.

the class Android16By9FormatStrategy method createAudioOutputFormat.

@Override
public MediaFormat createAudioOutputFormat(MediaFormat inputFormat) {
    if (mAudioBitrate == AUDIO_BITRATE_AS_IS || mAudioChannels == AUDIO_CHANNELS_AS_IS)
        return null;
    // Use original sample rate, as resampling is not supported yet.
    final MediaFormat format = MediaFormat.createAudioFormat(MediaFormatExtraConstants.MIMETYPE_AUDIO_AAC, inputFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE), mAudioChannels);
    format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
    format.setInteger(MediaFormat.KEY_BIT_RATE, mAudioBitrate);
    return format;
}
Also used : MediaFormat(android.media.MediaFormat)

Example 25 with MediaFormat

use of android.media.MediaFormat in project Pix-Art-Messenger by kriztan.

the class Android16By9FormatStrategy method createVideoOutputFormat.

@Override
public MediaFormat createVideoOutputFormat(MediaFormat inputFormat) {
    int width = inputFormat.getInteger(MediaFormat.KEY_WIDTH);
    int height = inputFormat.getInteger(MediaFormat.KEY_HEIGHT);
    int targetLonger = mScale * 16 * 16;
    int targetShorter = mScale * 16 * 9;
    int longer, shorter, outWidth, outHeight;
    if (width >= height) {
        longer = width;
        shorter = height;
        outWidth = targetLonger;
        outHeight = targetShorter;
    } else {
        shorter = width;
        longer = height;
        outWidth = targetShorter;
        outHeight = targetLonger;
    }
    if (longer * 9 != shorter * 16) {
        throw new OutputFormatUnavailableException("This video is not 16:9, and is not able to transcode. (" + width + "x" + height + ")");
    }
    if (shorter <= targetShorter) {
        Log.d(TAG, "This video's height is less or equal to " + targetShorter + ", pass-through. (" + width + "x" + height + ")");
        return null;
    }
    MediaFormat format = MediaFormat.createVideoFormat("video/avc", outWidth, outHeight);
    // From Nexus 4 Camera in 720p
    format.setInteger(MediaFormat.KEY_BIT_RATE, mVideoBitrate);
    format.setInteger(MediaFormat.KEY_FRAME_RATE, 30);
    format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 3);
    format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
    return format;
}
Also used : MediaFormat(android.media.MediaFormat)

Aggregations

MediaFormat (android.media.MediaFormat)130 IOException (java.io.IOException)29 MediaCodec (android.media.MediaCodec)24 ByteBuffer (java.nio.ByteBuffer)22 MediaExtractor (android.media.MediaExtractor)18 SuppressLint (android.annotation.SuppressLint)16 Handler (android.os.Handler)15 Test (org.junit.Test)14 TargetApi (android.annotation.TargetApi)13 InputStream (java.io.InputStream)12 Context (android.content.Context)10 HandlerThread (android.os.HandlerThread)10 Message (android.os.Message)10 Pair (android.util.Pair)10 Format (com.google.android.exoplayer2.Format)10 Runnable (java.lang.Runnable)10 Nullable (androidx.annotation.Nullable)9 File (java.io.File)9 BufferInfo (android.media.MediaCodec.BufferInfo)8 Surface (android.view.Surface)8