本篇項目地址,名字是《錄音視頻(有的播放器不能放,並且沒有時長顯示)》,求stargit
https://github.com/979451341/Audio-and-video-learning-materials
1.MediaMuser說明github
MediaMuser:將封裝編碼後的視頻流和音頻流到mp4容器中,說白了可以將音視頻整合成一個MP4文件,MediaMuxer最多僅支持一個視頻track和一個音頻track,因此若是有多個音頻track能夠先把它們混合成爲一個音頻track而後再使用MediaMuxer封裝到mp4容器中。ide
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();this
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.錄視頻過程線程
我先貼個圖,由於我以爲我後面會把本身繞暈,整理一下code
先將Camera收集的數據顯示在SurfaceVieworm
surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this); @Override public 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加入MediaMuxerrem
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);
開啓MediaCodec
mMediaCodec = MediaCodec.createByCodecName(mCodecInfo.getName());
mMediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mMediaCodec.start();
而後SurfaceView傳入視頻數據數據導入
@Override public void onPreviewFrame(byte[] bytes, Camera camera) { MediaMuxerThread.addVideoFrameData(bytes); }
這個數據MediaMuxerThread又傳給MediaThread
public 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數據轉爲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獲取數據從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也是同樣的套路
源碼地址在文章首部,各位多多研究,對了這個代碼有問題,沒有顯示時長,有一些播放器不能用,手機自帶應該沒問題