以下是计算一个音视频文件,播放所需要带宽的程序:
public
int
getBitrate(String path)
...
{
File f = new File(path);
if (f.exists()) ...{
int mpegPackets = 0;
int getpcr = 0;
long pcr0 = 0;
long pcr1;
int pid = 0;
try ...{
FileInputStream in = new FileInputStream(f);
while (true) ...{
byte[] b = new byte[188];
int ret = in.read(b, 0, 188);
if (ret == -1)
break;
mpegPackets++;
if (b[0] != 0x47) ...{
return -1;
}
System.out.println("");
System.out.println(mpegPackets + " : ");
for(int k=0; k<ret; k++)...{
System.out.print(Integer.toHexString(b[k]&0xFF) + " ");
if(k%10 ==0)...{
System.out.println("");
}
}
if ((((b[3] >> 4) & 0x3) & 0x02) != 0) ...{//ptati判断adaptation_field_control =='10' || ='11')
//确定后面为adaptation_field
if (b[4] == 0) //adaptation_field_length = 0 则退出循环
continue;
if ((b[5] & 0x10) != 0) ...{ //对应 PCG_flag = 0 则表示此packet 不包含pcr 信息
if (getpcr == 10) ...{ //当找到第11个包含pcr 信息的packet
pid = ((b[1] & 0xff & 0x1f) << 8)
| (b[2] & 0xff);
int tmp;
pcr0 = ((((b[6] & 0xff) << 24)
+ ((b[7] & 0xff) << 16)
+ ((b[8] & 0xff) << 8) + (b[9] & 0xff)));
pcr0 <<= 1;
if ((b[10] & 0xff & 0x80) != 0)
pcr0 |= 1;
//pcr0 为 program_clock_reference_base (33bit)
pcr0 *= 300;
//OPCR(i) = OPCR_base(i)*300 + OPCR_ext(i)
tmp = ((b[10] & 0xff & 1) << 8) + b[11];
//tmp 为 program_clock_reference_extension (9bit)
pcr0 += tmp;
mpegPackets = 0;
}
if ((getpcr > 20)
&& pid == (((b[1] & 0xff & 0x1f) << 8) | (b[2] & 0xff))) ...{
//从第21个包含pcr信息的packet的包并且pid与"第11个包含pcr 信息的packet的pid"相同
int tmp;
pcr1 = ((((b[6] & 0xff) << 24)
+ ((b[7] & 0xff) << 16)
+ ((b[8] & 0xff) << 8) + (b[9] & 0xff)));
pcr1 <<= 1;
if ((b[10] & 0xff & 0x80) != 0)
pcr1 |= 1;
pcr1 *= 300;
tmp = ((b[10] & 0xff & 1) << 8) + b[11] & 0xff;
pcr1 += tmp;
long PCRdelta = pcr1 - pcr0;
double bitRate = ((40608 * mpegPackets * 1.0) / PCRdelta) * 1000000;
//40608 = 188 * 216
return (int) bitRate;
}
getpcr++;
}
}
}
} catch (FileNotFoundException e) ...{
e.printStackTrace();
} catch (IOException e) ...{
e.printStackTrace();
}
} else
return 0;
return 0;
}
File f = new File(path);
if (f.exists()) ...{
int mpegPackets = 0;
int getpcr = 0;
long pcr0 = 0;
long pcr1;
int pid = 0;
try ...{
FileInputStream in = new FileInputStream(f);
while (true) ...{
byte[] b = new byte[188];
int ret = in.read(b, 0, 188);
if (ret == -1)
break;
mpegPackets++;
if (b[0] != 0x47) ...{
return -1;
}
System.out.println("");
System.out.println(mpegPackets + " : ");
for(int k=0; k<ret; k++)...{
System.out.print(Integer.toHexString(b[k]&0xFF) + " ");
if(k%10 ==0)...{
System.out.println("");
}
}
if ((((b[3] >> 4) & 0x3) & 0x02) != 0) ...{//ptati判断adaptation_field_control =='10' || ='11')
//确定后面为adaptation_field
if (b[4] == 0) //adaptation_field_length = 0 则退出循环
continue;
if ((b[5] & 0x10) != 0) ...{ //对应 PCG_flag = 0 则表示此packet 不包含pcr 信息
if (getpcr == 10) ...{ //当找到第11个包含pcr 信息的packet
pid = ((b[1] & 0xff & 0x1f) << 8)
| (b[2] & 0xff);
int tmp;
pcr0 = ((((b[6] & 0xff) << 24)
+ ((b[7] & 0xff) << 16)
+ ((b[8] & 0xff) << 8) + (b[9] & 0xff)));
pcr0 <<= 1;
if ((b[10] & 0xff & 0x80) != 0)
pcr0 |= 1;
//pcr0 为 program_clock_reference_base (33bit)
pcr0 *= 300;
//OPCR(i) = OPCR_base(i)*300 + OPCR_ext(i)
tmp = ((b[10] & 0xff & 1) << 8) + b[11];
//tmp 为 program_clock_reference_extension (9bit)
pcr0 += tmp;
mpegPackets = 0;
}
if ((getpcr > 20)
&& pid == (((b[1] & 0xff & 0x1f) << 8) | (b[2] & 0xff))) ...{
//从第21个包含pcr信息的packet的包并且pid与"第11个包含pcr 信息的packet的pid"相同
int tmp;
pcr1 = ((((b[6] & 0xff) << 24)
+ ((b[7] & 0xff) << 16)
+ ((b[8] & 0xff) << 8) + (b[9] & 0xff)));
pcr1 <<= 1;
if ((b[10] & 0xff & 0x80) != 0)
pcr1 |= 1;
pcr1 *= 300;
tmp = ((b[10] & 0xff & 1) << 8) + b[11] & 0xff;
pcr1 += tmp;
long PCRdelta = pcr1 - pcr0;
double bitRate = ((40608 * mpegPackets * 1.0) / PCRdelta) * 1000000;
//40608 = 188 * 216
return (int) bitRate;
}
getpcr++;
}
}
}
} catch (FileNotFoundException e) ...{
e.printStackTrace();
} catch (IOException e) ...{
e.printStackTrace();
}
} else
return 0;
return 0;
}
以上是根据pcr来计算播放所需要带宽