Android音視頻深入四錄視頻MP4(附源碼下載)-創(chuàng)新互聯(lián)

本篇項目地址,名字是《錄音視頻(有的播放器不能放,而且沒有時長顯示)》,求star

創(chuàng)新互聯(lián)公司2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目網(wǎng)站建設(shè)、做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元建鄴做網(wǎng)站,已為上家服務(wù),為建鄴各地企業(yè)和個人服務(wù),聯(lián)系電話:18980820575

https://github.com/979451341/Audio-and-video-learning-materials
1.MediaMuser說明

MediaMuser:將封裝編碼后的視頻流和音頻流到mp4容器中,說白了能夠?qū)⒁粢曨l整合成一個MP4文件,MediaMuxer最多僅支持一個視頻track和一個音頻track,所以如果有多個音頻track可以先把它們混合成為一個音頻track然后再使用MediaMuxer封裝到mp4容器中。

MediaMuxer muxer = new MediaMuxer("temp.mp4", OutputFormat.MUXER_OUTPUT_MPEG_4);
// More often, the MediaFormat will be retrieved from MediaCodec.getOutputFormat()
// or MediaExtractor.getTrackFormat().
MediaFormat audioFormat = new MediaFormat(...);
MediaFormat videoFormat = new MediaFormat(...);
int audioTrackIndex = muxer.addTrack(audioFormat);
int videoTrackIndex = muxer.addTrack(videoFormat);
ByteBuffer inputBuffer = ByteBuffer.allocate(bufferSize);
boolean finished = false;
BufferInfo bufferInfo = new BufferInfo();

muxer.start();
while(!finished) {
// getInputBuffer() will fill the inputBuffer with one frame of encoded
// sample from either MediaCodec or MediaExtractor, set isAudioSample to
// true when the sample is audio data, set up all the fields of bufferInfo,
// and return true if there are no more samples.
finished = getInputBuffer(inputBuffer, isAudioSample, bufferInfo);
if (!finished) {
int currentTrackIndex = isAudioSample ? audioTrackIndex : videoTrackIndex;
muxer.writeSampleData(currentTrackIndex, inputBuffer, bufferInfo);
}
};
muxer.stop();
muxer.release();

2.錄視頻過程

我先貼個圖,因為我覺得我后面會把自己繞暈,整理一下

先將Camera收集的數(shù)據(jù)顯示在SurfaceView

surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    Log.w("MainActivity", "enter surfaceCreated method");
    // 目前設(shè)定的是,當surface創(chuàng)建后,就打開攝像頭開始預(yù)覽
    camera = Camera.open();
    try {
        camera.setPreviewDisplay(surfaceHolder);
        camera.startPreview();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

然后開始錄視頻,開啟兩個線程分別處理音視頻數(shù)據(jù)

private void initMuxer() {
    muxerDatas = new Vector<>();
    fileSwapHelper = new FileUtils();
    audioThread = new AudioEncoderThread((new WeakReference<MediaMuxerThread>(this)));
    videoThread = new VideoEncoderThread(1920, 1080, new WeakReference<MediaMuxerThread>(this));
    audioThread.start();
    videoThread.start();
    try {
        readyStart();
    } catch (IOException e) {
        Log.e(TAG, "initMuxer 異常:" + e.toString());
    }
}

將兩個track加入MediaMuxer

mediaMuxer.writeSampleData(track, data.byteBuf, data.bufferInfo);

我們再來看看視頻數(shù)據(jù)如何處理的
MediaCodec初始化和配置

mediaFormat = MediaFormat.createVideoFormat(MIME_TYPE, this.mWidth, this.mHeight);
    mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE);
    mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, FRAME_RATE);
    mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar);
    mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);

開啟MediaCodec
mMediaCodec = MediaCodec.createByCodecName(mCodecInfo.getName());
mMediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mMediaCodec.start();

然后SurfaceView傳入視頻數(shù)據(jù)數(shù)據(jù)導(dǎo)入

@Override
public void onPreviewFrame(byte[] bytes, Camera camera) {
    MediaMuxerThread.addVideoFrameData(bytes);
}

這個數(shù)據(jù)MediaMuxerThread又傳給MediaThread

public void add(byte[] data) {
    if (frameBytes != null && isMuxerReady) {
        frameBytes.add(data);
    }
}

然后循環(huán)從frameBytes里取數(shù)據(jù)

if (!frameBytes.isEmpty()) {
byte[] bytes = this.frameBytes.remove(0);
Log.e("ang-->", "解碼視頻數(shù)據(jù):" + bytes.length);
try {
encodeFrame(bytes);
} catch (Exception e) {
Log.e(TAG, "解碼視頻(Video)數(shù)據(jù) 失敗");
e.printStackTrace();
}

取出的數(shù)據(jù)哪去轉(zhuǎn)換,也就是說mFrameData這個數(shù)據(jù)才是最后編碼出視頻

// 將原始的N21數(shù)據(jù)轉(zhuǎn)為I420
    NV21toI420SemiPlanar(input, mFrameData, this.mWidth, this.mHeight);

private static void NV21toI420SemiPlanar(byte[] nv21bytes, byte[] i420bytes, int width, int height) {
    System.arraycopy(nv21bytes, 0, i420bytes, 0, width * height);
    for (int i = width * height; i < nv21bytes.length; i += 2) {
        i420bytes[i] = nv21bytes[i + 1];
        i420bytes[i + 1] = nv21bytes[i];
    }
}

MediaCodec獲取數(shù)據(jù)從mFrameData

mMediaCodec.queueInputBuffer(inputBufferIndex, 0, mFrameData.length, System.nanoTime() / 1000, 0);

然后又拿出數(shù)據(jù)給muxer

mediaMuxer.addMuxerData(new MediaMuxerThread.MuxerData(MediaMuxerThread.TRACK_VIDEO, outputBuffer, mBufferInfo));

啊啊啊啊啊啊啊,瘋了,代碼可能看起來很糊,很多,但是絕大多數(shù)代碼是為了協(xié)調(diào)為了判斷當前還在錄視頻,但是真正的在錄視頻的代碼的運行情況就是兩條線,MediaCodec使用queueInputBuffer獲取數(shù)據(jù),然后進行編碼dequeueOutputBuffer給MediaMuxer,AudioCodec也是一樣的套路

源碼地址在文章首部,各位多多研究,對了這個代碼有問題,沒有顯示時長,有一些播放器不能用,手機自帶應(yīng)該沒問題

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

新聞標題:Android音視頻深入四錄視頻MP4(附源碼下載)-創(chuàng)新互聯(lián)
本文網(wǎng)址:http://bm7419.com/article6/hsiig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗網(wǎng)站營銷、網(wǎng)站設(shè)計、網(wǎng)站維護網(wǎng)站建設(shè)、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)