欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

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

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

10年積累的成都網(wǎng)站制作、成都做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(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說明

MediaMuser:將封裝編碼后的視頻流和音頻流到mp4容器中,說白了能夠?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.錄視頻過程

我先貼個(gè)圖,因?yàn)槲矣X得我后面會(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)建后,就打開攝像頭開始預(yù)覽
    camera = Camera.open();
    try {
        camera.setPreviewDisplay(surfaceHolder);
        camera.startPreview();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

然后開始錄視頻,開啟兩個(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數(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);
}

這個(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)換,也就是說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));

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

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

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

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、微信公眾號(hào)、小程序開發(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í)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)
91人妻人澡人人爽| 国产欧美日韩一二三四| 欧洲女人av天堂精品| 国产麻豆精品传媒av| 日本女优邻居人妻中文字幕| 特别黄的日本免费视频| 国产精品一级二区三区| 亚洲国产日韩在线精品| 亚洲av精二区三区四区| 男女啪啪国产精品视频| 国产精品偷拍自拍视频| 国产偷人伦激情在线观看| 99精品热视频在线观看| 丰满的少妇一区二区三区免费观看 | 日韩欧美亚洲视频另类| 亚洲黄色av网站在线| 欧美午夜精品福利在线观看| 91麻豆精品一区二区三区| 亚洲品质一区二区三区| 日韩国产传媒视频在线观看| 亚洲高清无毛一区二区| 91国产自拍在线视频| 国产欧美精品久久三级| 日韩人妻中文字幕乱码一区| 日本黄色高清视频一区| 亚洲精品最新地址久久久| 精品亚洲欧美日韩国产| 亚洲热久久国产经典视频| 成人午夜激情四射av| 亚洲av在线视频免费播放| 免费av在线观看日韩| 日韩av一区二区久久久| 日本精品av一区二区| 欧美黄色一区二区三区精品| 青青草原一区二区三区| 日韩高清中文字幕在线| 久久亚洲中文字幕精品熟女一区| 亚洲国产精品热久久网站| 色哟哟网站一区二区精品久久| 高清av在线国产成人精品自拍| 午夜福利中文在线观看|