PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/scala/dk/mehmedbasic/audiobook/ScalaudioConverter.scala

https://bitbucket.org/mehmedbasic/scalaudiobookconverter
Scala | 131 lines | 99 code | 26 blank | 6 comment | 7 complexity | 8f684e6219e42287bb188f8e1d274835 MD5 | raw file
  1. package dk.mehmedbasic.audiobook
  2. import conversion.ConverterFactory
  3. import execution._
  4. import dk.mehmedbasic.audiobook.io.StreamReader
  5. import gui.GuiRunner
  6. import java.io.File
  7. import collection.mutable
  8. import java.util.concurrent._
  9. import scopt.immutable.OptionParser
  10. import collection.JavaConversions.JConcurrentMapWrapper
  11. /**
  12. * The main class of the converter
  13. *
  14. * @author Jesenko Mehmedbasic
  15. * created 09-01-13, 03:24
  16. */
  17. object ScalaudioConverter {
  18. val console: ConsoleUtil = new ConsoleUtil(System.out, System.err)
  19. val timeUnit: TimeUnit = TimeUnit.SECONDS
  20. val processors: Int = Runtime.getRuntime.availableProcessors() - 1
  21. val converterPool: ExecutorService = Executors.newFixedThreadPool(processors)
  22. val finishedFiles = new JConcurrentMapWrapper[Int, File](new ConcurrentHashMap[Int, File]())
  23. def main(args: Array[String]) {
  24. console.header()
  25. console.horizontalLine()
  26. val parser: OptionParser[ConsoleConfig] = createParser()
  27. parser.parse(args, ConsoleConfig()) map {
  28. config =>
  29. if (config.isGraphical) {
  30. console.printLine("Graphical user interface flag set.")
  31. console.printLine("Looking up and running GUI")
  32. GuiRunner.configure()
  33. GuiRunner.locateAndRun()
  34. } else {
  35. if (config.outputFile.isEmpty) {
  36. console.error("No output file set.")
  37. System.exit(-1)
  38. }
  39. val files: mutable.MutableList[File] = config.inputFiles
  40. if (!filesExist(files)) {
  41. System.exit(-1)
  42. }
  43. val startTime = System.currentTimeMillis()
  44. var x = 0
  45. files.foreach(
  46. (inputFile: File) => {
  47. println("Starting conversion of \"" + inputFile.getAbsoluteFile + "\"")
  48. val y = x
  49. val factory: ConverterFactory = new ConverterFactory(
  50. inputFile, (inputFile: String, file: File) => {
  51. finishedFiles.put(y, file)
  52. println(inputFile + " converted")
  53. if (finishedFiles.keySet.size == files.size) {
  54. concatenateFiles()
  55. }
  56. })
  57. val converter: Runnable = factory.createConverter()
  58. converterPool.submit(converter)
  59. x += 1
  60. })
  61. def concatenateFiles() {
  62. val system: OperatingSystem = OperatingSystem.get()
  63. val outputFile: File = config.output.get
  64. val factory: ConcatCommandFactory = new ConcatCommandFactory(system, finishedFiles, outputFile)
  65. val executor: CommandExecutor = system.getExecutor
  66. val createCommand: Command = factory.createCommand()
  67. val process: Process = executor.execute(createCommand)
  68. val reader: StreamReader = new StreamReader(process.getInputStream)
  69. reader.start()
  70. process.waitFor()
  71. val endTime = System.currentTimeMillis().asInstanceOf[Double]
  72. console.printLine("Finished converting " + x + " files in " + ((endTime - startTime) / 1000d) + " seconds.")
  73. console.printLine("")
  74. console.printLine("Cleaning up...")
  75. new File(factory.temporaryOutput).renameTo(outputFile)
  76. finishedFiles.values.foreach((file: File) => file.delete())
  77. console.printLine("Done!")
  78. console.printLine("")
  79. console.printLine("Thank you for choosing us!")
  80. System.exit(0)
  81. }
  82. }
  83. }
  84. }
  85. def createParser(): scopt.immutable.OptionParser[ConsoleConfig] = {
  86. new scopt.immutable.OptionParser[ConsoleConfig]() {
  87. def options = Seq(
  88. arglist("Input files", "Input filenames") {
  89. case (s: String, config: ConsoleConfig) => config.addInputFile(s)
  90. },
  91. opt("o", "outputfile", "Output file") {
  92. case (s: String, config: ConsoleConfig) => config.setOutputFile(s)
  93. },
  94. flag("gui", "Graphical interface") {
  95. case (config: ConsoleConfig) => config.setGraphical()
  96. }
  97. )
  98. }
  99. }
  100. def filesExist(args: mutable.MutableList[File]): Boolean = {
  101. val filtered = args.filterNot((x: File) => x.exists())
  102. filtered.foreach((x: File) => console.error("File '" + x.getAbsoluteFile + "' does not exist"))
  103. filtered.size == 0
  104. }
  105. }