Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mp4parser;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.util.ArrayList;
- // For parsing frames from an mp4 file.
- public class mp4parser {
- public static void main(String[] args) throws IOException {
- // Holds the size of the box in bytes
- long size;
- // Temporary holder for bit shifting
- long size2;
- // Holds the type of the box as ASCII characters
- long type = 0;
- // Temporary holder for bit shifting
- long type2;
- // Signifies end of file when -1
- int eof = 0;
- // Signifies 64 bit size when 1
- int largesize = 0;
- // Counter for reading whole box
- int j = 0;
- // Input stream for file
- FileInputStream fis = new FileInputStream("C:\\Test\\57.mp4");
- // Array for the media data
- ArrayList<Integer> mdat = new ArrayList<Integer>();
- // Read through whole file
- while(eof != -1) {
- // Read size
- size = (fis.read() << 24);
- size2 = size;
- size = ((size2)) | (fis.read() << 16 );
- size2 = size;
- size = ((size2)) | (fis.read() << 8 );
- size2 = size;
- size = ((size2)) | (fis.read() );
- j+=4;
- // Print size
- System.out.println(String.format("%d", size));
- // Means there is an 8 byte size
- if (size == 1) {
- largesize = 1;
- }
- // Means the box goes to the end of the file
- // TODO Top level boxes don't appear to follow this logic
- else if (size == 0) {
- eof = -1;
- }
- // Means size is accurate
- else {
- largesize = 0;
- }
- // Read in the 8 byte size
- if(largesize == 1) {
- size = (fis.read() << 56);
- size2 = size;
- size = ((size2)) | (fis.read() << 48 );
- size2 = size;
- size = ((size2)) | (fis.read() << 40 );
- size2 = size;
- size = ((size2)) | (fis.read() << 32);
- size2 = size;
- size = size | (fis.read() << 24);
- size2 = size;
- size = ((size2)) | (fis.read() << 16 );
- size2 = size;
- size = ((size2)) | (fis.read() << 8 );
- size2 = size;
- size = ((size2)) | (fis.read() );
- j+=8;
- // Print size
- System.out.println(String.format("%d", size));
- }
- // Read through the whole box
- while (j < (size)) {
- // Read type
- if(j == 4) {
- type = fis.read();
- type2 = type;
- type = (type2) | (fis.read() << 8);
- type2 = type;
- type = (type2) | (fis.read() << 16);
- type2 = type;
- type = (type2) | (fis.read() << 24);
- j+=4;
- System.out.println(toASCII(type));
- }
- // Read and print file type info
- else if(toASCII((bytesToLong("ftyp".getBytes()))).equals(toASCII(type))) {
- // Read through whole box
- while (j < (size)) {
- if(j == 12) {
- type = (fis.read() << 24);
- type2 = type;
- type = ((type2)) | (fis.read() << 16 );
- type2 = type;
- type = ((type2)) | (fis.read() << 8 );
- type2 = type;
- type = ((type2)) | (fis.read() );
- j+=4;
- // Print size
- System.out.println(String.format("%d", size));
- } else {
- type = fis.read();
- type2 = type;
- type = (type2) | (fis.read() << 8);
- type2 = type;
- type = (type2) | (fis.read() << 16);
- type2 = type;
- type = (type2) | (fis.read() << 24);
- j+=4;
- // Print file type info
- System.out.println(toASCII(type));
- }
- }
- }
- // Skip free space
- else if(toASCII((bytesToLong("free".getBytes()))).equals(toASCII(type))) {
- fis.read();
- }
- // Read media data
- else if(toASCII((bytesToLong("mdat".getBytes()))).equals(toASCII(type))) {
- }
- // Read movie data
- else if(toASCII((bytesToLong("moov".getBytes()))).equals(toASCII(type))) {
- }
- }
- // Reset to read next box
- j = 0;
- }
- }
- // Converts long into ASCII string
- private static String toASCII(long type) {
- // Types are 4 bytes long
- int length = 4;
- // Counter for converting each character
- int i = 0;
- // For building 4 character string
- StringBuilder builder = new StringBuilder(length);
- // Add all characters to string
- while (i < length) {
- builder.append((char) ((type >> (8 * i)) & 0xFF));
- i++;
- }
- return builder.toString();
- }
- public static long bytesToLong(byte[] b) {
- long type = 0;
- long type2;
- int i = 0;
- while(i < 4){
- type2 = type;
- type = (type2) | (b[i] << (8*i));
- i++;
- }
- return type;
- }
- }
Add Comment
Please, Sign In to add comment