Exemple #1
0
  public static void splitMp3(File in, File out, File log) {
    byte[] buffer;
    byte[] buf2;
    int byteread;
    long bytesum = 0;
    Id3v23Header id3header;
    Mp3FrameHeader mp3header;
    int nextsize = 0;
    long out_cnt = 0;
    long fileLength = in.length();
    try {
      System.out.println(in.getAbsolutePath() + ",size=" + fileLength);
      InputStream inStream = new FileInputStream(in);
      while ((fileLength - bytesum) > 0) {
        buffer = new byte[4];
        if ((byteread = inStream.read(buffer)) == -1) {
          System.out.println("read error Mp3FrameHeader(4bytes)");
          inStream.close();
          System.out.println("read=" + bytesum);
          return;
        }
        bytesum += byteread;
        mp3header = new Mp3FrameHeader(buffer);
        System.out.println("MP3Header=" + mp3header);

        if (!mp3header.isValid()) {
          buf2 = new byte[6];
          if ((byteread = inStream.read(buf2)) == -1) {
            System.out.println("read error ID3Frame(10bytes)");
            inStream.close();
            System.out.println("read=" + bytesum);
            return;
          }
          bytesum += byteread;
          byte[] tmp = new byte[10];
          System.arraycopy(buffer, 0, tmp, 0, buffer.length);
          System.arraycopy(buf2, 0, tmp, buffer.length, buf2.length);
          id3header = new Id3v23Header(tmp);
          System.out.println("ID3Header=" + id3header);
          nextsize = (int) id3header.getId3Size();
        } else {
          nextsize = mp3header.getSize() - 4;
        }

        buf2 = new byte[nextsize];
        if ((byteread = inStream.read(buf2)) == -1) {
          System.out.println("read error Mp3FrameHeader(4bytes)");
          inStream.close();
          System.out.println("read=" + bytesum);
          return;
        }
        bytesum += byteread;
        if (mp3header.isValid()) {
          byte[] tmp = new byte[nextsize + 4];
          System.arraycopy(buffer, 0, tmp, 0, buffer.length);
          System.arraycopy(buf2, 0, tmp, buffer.length, buf2.length);
          String fileName = in.getName();
          String outFile =
              out.getAbsolutePath()
                  + "/"
                  + fileName.substring(0, fileName.indexOf("."))
                  + "_"
                  + Strings.format(String.valueOf(out_cnt), 5, '0')
                  + ".mp3";
          OutputStream os = new FileOutputStream(outFile);
          os.write(tmp);
          os.close();
          out_cnt++;
        }
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    System.out.println("bytesum=" + bytesum);
  }