C#中讀取流媒體視頻文件轉H.264具體實現方法

現在有越來越多的人在使用C#語言做編程,但我發現好像用C#做音視頻流媒體開發的比較少。我們的libEasyScreenLive目前支持Windows,Android平臺,通過EasyScreenLive我們就可以避免接觸到稍顯複雜的音視頻源採集,編碼和流媒體推送以及RTSP/RTP/RTCP/RTMP服務流程。

本文給大家介紹一下C#中讀取流媒體視頻文件轉H.264具體實現方法。

private void Test()
        {
            byte[] buffer;
            //c#文件流讀文件 
            using (FileStream fsRead = new FileStream(videoName, FileMode.Open, FileAccess.Read))
            {
                int fsLen = (int)fsRead.Length;
                buffer = new byte[fsLen];
                int r = fsRead.Read(buffer, 0, buffer.Length);
            } 

            PsToH264(buffer);
        }

public void PsToH264(byte[] buffer)
        {
            _publicByte = copybyte(_publicByte, buffer);
            int i = 0;
            int BANum = 0;
            int startIndex = 0;
            if (buffer == null || buffer.Length < 5)
            {
                return;
            }
            int bytes = _publicByte.Length - 4;
            while (i < bytes)
            {
                if (_publicByte[i] == 0x00 && _publicByte[i + 1] == 0x00 && _publicByte[i + 2] == 0x01 && _publicByte[i + 3] == 0xBA)
                {
                    BANum++;
                    if (BANum == 1)
                    {
                        startIndex = i;
                    }
                    if (BANum == 2)
                    {
                        break;
                    }
                }
                i++;
            }

            if (BANum == 2)
            {
                int esNum = i - startIndex;
                byte[] psByte = new byte[esNum];
                Array.Copy(_publicByte, startIndex, psByte, 0, esNum);

                try
                {
                    //處理psByte
                    doPsByte(psByte);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("===============" + ex.Message + ex.StackTrace.ToString());
                }

                byte[] overByte = new byte[_publicByte.Length - i];
                Array.Copy(_publicByte, i, overByte, 0, overByte.Length);
                _publicByte = overByte;
            }
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章