Guest User

Untitled

a guest
May 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. package mp4parser;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6.  
  7. // For parsing frames from an mp4 file.
  8. public class mp4parser {
  9.  
  10. public static void main(String[] args) throws IOException {
  11.  
  12. // Holds the size of the box in bytes
  13. long size;
  14. // Temporary holder for bit shifting
  15. long size2;
  16. // Holds the type of the box as ASCII characters
  17. long type = 0;
  18. // Temporary holder for bit shifting
  19. long type2;
  20. // Signifies end of file when -1
  21. int eof = 0;
  22. // Signifies 64 bit size when 1
  23. int largesize = 0;
  24. // Counter for reading whole box
  25. int j = 0;
  26. // Input stream for file
  27. FileInputStream fis = new FileInputStream("C:\\Test\\57.mp4");
  28. // Array for the media data
  29. ArrayList<Integer> mdat = new ArrayList<Integer>();
  30.  
  31. // Read through whole file
  32. while(eof != -1) {
  33.  
  34. // Read size
  35. size = (fis.read() << 24);
  36. size2 = size;
  37. size = ((size2)) | (fis.read() << 16 );
  38. size2 = size;
  39. size = ((size2)) | (fis.read() << 8 );
  40. size2 = size;
  41. size = ((size2)) | (fis.read() );
  42. j+=4;
  43.  
  44. // Print size
  45. System.out.println(String.format("%d", size));
  46.  
  47. // Means there is an 8 byte size
  48. if (size == 1) {
  49. largesize = 1;
  50. }
  51. // Means the box goes to the end of the file
  52. // TODO Top level boxes don't appear to follow this logic
  53. else if (size == 0) {
  54. eof = -1;
  55. }
  56. // Means size is accurate
  57. else {
  58. largesize = 0;
  59. }
  60.  
  61. // Read in the 8 byte size
  62. if(largesize == 1) {
  63. size = (fis.read() << 56);
  64. size2 = size;
  65. size = ((size2)) | (fis.read() << 48 );
  66. size2 = size;
  67. size = ((size2)) | (fis.read() << 40 );
  68. size2 = size;
  69. size = ((size2)) | (fis.read() << 32);
  70. size2 = size;
  71. size = size | (fis.read() << 24);
  72. size2 = size;
  73. size = ((size2)) | (fis.read() << 16 );
  74. size2 = size;
  75. size = ((size2)) | (fis.read() << 8 );
  76. size2 = size;
  77. size = ((size2)) | (fis.read() );
  78. j+=8;
  79. // Print size
  80. System.out.println(String.format("%d", size));
  81. }
  82.  
  83. // Read through the whole box
  84. while (j < (size)) {
  85.  
  86. // Read type
  87. if(j == 4) {
  88. type = fis.read();
  89. type2 = type;
  90. type = (type2) | (fis.read() << 8);
  91. type2 = type;
  92. type = (type2) | (fis.read() << 16);
  93. type2 = type;
  94. type = (type2) | (fis.read() << 24);
  95. j+=4;
  96. System.out.println(toASCII(type));
  97. }
  98. // Read and print file type info
  99. else if(toASCII((bytesToLong("ftyp".getBytes()))).equals(toASCII(type))) {
  100. // Read through whole box
  101. while (j < (size)) {
  102. if(j == 12) {
  103. type = (fis.read() << 24);
  104. type2 = type;
  105. type = ((type2)) | (fis.read() << 16 );
  106. type2 = type;
  107. type = ((type2)) | (fis.read() << 8 );
  108. type2 = type;
  109. type = ((type2)) | (fis.read() );
  110. j+=4;
  111. // Print size
  112. System.out.println(String.format("%d", size));
  113. } else {
  114. type = fis.read();
  115. type2 = type;
  116. type = (type2) | (fis.read() << 8);
  117. type2 = type;
  118. type = (type2) | (fis.read() << 16);
  119. type2 = type;
  120. type = (type2) | (fis.read() << 24);
  121. j+=4;
  122. // Print file type info
  123. System.out.println(toASCII(type));
  124. }
  125. }
  126. }
  127. // Skip free space
  128. else if(toASCII((bytesToLong("free".getBytes()))).equals(toASCII(type))) {
  129. fis.read();
  130. }
  131. // Read media data
  132. else if(toASCII((bytesToLong("mdat".getBytes()))).equals(toASCII(type))) {
  133. }
  134. // Read movie data
  135. else if(toASCII((bytesToLong("moov".getBytes()))).equals(toASCII(type))) {
  136.  
  137. }
  138. }
  139. // Reset to read next box
  140. j = 0;
  141. }
  142. }
  143.  
  144. // Converts long into ASCII string
  145. private static String toASCII(long type) {
  146. // Types are 4 bytes long
  147. int length = 4;
  148. // Counter for converting each character
  149. int i = 0;
  150. // For building 4 character string
  151. StringBuilder builder = new StringBuilder(length);
  152. // Add all characters to string
  153. while (i < length) {
  154. builder.append((char) ((type >> (8 * i)) & 0xFF));
  155. i++;
  156. }
  157. return builder.toString();
  158. }
  159.  
  160. public static long bytesToLong(byte[] b) {
  161. long type = 0;
  162. long type2;
  163. int i = 0;
  164. while(i < 4){
  165. type2 = type;
  166. type = (type2) | (b[i] << (8*i));
  167. i++;
  168. }
  169. return type;
  170. }
  171. }
Add Comment
Please, Sign In to add comment