日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 音视频深入 四 录视频MP4(附源码下载)

發布時間:2023/12/15 Android 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 音视频深入 四 录视频MP4(附源码下载) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

https://github.com/979451341/Audio-and-video-learning-materials

1.MediaMuser說明


MediaMuser:將封裝編碼后的視頻流和音頻流到mp4容器中,說白了能夠將音視頻整合成一個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收集的數據顯示在SurfaceView

surfaceHolder = surfaceView.getHolder();surfaceHolder.addCallback(this);@Overridepublic void surfaceCreated(SurfaceHolder surfaceHolder) {Log.w("MainActivity", "enter surfaceCreated method");// 目前設定的是,當surface創建后,就打開攝像頭開始預覽camera = Camera.open();try {camera.setPreviewDisplay(surfaceHolder);camera.startPreview();} catch (IOException e) {e.printStackTrace();}}


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

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);

我們再來看看視頻數據如何處理的
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);開啟MediaCodecmMediaCodec = MediaCodec.createByCodecName(mCodecInfo.getName());mMediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);mMediaCodec.start();

然后SurfaceView傳入視頻數據數據導入

@Overridepublic void onPreviewFrame(byte[] bytes, Camera camera) {MediaMuxerThread.addVideoFrameData(bytes);}這個數據MediaMuxerThread又傳給MediaThreadpublic void add(byte[] data) {if (frameBytes != null && isMuxerReady) {frameBytes.add(data);}}

然后循環從frameBytes里取數據

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

取出的數據哪去轉換,也就是說mFrameData這個數據才是最后編碼出視頻

// 將原始的N21數據轉為I420NV21toI420SemiPlanar(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獲取數據從mFrameData

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


然后又拿出數據給muxer

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


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

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

轉載于:https://www.cnblogs.com/jianpanwuzhe/p/8409064.html

總結

以上是生活随笔為你收集整理的Android 音视频深入 四 录视频MP4(附源码下载)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。