Android音視頻深入四錄視頻MP4(附源碼下載)

本篇項(xiàng)目地址,名字是《錄音視頻(有的播放器不能放,而且沒(méi)有時(shí)長(zhǎng)顯示)》,求star

10年積累的成都網(wǎng)站制作、成都做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先做網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有沙依巴克免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

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

MediaMuser:將封裝編碼后的視頻流和音頻流到mp4容器中,說(shuō)白了能夠?qū)⒁粢曨l整合成一個(gè)MP4文件,MediaMuxer最多僅支持一個(gè)視頻track和一個(gè)音頻track,所以如果有多個(gè)音頻track可以先把它們混合成為一個(gè)音頻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.錄視頻過(guò)程

我先貼個(gè)圖,因?yàn)槲矣X(jué)得我后面會(huì)把自己繞暈,整理一下

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

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

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

然后開(kāi)始錄視頻,開(kāi)啟兩個(gè)線程分別處理音視頻數(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());
    }
}

將兩個(gè)track加入MediaMuxer

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

我們?cè)賮?lái)看看視頻數(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);

開(kāi)啟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);
}

這個(gè)數(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)換,也就是說(shuō)mFrameData這個(gè)數(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));

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

源碼地址在文章首部,各位多多研究,對(duì)了這個(gè)代碼有問(wèn)題,沒(méi)有顯示時(shí)長(zhǎng),有一些播放器不能用,手機(jī)自帶應(yīng)該沒(méi)問(wèn)題

網(wǎng)頁(yè)標(biāo)題:Android音視頻深入四錄視頻MP4(附源碼下載)
URL地址:http://bm7419.com/article34/iihjse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、微信公眾號(hào)、小程序開(kāi)發(fā)企業(yè)網(wǎng)站制作、移動(dòng)網(wǎng)站建設(shè)、

廣告

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

h5響應(yīng)式網(wǎng)站建設(shè)