Advertisement
CoolPersonGuy

New Mash

Oct 13th, 2014
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.51 KB | None | 0 0
  1.  
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.RandomAccessFile;
  5. import javax.swing.JOptionPane;
  6. import javax.swing.JTextField;
  7.  
  8.  
  9. /**
  10. * Francis Lyons
  11. * 10/13/14
  12. *
  13. * program built to delete i frames and repeat certain sections of b-frames
  14. *
  15. * takes as an argument the filename w/o filetype of the video to be used (works with videos in AVI format, converted using FFMPEG)
  16. *
  17. * reccomended to use avidemux or something similar to find total number of frames in video / specific frame locations
  18. */
  19.  
  20. public class NewMash
  21. {
  22. public static final int[] F_BYTES = new int[4];
  23. public static final int[] I_BYTES = new int[4];
  24.  
  25. /*
  26. * the nextFrame method should return the long position of the
  27. * beginning of the next frame, regardless of if it is a keyframe
  28. * or not. method will return -1 if it hits the end of the file
  29. * before finding another frame
  30. * takes as a parameter the current read position in the file
  31. */
  32. public static long nextFrame(RandomAccessFile file, long start) throws IOException
  33. {
  34. long startPos = -1;
  35. long currentPos = start; // get current read position
  36. file.seek(start);
  37.  
  38. while(file.getFilePointer() < file.length())
  39. {
  40. file.seek(currentPos); // currentPos is incremented by 1 each time, reads in if went to far
  41. if(file.read() == F_BYTES[0] && file.read() == F_BYTES[1] && file.read() == F_BYTES[2] && file.read() == F_BYTES[3])
  42. {
  43. // we have found the beginning of a frame
  44. // grab the found frame start position and get out of the while loop
  45.  
  46. startPos = currentPos;
  47. break;
  48. }
  49. currentPos++;
  50. }
  51. file.seek(start);
  52. return startPos;
  53. }
  54.  
  55.  
  56. /*
  57. * method to check if current frame is a keyframe
  58. * to be used properly this method should be used in conjunction
  59. * with nextFrame (this method should take as a parameter the long position
  60. * of the beginning of a frame in the file, then it will return boolean
  61. * of whether or not the frame is an i frame)
  62. *
  63. * right now the number of bytes to search for the iframe flag
  64. * is hardcoded at 15 to be safe
  65. */
  66. public static boolean isIFrame(RandomAccessFile file, long pos) throws IOException
  67. {
  68. boolean iFrame = false;
  69.  
  70. for(int i = 0; i < 15; i++)
  71. {
  72. file.seek(pos + i); // start reading at the given position, then moves 1 forward each time
  73.  
  74. if(file.read() == I_BYTES[0] && file.read() == I_BYTES[1] && file.read() == I_BYTES[2] && file.read() == I_BYTES[3])
  75. {
  76. // we have found the flag that indicates that this is an iframe
  77. // exit the for loop
  78.  
  79. iFrame = true;
  80. break;
  81. }
  82. }
  83. file.seek(pos);
  84. return iFrame;
  85. }
  86.  
  87.  
  88. public static void main(String[] args) throws IOException
  89. {
  90.  
  91. String filename = args[0]; // only argument should be the filename of the video
  92.  
  93. // get user input for various parameters
  94. JTextField endBeginTxtFld = new JTextField(); // end of beginning unchanged area
  95. JTextField startTxtFld = new JTextField(); // start of changed area
  96. JTextField endTxtFld = new JTextField(); // end of changed area
  97. JTextField repeatTxtFld = new JTextField(); // number of times to repeat each frame
  98. JTextField totalRepTxtFld = new JTextField(); // number of times to repeat whole change area
  99.  
  100. Object[] message = {"end of beginning: ", endBeginTxtFld,
  101. "start frame: ", startTxtFld,
  102. "end frame: ", endTxtFld,
  103. "number of times to repeat each frame: ", repeatTxtFld,
  104. "number of times to repeat total frame area: ", totalRepTxtFld};
  105.  
  106. JOptionPane.showConfirmDialog(null, message, "Enter Specified Values", JOptionPane.OK_OPTION);
  107.  
  108. // get strings for each of the inputs
  109. String endBegStr = endBeginTxtFld.getText();
  110. String startStr = startTxtFld.getText();
  111. String endStr = endTxtFld.getText();
  112. String repeatStr = repeatTxtFld.getText();
  113. String totalRepStr = totalRepTxtFld.getText();
  114.  
  115. // get usable integer values from above strings
  116. int endBegin = Integer.parseInt(endBegStr);
  117. int start = Integer.parseInt(startStr);
  118. int end = Integer.parseInt(endStr);
  119. int repeat = Integer.parseInt(repeatStr);
  120. int totalRep = Integer.parseInt(totalRepStr);
  121.  
  122.  
  123. F_BYTES[0] = 48; // decimal equivalent of 30 in hex
  124. F_BYTES[1] = 48; // decimal equivalent of 30 in hex
  125. F_BYTES[2] = 100; // decimal equivalent of 64 in hex
  126. F_BYTES[3] = 99; // decimal equivalent of 63 in hex
  127.  
  128. I_BYTES[0] = 0; // decimal equivalent of 00 in hex
  129. I_BYTES[1] = 1; // decimal equivalent of 01 in hex
  130. I_BYTES[2] = 176; // decimal equivalent of B0 in hex
  131. I_BYTES[3] = 1; // decimal equivalent of 01 in hex
  132.  
  133.  
  134. RandomAccessFile file = new RandomAccessFile(filename + ".avi", "rw");
  135.  
  136. // skip some initial frames otherwise video just changes from green
  137. int frames = 0;
  138. long current = nextFrame(file, 0);
  139. System.out.println("finding beginning");
  140. while(frames < endBegin)
  141. {
  142. current = nextFrame(file, current + 1);
  143. frames++; // found another frame, increment count
  144. //System.out.println(frames + " / " + copyBegin);
  145. }
  146.  
  147.  
  148. FileOutputStream newFile = new FileOutputStream(filename + "_0.avi" ,true);
  149.  
  150. file.seek(0);
  151. System.out.println("copying beginning");
  152. // copy beginning of file until moved past startOffSet number of frames
  153. while(file.getFilePointer() < current)
  154. {
  155. newFile.write(file.readByte());
  156. }
  157.  
  158. System.out.println("skipping from beginning to repeat area");
  159.  
  160. // go through these frames but dont read them over
  161. while(frames < start)
  162. {
  163. current = nextFrame(file, current + 1);
  164. frames++; // found another frame, increment count
  165. //System.out.println(frames + " / " + start);
  166. }
  167.  
  168.  
  169. long currFrame = nextFrame(file, file.getFilePointer());
  170. long nextFrame;
  171.  
  172. long startFrame = currFrame;
  173.  
  174. // need to loop through file and copy over bytes unless we are in an iframe
  175. System.out.println("copying repeat area");
  176. for(int j = 0; j < totalRep; j++)
  177. {
  178. file.seek(startFrame);
  179. currFrame = startFrame;
  180. nextFrame = nextFrame(file, currFrame + 1);
  181. int frameCount = start;
  182.  
  183. while(file.getFilePointer() < file.length() && frameCount < end)
  184. {
  185. if(nextFrame == -1)
  186. {
  187. // there is no next frame, just copy the rest
  188. while(file.getFilePointer() < file.length())
  189. {
  190. newFile.write(file.readByte());
  191. }
  192. }
  193.  
  194. // if next frame is iFrame and we want to delete them, skip past it
  195. else if(isIFrame(file, currFrame))
  196. {
  197. //System.out.println("skipped iFrame");
  198. // skip to next frame
  199. file.seek(nextFrame);
  200. }
  201.  
  202. // if not iFrame then we want to copy it
  203. else
  204. {
  205. for(int i = 0; i < repeat; i++)
  206. {
  207. while(file.getFilePointer() < nextFrame)
  208. {
  209. newFile.write(file.readByte());
  210. }
  211. file.seek(currFrame);
  212. }
  213. }
  214.  
  215. // nextFrame is now the current frame
  216. currFrame = nextFrame;
  217. nextFrame = nextFrame(file, file.getFilePointer() + 1);
  218. frameCount++;
  219. //System.out.println(frameCount + " / " + end);
  220. newFile.flush();
  221. //System.out.println(file.getFilePointer() + " / " + file.length());
  222. }
  223. System.out.println("repeat: " + j);
  224. }
  225.  
  226. System.out.println("copying end");
  227. while(file.getFilePointer() < file.length())
  228. {
  229. //copy rest of the file after 'end'
  230. nextFrame = nextFrame(file, file.getFilePointer() + 1);
  231.  
  232. if(nextFrame == -1)
  233. {
  234. // there is no next frame, just copy the rest
  235. while(file.getFilePointer() < file.length())
  236. {
  237. newFile.write(file.readByte());
  238. }
  239. }
  240.  
  241. // if next frame is iFrame and we want to delete them, skip past it
  242. else if(isIFrame(file, currFrame))
  243. {
  244. //System.out.println("skipped iFrame");
  245. // skip to next frame
  246. file.seek(nextFrame);
  247. }
  248.  
  249. // if not iFrame or we want to keep iFrames then copy it
  250. else
  251. {
  252. while(file.getFilePointer() < nextFrame)
  253. {
  254. newFile.write(file.readByte());
  255. }
  256. file.seek(currFrame);
  257. }
  258. currFrame = nextFrame;
  259. nextFrame = nextFrame(file, file.getFilePointer() + 1);
  260. newFile.flush();
  261. }
  262.  
  263. System.out.println("done");
  264. newFile.close();
  265. }
  266.  
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement