2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
7 import java.util.ArrayList;
9 import java.util.logging.Level;
10 import java.util.logging.Logger;
11 import jtssplitter.data.PATData;
12 import jtssplitter.data.PIDs;
13 import jtssplitter.data.PMTData;
16 * @author Administrator
19 public void readTs(String fpath){
20 byte[] tb=new byte[188];
21 int[] ib=new int[188];
22 ArrayList<PATData> pat=new ArrayList<PATData>();
23 ArrayList<PMTData> pmt=new ArrayList<PMTData>();
24 String[] sb=new String[188];
26 FileInputStream in = new FileInputStream(fpath);
27 BufferedInputStream bis=new BufferedInputStream(in);
28 int[] PIDCount=new int[8200];
29 for (int i=0;i<3000;i++){
33 m2tp=new Mpeg2TSPacket();
39 PIDCount[m2tp.getPID()]++;
41 for (int i=0;i<8200;i++){
45 for (int i=0;i<188;i++){
48 String s=Integer.toBinaryString(it);
50 for (int i2=s.length();i2<8;i2++){
58 } catch (IOException ex) {
59 Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
63 public void splitTS(String origpath,String destpath,int csch){
69 p_table=getFirstP_Table(origpath);
71 PIDs pidss=getTablePID(origpath,p_table);
72 p_table=pidss.Program_Table;
74 byte[] tb=new byte[188];
76 FileInputStream in = new FileInputStream(origpath);
77 FileOutputStream fos=new FileOutputStream(destpath);
78 BufferedInputStream bis=new BufferedInputStream(in,128*188);
79 BufferedOutputStream bos=new BufferedOutputStream(fos);
83 boolean readend=false;
85 m2tpp=new Mpeg2TSPacket();
86 while (readend==false){
87 byte[] tstt=new byte[3];
93 m2tp=new Mpeg2TSPacket();
94 int ii=m2tpp.getPIDFirst(tstt);
95 boolean alreadyreaded=false;
96 for (int k=0;k<pids.length;k++){
99 int readti=bis.read(tb);
100 if ((ii==0)&&(readti>187)){
101 //Mpeg2TSPacket m2tp2=new Mpeg2TSPacket();
102 wbyte=m2tpp.splitPAT(tb,p_table);
103 }else if (readti>187){
112 if (alreadyreaded==false){
115 int bbb=bis.available();
116 //if (bis.available()<188){
129 } catch (IOException ex) {
130 Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
133 private void writeTS(byte[] tspacket,String path){
134 FileOutputStream fos = null;
136 fos = new FileOutputStream(path,true);
139 } catch (IOException ex) {
140 Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
144 } catch (IOException ex) {
145 Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
149 private PIDs getTablePID(String fpath,int p_table){
151 * PIDを取得し、これと思われる必要なPIDを抜き出す。
152 * @return プログラム番号(return[0])とPIDのリスト(return[1-])
154 FileInputStream in = null;
155 PIDs pids=new PIDs();
158 ArrayList<Integer> ret=new ArrayList<Integer>();
159 byte[] tb = new byte[188];
160 int[] ib = new int[188];
161 ArrayList<PATData> pat = new ArrayList<PATData>();
162 ArrayList<PMTData> pmt = new ArrayList<PMTData>();
163 String[] sb = new String[188];
164 in = new FileInputStream(fpath);
165 //int[] PIDCount = new int[8200];
167 for (int i = 0; i < imax ; i++) {
171 m2tp = new Mpeg2TSPacket();
176 for (int i2=0;i2<pat.size();i2++){
177 PATData patdd=pat.get(i2);
178 if (pat.get(i2).Program_TABLE==p_table){
184 ArrayList<PMTData> pmtt=m2tp.getPMT();
185 for (int i2=0;i2<pmtt.size();i2++){
186 if (!(pmt.contains(pmtt.get(i2)))){
187 pmt.add(pmtt.get(i2));
190 if ((i==imax-1)&&(pmt.size()==0)){
193 for (int i2=0;i2<pmt.size();i2++){
195 if (p_table==pmt.get(i2).Program_Table){
196 if (!(ret.contains(pmt.get(i2).Elementary_PID))){
197 ret.add(pmt.get(i2).Elementary_PID);
201 if (ret.size()==itt){
206 PATData patd_det=new PATData();
207 for (int i=0;i<pat.size();i++){
208 if (pat.get(i).Program_TABLE==p_table){
214 ret.add(patd_det.PID);
215 for (int i=0;i<pmt.size();i++){
216 if (patd_det.Program_TABLE==pmt.get(i).Program_Table){
217 if (!(ret.contains(pmt.get(i).Elementary_PID))){
218 ret.add(pmt.get(i).Elementary_PID);
222 pids.Program_Table=patd_det.Program_TABLE;
223 pids.PIDs=new int[ret.size()];
224 for (int i=0;i<ret.size();i++){
225 pids.PIDs[i]=ret.get(i);
228 } catch (IOException ex) {
229 Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
233 } catch (IOException ex) {
234 Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
239 private int getFirstP_Table(String fpath){
241 * PIDを取得し、これと思われる必要なPIDを抜き出す。
242 * @return プログラム番号(return[0])とPIDのリスト(return[1-])
244 FileInputStream in = null;
246 int[] pat_det=new int[2];
248 PATData patdd=new PATData();
251 ArrayList ret=new ArrayList();
252 byte[] tb = new byte[188];
253 int[] ib = new int[188];
254 ArrayList<PATData> pat = new ArrayList<PATData>();
255 ArrayList<PMTData> pmt = new ArrayList<PMTData>();
256 String[] sb = new String[188];
257 in = new FileInputStream(fpath);
258 //int[] PIDCount = new int[8200];
260 for (int i = 0; i < imax ; i++) {
264 m2tp = new Mpeg2TSPacket();
270 if ((i==imax-1)&&(pmt.size()==0)){
275 for (int i=0;i<pat.size();i++){
276 if (pat.get(i).Program_TABLE>0){
281 } catch (IOException ex) {
282 Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
286 } catch (IOException ex) {
287 Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
290 return patdd.Program_TABLE;