OSDN Git Service

Ts Split Software Alpha(Not usable yet).
authorgn64_jp <gn64_jp@4e526526-5e11-4fc0-8910-f8fd03428081>
Sun, 25 Oct 2009 13:07:53 +0000 (13:07 +0000)
committergn64_jp <gn64_jp@4e526526-5e11-4fc0-8910-f8fd03428081>
Sun, 25 Oct 2009 13:07:53 +0000 (13:07 +0000)
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/rec10@186 4e526526-5e11-4fc0-8910-f8fd03428081

jTsSplitter/trunk/src/jtssplitter/Main.java
jTsSplitter/trunk/src/jtssplitter/Mpeg2TSPacket.java
jTsSplitter/trunk/src/jtssplitter/Tsfile.java

index 118b932..3231323 100644 (file)
@@ -17,7 +17,8 @@ public class Main {
     public static void main(String[] args) {
         // TODO code application logic here
         Tsfile t1=new Tsfile();
-        t1.readTs("D:\\jTssplitter\\test.ts.b25");
+        //t1.readTs("D:\\jTssplitter\\test.ts.b25");
+        t1.splitTS("D:\\jTssplitter\\test.ts", "D:\\jTssplitter\\test-sp.ts",0);
     }
 
 }
index 67b56d6..0da8fb2 100644 (file)
@@ -84,16 +84,16 @@ public class Mpeg2TSPacket {
             }
         }
         if (PID==0){
-            System.out.print("Pointer field ? : "+pointer_field+"\n");
+            //System.out.print("Pointer field ? : "+pointer_field+"\n");
             pat_list_now=readPAT(payload);
             pat_list_all.addAll(pat_list_now);
             //System.out.print("ここから変更済み\n");
-            readPAT(changePAT(payload));
+            //readPAT(changePAT(payload));
         }
         for (int i=0;i<pat_list_now.size();i++){
             int[] pid_temp=(int[])pat_list_now.get(i);
             if ((PID==pid_temp[1])&&(PID!=0)){
-                System.out.println("PMT PID : 0x"+Integer.toHexString(PID));
+                //System.out.println("PMT PID : 0x"+Integer.toHexString(PID));
 
                 pmt_list.addAll(readPMT(payload,pid_temp[0]));
             }
@@ -126,18 +126,18 @@ public class Mpeg2TSPacket {
         }
         payload_temp=stest;
         int nowbit=64;
-            System.out.print(Integer.toString(patnum)+"\n");
+            //System.out.print(Integer.toString(patnum)+"\n");
         for (int i=0;i<patnum;i++){
-            System.out.print(payload_temp.substring(64+32*i,95+32*i+1)+"\n");
+            //System.out.print(payload_temp.substring(64+32*i,95+32*i+1)+"\n");
             int[] pat=new int[2];
             pat[0]=TSString2Int(payload_temp,64+32*i,16);
             pat[1]=TSString2Int(payload_temp,32*i+64+19, 13);
-            System.out.print("TABLE : "+Integer.toString(pat[0])+"\n");
-            System.out.print("PMT PID : "+Integer.toString(pat[1])+"\n");
+            //System.out.print("TABLE : "+Integer.toString(pat[0])+"\n");
+            //System.out.print("PMT PID : "+Integer.toString(pat[1])+"\n");
             program_number.add(pat);
         }
-        System.out.print("TABLEID : "+Integer.toString(tableid)+" LENGTH = "+Integer.toString(sectionlength)+"\n");
-        System.out.print(Integer.toBinaryString(TSString2Int(payload_temp,65,32))+"\n");
+        //System.out.print("TABLEID : "+Integer.toString(tableid)+" LENGTH = "+Integer.toString(sectionlength)+"\n");
+        //System.out.print(Integer.toBinaryString(TSString2Int(payload_temp,65,32))+"\n");
         return program_number;
     }
     private ArrayList readPMT(String payload_temp,int PAT_TABLE){
@@ -170,8 +170,57 @@ public class Mpeg2TSPacket {
         int i=Integer.parseInt(st, 2);
         return i;
     }
-    public String changePAT(String payload_temp){
-        
+    public byte[] splitPAT(byte[] ts,int p_table){
+        /**
+         * 
+         * p_tableで指定された番組テーブルのみを取り出すPATを作る。
+         */
+        String tsbyte=new String();
+        for (int i=0;i<188;i++){
+            int it=ts[i]& 0xFF;
+            String s=Integer.toBinaryString(it);
+            if ((s.length()<8)&(s.length()>0)){
+                    for (int i2=s.length();i2<8;i2++){
+                        s="0"+s;
+                    }
+            }
+            tsbyte=tsbyte+s;
+        }
+        String tsheader="";
+        header=tsbyte.substring(0,31);
+        payload=tsbyte.substring(32);
+        starter=TSString2Int(tsbyte,0,8);
+        transporterror=TSString2Int(tsbyte,8,1);
+        payloadstart=TSString2Int(tsbyte,9,1);
+        transport_priority=TSString2Int(tsbyte,10,1);
+        PID=TSString2Int(tsbyte,11,13);
+        adaptation_field=TSString2Int(tsbyte, 26, 2);
+        continuity_counter=TSString2Int(tsbyte, 28, 4);
+        payload="";
+        if (PID!=8191){
+            if (adaptation_field==1){
+                pointer_field=TSString2Int(tsbyte, 32, 8);
+                tsheader=tsbyte.substring(0,40);
+                payload=tsbyte.substring(40);
+            }else if (adaptation_field==3){
+                int al=TSString2Int(tsbyte,32,8);
+                if (al<188){
+                    al=al*8;
+                }
+                tsheader=tsbyte.substring(0,40);
+                payload=tsbyte.substring(48+al);
+            }
+        }
+        if (PID==0){
+            //System.out.print("Pointer field ? : "+pointer_field+"\n");
+            payload=makePAT(payload, p_table);
+            //System.out.print("ここから変更済み\n");
+            //readPAT(changePAT(payload));
+        }
+        String rets = tsheader+payload;
+        return Stream2Byte(rets);
+    }
+    private String makePAT(String payload_temp,int Table){
         int tableid;
         int sectionlength;
         ArrayList program_number=new ArrayList();
@@ -185,13 +234,13 @@ public class Mpeg2TSPacket {
         String loop = "";
         patnum=patnum/32;
         for (int i=0;i<patnum;i++){
-            System.out.print(payload_temp.substring(64+32*i,95+32*i+1)+"\n");
+            //System.out.print(payload_temp.substring(64+32*i,95+32*i+1)+"\n");
             int[] pat=new int[2];
             pat[0]=TSString2Int(payload_temp,64+32*i,16);
             pat[1]=TSString2Int(payload_temp,32*i+64+19, 13);
             if (pat[0]==0){
                 loop=payload_temp.substring(64+32*i,95+32*i+1);
-            }else if (pat[0]>0){
+            }else if (pat[0]==Table){
                 loop=loop+payload_temp.substring(64+32*i,95+32*i+1);
                 i=patnum;
             }
@@ -204,7 +253,6 @@ public class Mpeg2TSPacket {
             crc.update(tt2);
         }
         rets=rets+Long2String(crc.getValue(),32);
-        //for (int i3=0;i<)
         return rets;
     }
     private String Int2String(int num,int length){
index 50e8b71..52bd461 100644 (file)
@@ -37,7 +37,7 @@ public class Tsfile {
             }
             for (int i=0;i<8200;i++){
                 if (PIDCount[i]>0){
-                    System.out.print(Integer.toString(i)+" : "+Integer.toString(PIDCount[i])+"\n");
+                    //System.out.print(Integer.toString(i)+" : "+Integer.toString(PIDCount[i])+"\n");
                 }
                 //System.out.print(PIDCount[i]);
             }
@@ -62,6 +62,51 @@ public class Tsfile {
         }
 
     }
+    public void splitTS(String origpath,String destpath,int csch){
+        int[] pids=null;
+        int p_table=0;
+        if (csch>0){
+
+        }else{
+            int[] pidst=getPID(origpath);
+            p_table=pidst[0];
+            pids=new int[pidst.length-1];
+            for (int i=0;i<pidst.length-1;i++){
+                pids[i]=pidst[i+1];
+            }
+        }
+        byte[] tb=new byte[188];
+        int[] ib=new int[188];
+        ArrayList pat=new ArrayList();
+        ArrayList pmt=new ArrayList();
+        String[] sb=new String[188];
+        try {
+            FileInputStream in = new FileInputStream(origpath);
+            //int[] PIDCount=new int[8200];
+            byte[] wbyte=null;
+            for (int i=0;i<3000;i++){
+                in.read(tb);
+                Mpeg2TSPacket m2tp;
+                m2tp=new Mpeg2TSPacket();
+                m2tp.readTS(tb);
+                for (int j=0;j<pids.length;j++){
+                    if (m2tp.getPID()==pids[j]){
+                        if (m2tp.getPID()==0){
+                            wbyte=m2tp.splitPAT(tb,p_table);
+                        }else{
+                            wbyte=tb;
+                        }
+                    }
+                }
+                FileOutputStream fos=new FileOutputStream(destpath,true);
+                fos.write(wbyte);
+                fos.close();
+            }
+            
+        } catch (IOException ex) {
+            Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);
+        }
+    }
     private void writeTS(byte[] tspacket,String path){
         FileOutputStream fos = null;
         try {
@@ -79,6 +124,10 @@ public class Tsfile {
         }
     }
     private int[] getPID(String fpath){
+        /**
+         * PIDを取得し、これと思われる必要なPIDを抜き出す。
+         * @return プログラム番号(return[0])とPIDのリスト(return[1-])
+         */
         FileInputStream in = null;
         int[] reti=null;
         try {
@@ -90,7 +139,8 @@ public class Tsfile {
             String[] sb = new String[188];
             in = new FileInputStream(fpath);
             //int[] PIDCount = new int[8200];
-            for (int i = 0; i < 1000; i++) {
+            int imax=1000;
+            for (int i = 0; i < imax ; i++) {
                 in.read(tb);
                 String last8;
                 Mpeg2TSPacket m2tp;
@@ -101,6 +151,9 @@ public class Tsfile {
                 pat = m2tp.getPAT();
                 pmt = m2tp.getPMT();
                 //PIDCount[m2tp.getPID()]++;
+                if ((i==imax-1)&&(pmt.size()==0)){
+                    imax=imax+500;
+                }
             }
             
             int[] pat_det=new int[2];
@@ -122,10 +175,11 @@ public class Tsfile {
                     }
                 }
             }
-            reti=new int[ret.size()];
+            reti=new int[ret.size()+1];
+            reti[0]=pat_det[0];
             for (int i=0;i<ret.size();i++){
                 int retit=(Integer)ret.get(i);
-                reti[i]=retit;
+                reti[i+1]=retit;
             }
         } catch (IOException ex) {
             Logger.getLogger(Tsfile.class.getName()).log(Level.SEVERE, null, ex);