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