如何计算 一个音视频文件 (.ts)文件 (包含pcr信息). 播放所使用的带宽

学习 专栏收录该内容
7 篇文章 0 订阅

以下是计算一个音视频文件,播放所需要带宽的程序:

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, 0188);
                    
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来计算播放所需要带宽 

                                                                                                                      filippo.le@gmail.com 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值