/src/main/scala/dk/mehmedbasic/audiobook/ScalaudioConverter.scala
Scala | 131 lines | 99 code | 26 blank | 6 comment | 7 complexity | 8f684e6219e42287bb188f8e1d274835 MD5 | raw file
- package dk.mehmedbasic.audiobook
- import conversion.ConverterFactory
- import execution._
- import dk.mehmedbasic.audiobook.io.StreamReader
- import gui.GuiRunner
- import java.io.File
- import collection.mutable
- import java.util.concurrent._
- import scopt.immutable.OptionParser
- import collection.JavaConversions.JConcurrentMapWrapper
- /**
- * The main class of the converter
- *
- * @author Jesenko Mehmedbasic
- * created 09-01-13, 03:24
- */
- object ScalaudioConverter {
- val console: ConsoleUtil = new ConsoleUtil(System.out, System.err)
- val timeUnit: TimeUnit = TimeUnit.SECONDS
- val processors: Int = Runtime.getRuntime.availableProcessors() - 1
- val converterPool: ExecutorService = Executors.newFixedThreadPool(processors)
- val finishedFiles = new JConcurrentMapWrapper[Int, File](new ConcurrentHashMap[Int, File]())
- def main(args: Array[String]) {
- console.header()
- console.horizontalLine()
- val parser: OptionParser[ConsoleConfig] = createParser()
- parser.parse(args, ConsoleConfig()) map {
- config =>
- if (config.isGraphical) {
- console.printLine("Graphical user interface flag set.")
- console.printLine("Looking up and running GUI")
- GuiRunner.configure()
- GuiRunner.locateAndRun()
- } else {
- if (config.outputFile.isEmpty) {
- console.error("No output file set.")
- System.exit(-1)
- }
- val files: mutable.MutableList[File] = config.inputFiles
- if (!filesExist(files)) {
- System.exit(-1)
- }
- val startTime = System.currentTimeMillis()
- var x = 0
- files.foreach(
- (inputFile: File) => {
- println("Starting conversion of \"" + inputFile.getAbsoluteFile + "\"")
- val y = x
- val factory: ConverterFactory = new ConverterFactory(
- inputFile, (inputFile: String, file: File) => {
- finishedFiles.put(y, file)
- println(inputFile + " converted")
- if (finishedFiles.keySet.size == files.size) {
- concatenateFiles()
- }
- })
- val converter: Runnable = factory.createConverter()
- converterPool.submit(converter)
- x += 1
- })
- def concatenateFiles() {
- val system: OperatingSystem = OperatingSystem.get()
- val outputFile: File = config.output.get
- val factory: ConcatCommandFactory = new ConcatCommandFactory(system, finishedFiles, outputFile)
- val executor: CommandExecutor = system.getExecutor
- val createCommand: Command = factory.createCommand()
- val process: Process = executor.execute(createCommand)
- val reader: StreamReader = new StreamReader(process.getInputStream)
- reader.start()
- process.waitFor()
- val endTime = System.currentTimeMillis().asInstanceOf[Double]
- console.printLine("Finished converting " + x + " files in " + ((endTime - startTime) / 1000d) + " seconds.")
- console.printLine("")
- console.printLine("Cleaning up...")
- new File(factory.temporaryOutput).renameTo(outputFile)
- finishedFiles.values.foreach((file: File) => file.delete())
- console.printLine("Done!")
- console.printLine("")
- console.printLine("Thank you for choosing us!")
- System.exit(0)
- }
- }
- }
- }
- def createParser(): scopt.immutable.OptionParser[ConsoleConfig] = {
- new scopt.immutable.OptionParser[ConsoleConfig]() {
- def options = Seq(
- arglist("Input files", "Input filenames") {
- case (s: String, config: ConsoleConfig) => config.addInputFile(s)
- },
- opt("o", "outputfile", "Output file") {
- case (s: String, config: ConsoleConfig) => config.setOutputFile(s)
- },
- flag("gui", "Graphical interface") {
- case (config: ConsoleConfig) => config.setGraphical()
- }
- )
- }
- }
- def filesExist(args: mutable.MutableList[File]): Boolean = {
- val filtered = args.filterNot((x: File) => x.exists())
- filtered.foreach((x: File) => console.error("File '" + x.getAbsoluteFile + "' does not exist"))
- filtered.size == 0
- }
- }