PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/assets manager sources/src/com/flashjs/assetsmanager/Main.java

http://github.com/PixelsCommander/FlashJS
Java | 261 lines | 202 code | 50 blank | 9 comment | 33 complexity | 57e892914ff84cb9569471a1fc83d4ed MD5 | raw file
  1. package com.flashjs.assetsmanager;
  2. import it.sauronsoftware.jave.AudioAttributes;
  3. import it.sauronsoftware.jave.Encoder;
  4. import it.sauronsoftware.jave.EncoderException;
  5. import it.sauronsoftware.jave.EncodingAttributes;
  6. import it.sauronsoftware.jave.FFMPEGLocator;
  7. import it.sauronsoftware.jave.InputFormatException;
  8. import it.sauronsoftware.jave.MultimediaInfo;
  9. import java.awt.image.BufferedImage;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.io.OutputStream;
  16. import java.util.ArrayList;
  17. import javax.imageio.ImageIO;
  18. import org.imgscalr.*;
  19. public class Main {
  20. static String pathToSrcFolder;
  21. static File srcFolder;
  22. static Float currentScale;
  23. static Float step;
  24. static ArrayList<String> soundExtensions;
  25. static ArrayList<File> soundDirs;
  26. public static void main(String[] arguments) throws IOException, IllegalArgumentException, InputFormatException, EncoderException{
  27. soundExtensions = new ArrayList<String>();
  28. soundExtensions.add("mp3");
  29. soundExtensions.add("ogg");
  30. soundExtensions.add("wav");
  31. if (arguments.length > 0){
  32. srcFolder = new File(arguments[0]);
  33. } else {
  34. srcFolder = new File(".");
  35. }
  36. if (srcFolder.exists()){
  37. processDirectory(new File(srcFolder.getAbsolutePath() + "/assets/4/"));
  38. processSoundDirectories(new File(srcFolder.getAbsolutePath() + "/sounds/"));
  39. }
  40. }
  41. static void processSoundDirectories(File srcDir) throws IOException, IllegalArgumentException, InputFormatException, EncoderException{
  42. soundDirs = new ArrayList<File>();
  43. for (int i = 0; i < soundExtensions.size(); i++){
  44. File dir = new File(srcDir, soundExtensions.get(i));
  45. soundDirs.add(dir);
  46. if(!dir.exists()){
  47. dir.mkdir();
  48. System.out.println("Directory created " + dir.getName());
  49. }
  50. }
  51. for (int i = 0; i < soundExtensions.size(); i++){
  52. processSoundDirectory(soundDirs.get(i));
  53. }
  54. }
  55. static void copyDirToAllSoundDirs(File src){
  56. String relativePath = getRelativePath(src);
  57. for (int i = 0; i < soundDirs.size(); i++){
  58. File dest = new File(soundDirs.get(i), relativePath);
  59. if(!dest.exists()){
  60. dest.mkdir();
  61. }
  62. }
  63. }
  64. static void processSoundDirectory(File src) throws IOException, IllegalArgumentException, InputFormatException, EncoderException{
  65. if(src.isDirectory()){
  66. copyDirToAllSoundDirs(src);
  67. //list all the directory contents
  68. String files[] = src.list();
  69. for (String file : files) {
  70. //construct the src and dest file structure
  71. File srcFile = new File(src, file);
  72. //recursive copy
  73. processSoundDirectory(srcFile);
  74. }
  75. }else{
  76. if (getDirByExtension(getExtension(src)) != null) copyFileToAllExtensions(src);
  77. //Encode file to all extensions and copy to all folders
  78. }
  79. }
  80. static void copyFileToAllExtensions(File file) throws IllegalArgumentException, InputFormatException, EncoderException{
  81. System.out.println("Converting file " + file.getAbsolutePath().replaceAll("sounds//.//sounds", "") + " it exists = " + file.exists());
  82. String extension = getExtension(file);
  83. if (extension != "mp3"){
  84. copyFileToMP3(file);
  85. }
  86. if (extension != "ogg"){
  87. copyFileToOgg(file);
  88. }
  89. }
  90. static void copyFileToMP3(File source) throws IllegalArgumentException, InputFormatException, EncoderException{
  91. File target = new File(getDirByExtension("mp3"), changeExtension(getRelativePath(source), ".mp3"));
  92. if (target.exists()) return;
  93. AudioAttributes audio = new AudioAttributes();
  94. audio.setCodec("libmp3lame");
  95. audio.setBitRate(new Integer(64000));
  96. audio.setChannels(new Integer(2));
  97. audio.setSamplingRate(new Integer(22050));
  98. EncodingAttributes attrs = new EncodingAttributes();
  99. attrs.setFormat("mp3");
  100. attrs.setAudioAttributes(audio);
  101. Encoder encoder = new Encoder(new Myffmpeg());
  102. encoder.encode(source, target, attrs);
  103. }
  104. static void copyFileToOgg(File source) throws IllegalArgumentException, InputFormatException, EncoderException{
  105. File target = new File(getDirByExtension("ogg"), changeExtension(getRelativePath(source), ".ogg"));
  106. if (target.exists()) return;
  107. AudioAttributes audio = new AudioAttributes();
  108. audio.setCodec("libvorbis");
  109. audio.setBitRate(new Integer(64000));
  110. audio.setChannels(new Integer(2));
  111. audio.setSamplingRate(new Integer(22050));
  112. EncodingAttributes attrs = new EncodingAttributes();
  113. attrs.setFormat("ogg");
  114. attrs.setAudioAttributes(audio);
  115. Encoder encoder = new Encoder(new Myffmpeg());
  116. encoder.encode(source, target, attrs);
  117. }
  118. static void copyFileToWav(File source) throws IllegalArgumentException, InputFormatException, EncoderException{
  119. File target = new File(getDirByExtension("wav"), changeExtension(getRelativePath(source), ".wav"));
  120. if (target.exists()) return;
  121. AudioAttributes audio = new AudioAttributes();
  122. audio.setCodec("pcm_s16le");
  123. audio.setBitRate(new Integer(128000));
  124. audio.setChannels(new Integer(2));
  125. audio.setSamplingRate(new Integer(44100));
  126. EncodingAttributes attrs = new EncodingAttributes();
  127. attrs.setFormat("wav");
  128. attrs.setAudioAttributes(audio);
  129. Encoder encoder = new Encoder(new Myffmpeg());
  130. encoder.encode(source, target, attrs);
  131. }
  132. static File getDirByExtension(String ext){
  133. for (int i = 0; i < soundDirs.size(); i++){
  134. String dirPath = soundDirs.get(i).getAbsolutePath();
  135. if (dirPath.indexOf("sounds/" + ext) != -1){
  136. return soundDirs.get(i);
  137. }
  138. }
  139. return null;
  140. }
  141. static String getExtension(File src){
  142. String formatName = src.getName();
  143. int pos = formatName.lastIndexOf('.');
  144. return formatName.substring(pos+1);
  145. }
  146. static String getRelativePath(File file){
  147. String filePath = file.getAbsolutePath();
  148. for (int i = 0; i < soundDirs.size(); i++){
  149. String dirPath = soundDirs.get(i).getAbsolutePath();
  150. if (filePath.indexOf(dirPath) != -1){
  151. return filePath.replaceAll(dirPath, "");
  152. }
  153. }
  154. return "";
  155. }
  156. static void processDirectory(File srcDir){
  157. step = new Float(1);
  158. currentScale = new Float(4);
  159. while(currentScale > 1.0){
  160. currentScale -= step;
  161. copyDirectoryStructure(srcDir);
  162. }
  163. }
  164. static void copyDirectoryStructure(File srcDir){
  165. File dest = new File(srcDir.getParent() + "/" + currentScale.intValue() + "/");
  166. try {
  167. copyFolder(srcDir, dest);
  168. } catch (IOException e) {
  169. // TODO Auto-generated catch block
  170. e.printStackTrace();
  171. }
  172. }
  173. public static void copyFolder(File src, File dest)
  174. throws IOException{
  175. if(src.isDirectory()){
  176. //if directory not exists, create it
  177. if(!dest.exists()){
  178. dest.mkdir();
  179. System.out.println("Directory copied from "
  180. + src + " to " + dest);
  181. }
  182. //list all the directory contents
  183. String files[] = src.list();
  184. for (String file : files) {
  185. //construct the src and dest file structure
  186. File srcFile = new File(src, file);
  187. File destFile = new File(dest, file);
  188. //recursive copy
  189. copyFolder(srcFile,destFile);
  190. }
  191. }else{
  192. BufferedImage srcImage = ImageIO.read(src);
  193. if (srcImage == null) return;
  194. BufferedImage destImage = Scalr.resize(srcImage, (int)((srcImage.getWidth() / 4) * currentScale), (int)((srcImage.getHeight() / 4) * currentScale));
  195. String formatName = getExtension(src);
  196. ImageIO.write(destImage, formatName, dest);
  197. srcImage = null;
  198. destImage = null;
  199. Runtime.getRuntime().gc();
  200. System.out.println("File copied from " + src + " to " + dest);
  201. }
  202. }
  203. static String changeExtension(String originalName, String newExtension) {
  204. int lastDot = originalName.lastIndexOf(".");
  205. if (lastDot != -1) {
  206. return originalName.substring(0, lastDot) + newExtension;
  207. } else {
  208. return originalName + newExtension;
  209. }
  210. }
  211. }