salut tt le monde, j'avoue que ce problème persite depuis longtemps, j'arrive à calculer le bitrate d'un video depuis un serveur FTP, le problème c'est que mon code marche pour les petits video de test ( [8 Mo-70 Mo]) . une fois j'ai migrer vers les videos de production (films warner >=3 GO) alors là ça plante pas mais ca demande plus de 45 mn (je dois parcourir tt le video) pour calculer ce bitrate qui est nécessaire pour caculer la durée du video. c'est quelqu'un peut m'aider à calcluer le BitRate sans parcourir tt le video je serais trés reconnaissant.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
 
 // This method allows calculate file bitrate :
    public static double CalculateBitRate(string _sFileName)
    {
        //Create FTP request
        FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(ReturnURI(_sFileName));
        reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
        reqFTP.Credentials = new NetworkCredential(StreamMediaInfo.sFTPServerUser, StreamMediaInfo.FTPServerPassword);
        reqFTP.UseBinary = true;
        reqFTP.UsePassive = false;
        reqFTP.KeepAlive = true;
        int _iBufferSize = (int)reqFTP.GetResponse().ContentLength;
 
        //Now get the actual data
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(ReturnURI(_sFileName));
        reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
        reqFTP.Credentials = new NetworkCredential(StreamMediaInfo.sFTPServerUser, StreamMediaInfo.FTPServerPassword);
        reqFTP.UseBinary = true;
        reqFTP.UsePassive = false;
        reqFTP.KeepAlive = false;
 
        //Stream
        FtpWebResponse _FtpWebResponse = (FtpWebResponse)reqFTP.GetResponse();
        Stream reader = _FtpWebResponse.GetResponseStream();
        MemoryStream memStream = new MemoryStream();      
 
        // Calculate Bitrate
        byte[] acPacket = new byte[Constants.TS_PACKET_SIZE];
        int nPID = 0;
        int nBytesRead = 0;
        int nPCRExtension1 = 0;
        int nPCRExtension2 = 0;
        int nPCRPID = 0;
        ulong nNumPackets = 0;
        ulong nLength = 0;
        ulong nIndex = 0;
        ulong nNum = 0;
        ulong nDen = 0;
        double dBitRateVideo = -1;
        ulong nPCR1 = 0;
        ulong nPCR2 = 0;
        ulong nPCR1D = 0;
        ulong nPCR2D = 0;
        ulong nPacket1 = 0;
        ulong nPacket2 = 0;
        long nPCRBase1 = 0;
        long nPCRBase2 = 0;
        bool bPCRCalculation = false;
        byte[] buffer = new byte[Constants.TS_PACKET_SIZE];
        nLength = (ulong)_iBufferSize;
        nNumPackets =nLength / Constants.TS_PACKET_SIZE;
 
        nPCRPID = StreamMediaInfo.CalculatePCRPID(_sFileName);
        if (nPCRPID < 0)
        {
            return -1;
        }
        int i = 0;
        while (true)
        {
            //Try to read the data
            int bytesRead = reader.Read(buffer, 0, buffer.Length);
            if (bytesRead == 0)
            {
                break;
            }
            else if(bytesRead==188)
            {
                if (nIndex < nNumPackets)
                {
                    //Write the downloaded data
 
                    memStream.Write(buffer, 0, bytesRead);
                    memStream.Seek(0, SeekOrigin.Begin);
                    acPacket = memStream.ToArray();
                    nBytesRead = acPacket.Length;
 
                    if (nBytesRead != Constants.TS_PACKET_SIZE)
                    {
                        //STR_INVALID_PCR_PID_ERROR
                        reader.Close();
                        memStream.Close();
                        _FtpWebResponse.Close();
                    }
 
                    nPID = 0xFF & acPacket[2];
                    nPID = nPID | ((acPacket[1] & 0x1F) << 8);
 
                    // dBitRateVideo = nPID;
                    if (nPID == nPCRPID)
                    {
                        if (((acPacket[3] & 0x30) == 0x20) || ((acPacket[3] & 0x30) == 0x30))
                        {
                            //It has adaption field
                            if (acPacket[4] != 0x00)
                            {
                                //adaption_field_length != 0
                                if ((acPacket[5] & 0x10) == 0x10)
                                {
                                    //PCR_flag = 1
                                    if (bPCRCalculation == false)
                                    {
                                        nPCRBase1 = 0;
                                        nPCRBase1 = nPCRBase1 | (0xFF & acPacket[9]);
                                        nPCRBase1 = nPCRBase1 | ((0xFF & acPacket[8]) << 8);
                                        nPCRBase1 = nPCRBase1 | ((0xFF & acPacket[7]) << 16);
                                        nPCRBase1 = nPCRBase1 | (0xFF000000 & ((0xFF & acPacket[6]) << 24));
                                        nPCRBase1 = nPCRBase1 << 1;
                                        nPCRBase1 = nPCRBase1 | ((acPacket[10] >> 7) & 0x01);
                                        bPCRCalculation = true;
                                        nPacket1 = nIndex;
 
                                        nPCRExtension1 = 0;
                                        nPCRExtension1 = nPCRExtension1 | (0xFF & acPacket[11]);
                                        nPCRExtension1 = nPCRExtension1 | ((0x01 & acPacket[10]) << 8);
 
                                        nPCR1 = (ulong)(nPCRBase1 * Constants.TIME_UNIT_BASE_PCR + nPCRExtension1 * Constants.TIME_UNIT_EXT_PCR);
                                        nPCR1D = (ulong)(nPCRBase1 * 300 + nPCRExtension1);
                                    }
                                    else
                                    {
                                        nPCRBase2 = 0;
                                        nPCRBase2 = nPCRBase2 | (0xFF & acPacket[9]);
                                        nPCRBase2 = nPCRBase2 | ((0xFF & acPacket[8]) << 8);
                                        nPCRBase2 = nPCRBase2 | ((0xFF & acPacket[7]) << 16);
                                        nPCRBase2 = nPCRBase2 | (0xFF000000 & ((0xFF & acPacket[6]) << 24));
                                        nPCRBase2 = nPCRBase2 << 1;
                                        nPCRBase2 = nPCRBase2 | ((acPacket[10] >> 7) & 0x01);
                                        nPacket2 = nIndex;
 
                                        nPCRExtension2 = 0;
                                        nPCRExtension2 = nPCRExtension2 | (0xFF & acPacket[11]);
                                        nPCRExtension2 = nPCRExtension2 | ((0x01 & acPacket[10]) << 8);
 
                                        nPCR2 = (ulong)(nPCRBase2 * Constants.TIME_UNIT_BASE_PCR + nPCRExtension2 * Constants.TIME_UNIT_EXT_PCR);
                                        nPCR2D = (ulong)(nPCRBase2 * 300 + nPCRExtension2);
                                        nNum = (nPacket2 - nPacket1) * Constants.TS_PACKET_SIZE * 8;
                                        nDen = nPCR2 - nPCR1;
                                        if (nDen < 100)
                                        {
                                            //dBitRateVideo = (double)(nNum / nDen);
                                            nDen = nPCR2D - nPCR1D;
                                            dBitRateVideo = ((double)nNum / (double)nDen) * Constants.SYSTEM_CLOCK_FREQUENCY;
                                            continue;
                                        }
 
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
 
            }
            else if (bytesRead < 188)
            {
                i++;
            }
            nIndex++;
        }
        reader.Close();
        memStream.Close();
        _FtpWebResponse.Close();
        return dBitRateVideo;   
    }
 
    // This method allows calculate video duration 
    public static double CalculateVideoDurationS(string _sFileName)
    {
        //Create FTP request
        FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(ReturnURI(_sFileName));
        reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
        reqFTP.Credentials = new NetworkCredential(StreamMediaInfo.sFTPServerUser, StreamMediaInfo.FTPServerPassword);
        reqFTP.UseBinary = true;
        reqFTP.UsePassive = false;
        reqFTP.KeepAlive = true;
        int _iBufferSize = (int)reqFTP.GetResponse().ContentLength;
 
        // Calculate VideoDuration
        ulong dLength = 0;
        double dDuration = -1;
        dLength = (ulong)_iBufferSize;
        double _dBitrate = StreamMediaInfo.CalculateBitRate(_sFileName);
        if (_dBitrate == -1)
        {
            return -1;
        }
        dDuration = dLength * 8 / _dBitrate;
        return dDuration;
    }