溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

視頻編解碼技術(shù)初探

發(fā)布時(shí)間:2020-07-14 21:56:07 來(lái)源:網(wǎng)絡(luò) 閱讀:468 作者:屠夫章哥 欄目:移動(dòng)開發(fā)
一、需要閱讀的文章
        https://blog.csdn.net/feifeiwendao/article/details/52527824

        ####MediaCodec類相關(guān)的文章:########
        https://www.cnblogs.com/renhui/p/7478527.html

        https://blog.csdn.net/junzia/article/details/54018671    (思路很清晰的一篇文章)

        https://www.cnblogs.com/roger-yu/p/5635494.html

        https://www.cnblogs.com/Sharley/p/5964490.html

        https://blog.csdn.net/leif_/article/details/50971616

        MediaCodec實(shí)際上就是一個(gè)編解碼的容器,將byte[]放進(jìn)去,取出來(lái)就得到自己想要的編碼格式的byte[],然后
        寫入文件即可。

        #########AudioRecorder錄音:###################
        https://blog.csdn.net/qq_36982160/article/details/79383046

        https://www.cnblogs.com/whoislcj/p/5477216.html

        錄音的流程:
        1)初始化AudioRecorder
        2)調(diào)用AudioRecorder的startRecording方法
        3)調(diào)用AudioRecorder的read方法,讀出byte[]數(shù)據(jù)。你可以選擇將數(shù)據(jù)存儲(chǔ)到文件,最終得到的是.pcm文件。
             這種文件一般播放器不支持,需要用andorid的AudioTrack去播放。

        AudioTrack,播放PCM音頻文件。
        https://www.cnblogs.com/stnlcd/p/7151438.html
        注意:AudioTrack的構(gòu)造參數(shù),要與錄制時(shí)的參數(shù)保持一致 。如channelConfig如果不對(duì)應(yīng)
        則人說(shuō)話的聲音就超級(jí)奇怪了。

        以下是PCM錄音和播放的源碼: 
package com.xinyi.czsuperrecorder;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaFormat;
import android.media.MediaRecorder;
import android.util.Log;

import com.xinyi.baselib.io.tf.TFileHelper;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by XinYi on 2018/7/5.
 * (錄音API)
 */
public class AudioRecorder {
    private static final String TAG = "AudioRecorder";
    private int sampleRate=44100;   //采樣率,默認(rèn)44.1k
    private int channelConfig= AudioFormat.CHANNEL_IN_STEREO;        //通道設(shè)置,默認(rèn)立體聲
    private int audioFormat=AudioFormat.ENCODING_PCM_16BIT;     //設(shè)置采樣數(shù)據(jù)格式,默認(rèn)16比特PCM
    private AudioRecord mRecorder;
    private int bufferSize;
    private File recordingFile = new File(TFileHelper.getInstance().getRoot() + "/test/a.pcm");

    public void prepare(){
        bufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat)*2;
        //        buffer=new byte[bufferSize];
        mRecorder=new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,
                audioFormat,bufferSize);
    }

    public void start() throws IOException {
        byte[] tempBuffer = new byte[bufferSize];
        if (mRecorder.getState() != AudioRecord.STATE_INITIALIZED) {
            stop();
            return;
        }

        //開始錄制
        mRecorder.startRecording();
        //循環(huán)讀取數(shù)據(jù)到buffer中,并保存buffer中的數(shù)據(jù)到文件中
        Log.e(TAG, "start: 錄音中。。。");

        TFileHelper.getInstance().deleteFile(recordingFile.getAbsolutePath());
        TFileHelper.getInstance().createFile(recordingFile.getAbsolutePath());
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(recordingFile);
            int length;
            while ((length = mRecorder.read(tempBuffer, 0, bufferSize)) != -1) {
                fileOutputStream.write(tempBuffer, 0, length);
                fileOutputStream.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void stop(){
        //中止循環(huán)并結(jié)束錄制
        mRecorder.stop();
        Log.e(TAG, "start: 錄音結(jié)束。。。");
    }

    //播放音頻(PCM)
    public void play()
    {
        DataInputStream dis=null;
        try {
            //從音頻文件中讀取聲音
            dis=new DataInputStream(new BufferedInputStream(new FileInputStream(recordingFile)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //最小緩存區(qū)
        int bufferSizeInBytes= AudioTrack.getMinBufferSize(sampleRate,AudioFormat.CHANNEL_OUT_MONO,AudioFormat.ENCODING_PCM_16BIT);
        //創(chuàng)建AudioTrack對(duì)象   依次傳入 :流類型、采樣率(與采集的要一致)、音頻通道(采集是IN 播放時(shí)OUT)、量化位數(shù)、最小緩沖區(qū)、模式
        /**
         * !!注意,音頻通道與錄制時(shí)的音頻通道要保持一致。
         */
        AudioTrack player=new AudioTrack(AudioManager.STREAM_MUSIC,sampleRate,AudioFormat.CHANNEL_OUT_STEREO,AudioFormat.ENCODING_PCM_16BIT, bufferSizeInBytes, AudioTrack.MODE_STREAM);

        byte[] data =new byte [bufferSizeInBytes];
        player.play();//開始播放
        while(true)
        {
            int i=0;
            try {
                while(dis.available()>0&&i<data.length)
                {
                    data[i]=dis.readByte();
                    i++;
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            player.write(data,0,data.length);

            if(i!=bufferSizeInBytes) //表示讀取完了
            {
                player.stop();//停止播放
                player.release();//釋放資源
                break;
            }
        }

    }

    public AudioRecord getmRecorder() {
        return mRecorder;
    }

    public int getBufferSize() {
        return bufferSize;
    }
}

二、緩沖區(qū)
https://blog.csdn.net/bzlj2912009596/article/details/75581675

三、音頻AAC編碼
關(guān)于AAC編碼的文章:
https://blog.csdn.net/jay100500/article/details/52955232/ (AAC的頭文件,介紹了一款工具可以查看aac文件的頭文件)

     音頻AAC編碼實(shí)現(xiàn)過(guò)程
     1)開啟音頻錄制
     2)通過(guò)MediaCodec,將音頻編碼,得到AAC裸流,加上AAC頭,然后再寫入文件。
     代碼如下:
         package com.xinyi.czsuperrecorder.code.audio;

import android.media.AudioRecord;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaFormat;
import android.util.Log;

import com.xinyi.baselib.io.tf.TFileHelper;
import com.xinyi.czsuperrecorder.Config;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * Created by XinYi on 2018/7/5.
 * AAC編碼
 */
public class AACEncoder {
    private static final String TAG = "AACEncoder";

    private String mime = "audio/mp4a-latm";    //錄音編碼的mime
    private int rate=256000;                    //編碼的key bit rate
    private  MediaCodec mEnc;
    private AudioRecorder audioRecorder;
    private File recordingFile = new File(TFileHelper.getInstance().getRoot() + "/test/a.m4a");
    private boolean isStopped = true;
    private int bufferSize;

    public AACEncoder(AudioRecorder audioRecorder) {
        this.audioRecorder = audioRecorder;
        isStopped = true;
    }

    public void prepare(){
        audioRecorder.prepare();
        try {
            bufferSize = 0;
            //相對(duì)于上面的音頻錄制,我們需要一個(gè)編碼器的實(shí)例
            MediaFormat format=MediaFormat.createAudioFormat(mime, Config.sampleRate,Config.channelCount);
            format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
            format.setInteger(MediaFormat.KEY_BIT_RATE, rate);
            mEnc= MediaCodec.createEncoderByType(mime);
            mEnc.configure(format,null,null,MediaCodec.CONFIGURE_FLAG_ENCODE);  //設(shè)置為編碼器
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void start() throws IOException {
//同樣,在設(shè)置錄音開始的時(shí)候,也要設(shè)置編碼開始
        mEnc.start();

//之前的音頻錄制是直接循環(huán)讀取,然后寫入文件,這里需要做編碼處理再寫入文件
//這里的處理就是和之前傳送帶取盒子放原料的流程一樣了,注意一般在子線程中循環(huán)處理
        isStopped = false;
        MediaCodec.BufferInfo mInfo=new MediaCodec.BufferInfo();
        FileOutputStream  fos = null;
        TFileHelper.getInstance().deleteFile(recordingFile.getAbsolutePath());
        TFileHelper.getInstance().createFile(recordingFile.getAbsolutePath());

        try {
            fos = new FileOutputStream(recordingFile);
            audioRecorder.start();

            while (!isStopped){     //循環(huán)讀取AudioRecorder的數(shù)據(jù)流
                int index=mEnc.dequeueInputBuffer(-1);
                Log.e(TAG, "start: index1 = " + index);
                if(index>=0){
                    final ByteBuffer buffer=mEnc.getInputBuffer(index);
                    buffer.clear();
                    int length=audioRecorder.getmRecorder().read(buffer,audioRecorder.getBufferSize());
                    Log.e(TAG, "start length = : " + length);
                    if(length>0){
                        mEnc.queueInputBuffer(index,0,length,System.nanoTime()/1000,0);
                    }
                }

                int outIndex;
                //每次取出的時(shí)候,把所有加工好的都循環(huán)取出來(lái)
                do{
                    outIndex=mEnc.dequeueOutputBuffer(mInfo,0);
                    Log.e(TAG, "start: index2 = " + outIndex);
                    if(outIndex>=0){
                        ByteBuffer buffer=mEnc.getOutputBuffer(outIndex);
                        buffer.position(mInfo.offset);
                        //AAC編碼,需要加數(shù)據(jù)頭,AAC編碼數(shù)據(jù)頭固定為7個(gè)字節(jié)
                        byte[] temp=new byte[mInfo.size+7];
                        buffer.get(temp,7,mInfo.size);
                        addADTStoPacket(temp,temp.length);
                        fos.write(temp);
                        mEnc.releaseOutputBuffer(outIndex,false);
                    }else if(outIndex ==MediaCodec.INFO_TRY_AGAIN_LATER){
                        //TODO something
                    }else if(outIndex==MediaCodec.INFO_OUTPUT_FORMAT_CHANGED){
                        //TODO something
                    }
                }while (outIndex>=0);
            }
            //編碼停止,發(fā)送編碼結(jié)束的標(biāo)志,循環(huán)結(jié)束后,停止并釋放編碼器
            mEnc.stop();
            mEnc.release();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MediaCodec.CryptoException e) {
            e.printStackTrace();
        } finally {
            if(fos != null){
                fos.close();
            }
        }
    }

    /**
     * 給編碼出的aac裸流添加adts頭字段
     * @param packet 要空出前7個(gè)字節(jié),否則會(huì)搞亂數(shù)據(jù)
     * @param packetLen
     */
    private void addADTStoPacket(byte[] packet, int packetLen) {
        int profile = 2;  //AAC LC
        int freqIdx = 4;  //44.1KHz
        int chanCfg = 2;  //CPE
        packet[0] = (byte)0xFF;
        packet[1] = (byte)0xF9;
        packet[2] = (byte)(((profile-1)<<6) + (freqIdx<<2) +(chanCfg>>2));
        packet[3] = (byte)(((chanCfg&3)<<6) + (packetLen>>11));
        packet[4] = (byte)((packetLen&0x7FF) >> 3);
        packet[5] = (byte)(((packetLen&7)<<5) + 0x1F);
        packet[6] = (byte)0xFC;
    }

    public void stop(){
        audioRecorder.stop();
        isStopped = true;
    }

}

四、視頻編碼
https://www.jianshu.com/p/f3a55d3d1f5d/ (攝像頭采集的數(shù)據(jù)格式Y(jié)UV)

五、MP4文件裁剪

1)mp4parser
https://github.com/sannies/mp4parser

https://blog.csdn.net/u012027644/article/details/53885837

https://blog.csdn.net/u014691453/article/details/53256605

https://blog.csdn.net/foryou96/article/details/64132636 (詳細(xì) )

此開源庫(kù)的缺點(diǎn)就是由開源庫(kù)裁剪或者合并出來(lái)的視頻文件,不能再由此開源庫(kù)進(jìn)行二次操作,否則會(huì)拋出異常。

2)FFmepg
https://www.jianshu.com/p/2cf527f2129f

六、視頻壓縮

  1. https://github.com/zerochl/FFMPEG-AAC-264-Android-32-64
  2. https://blog.csdn.net/w690333243/article/details/88591807
  3. https://blog.csdn.net/qq_21937107/article/details/80083380 (根據(jù)SilliCompresser改的,部分視頻可能壓縮不成功,有bug。)
    bug:https://stackoverflow.com/questions/36915383/what-does-error-code-1010-in-android-mediacodec-mean

  4. https://www.cnblogs.com/wainiwann/p/4633208.html (MediaMetadataRetriever視頻信息獲?。?/li>
  5. https://blog.csdn.net/chen930724/article/details/50267669 (MediaMetadataRetriever視頻信息獲?。?/li>
  6. https://blog.csdn.net/u014653815/article/details/81084161 (MediaCodec解碼)
  7. https://github.com/mabeijianxi/small-video-record
    https://www.jianshu.com/p/cdae476087d4
    8.https://www.cnblogs.com/wzqnxd/p/10038881.html (SiliCompressor,親測(cè)可用,缺點(diǎn)是沒有進(jìn)度監(jiān)聽)
    9.https://blog.csdn.net/dzzzheng95/article/details/60142379 (系統(tǒng)相機(jī)錄制視頻無(wú)效)

七、視頻錄制
調(diào)用系統(tǒng)相機(jī),很多API兼容性都不好,如前后攝像頭、輸出路徑,可能都不好使。

  1. https://www.jianshu.com/p/3f4ad878f6c8
  2. https://blog.csdn.net/nature_day/article/details/36889815 (音視頻Uri轉(zhuǎn)真實(shí)地址)

八、音頻

  1. 音頻格式:https://blog.csdn.net/love_xsq/article/details/51254777
  2. 播放音頻的幾種方式:https://www.cnblogs.com/HDK2016/p/8043247.html
  3. 暫停其他應(yīng)用的音頻:https://blog.csdn.net/franksunny/article/details/12224551
    https://blog.csdn.net/oLevin/article/details/51476122
    https://bbs.csdn.net/topics/391930743?page=1
  4. https://www.cnblogs.com/senior-engineer/p/7867626.html (mediaplayer詳解)
  5. https://blog.csdn.net/weixin_34015860/article/details/87948255 (音視頻播放器框架)
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI