-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9195b02
commit 55f3d65
Showing
11 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
*.class | ||
*.log | ||
target | ||
.ensime* | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
maxColumn = 120 | ||
project.excludeFilters = [".ensime_cache"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
language: scala | ||
scala: | ||
- 2.10.4 | ||
- 2.11.2 | ||
branches: | ||
only: | ||
- master | ||
notifications: | ||
email: | ||
recipients: | ||
- [email protected] | ||
jdk: | ||
- oraclejdk7 | ||
- oraclejdk8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Dependencies._ | ||
|
||
lazy val root = (project in file(".")).settings( | ||
inThisBuild( | ||
List( | ||
organization := "tech.profitware", | ||
scalaVersion := "2.12.3", | ||
version := "0.1.0-SNAPSHOT" | ||
)), | ||
name := "AudioConverter", | ||
libraryDependencies += scalaTest % Test | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import sbt._ | ||
|
||
object Dependencies { | ||
lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.0.3" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version=1.0.2 |
56 changes: 56 additions & 0 deletions
56
src/main/scala/tech/profitware/audioconverter/Converter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package tech.profitware.audioconverter | ||
|
||
import scala.sys.process._ | ||
import java.io.IOException | ||
import java.io.File | ||
|
||
trait Conversion { | ||
val sourceExtension: String | ||
val destinationFormat: String | ||
val destinationExtension: String | ||
def isPresent: Boolean | ||
def alertNotInstalled(): Unit | ||
def convertFile(file: File, directory: Directory): Boolean | ||
} | ||
|
||
trait FFMpegConversion extends Conversion with Logger { | ||
def isPresent = | ||
try { | ||
("ffmpeg -version" #> new File("/dev/null")).! == 0 | ||
} catch { | ||
case ioe: IOException => false | ||
} | ||
def alertNotInstalled() = log("FFMpeg is not installed!") | ||
def convertFile(file: File, directory: Directory) = { | ||
val absoluteFile = file.getAbsoluteFile | ||
val album = absoluteFile.getParentFile.getName | ||
val destionationDirectory = | ||
new File(directory.destinationArtistDirectory.getAbsolutePath, album) | ||
val destinationFile = new File(destionationDirectory, s"${file.getName}.${destinationExtension}").getAbsolutePath | ||
|
||
val executableSeq = Seq("ffmpeg", "-i", s"${absoluteFile.getPath}", "-acodec", destinationFormat, destinationFile) | ||
val executable = Process(executableSeq, absoluteFile.getParentFile) | ||
|
||
try { | ||
destionationDirectory.mkdirs() | ||
("echo y" #| executable).! == 0 | ||
} catch { | ||
case ioe: IOException => false | ||
} | ||
} | ||
} | ||
|
||
trait FFMpegFlacToAlacConversion extends FFMpegConversion { | ||
val sourceExtension = "flac" | ||
val destinationFormat = "alac" | ||
val destinationExtension = "m4a" | ||
} | ||
|
||
abstract class Converter(val directory: Directory) extends Conversion with Logger { | ||
def convert() = | ||
directory.getSourceDirectoryTree.par | ||
.filter(_.getName().endsWith(s".${sourceExtension}")) | ||
.map( | ||
currentFile => convertFile(currentFile, directory) | ||
) | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/scala/tech/profitware/audioconverter/Directory.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package tech.profitware.audioconverter | ||
|
||
import java.io.File | ||
|
||
trait DestinationDirectory { | ||
def absoluteDestinationDirectory: File | ||
} | ||
|
||
trait MusicDestinationDirectory extends DestinationDirectory { | ||
def absoluteDestinationDirectory = new File( | ||
System.getProperty("user.home"), | ||
"Music" | ||
) | ||
} | ||
|
||
abstract class Directory(val sourceDirectory: String) extends DestinationDirectory with Logger { | ||
val absoluteSourceDirectory = new File(sourceDirectory).getAbsoluteFile | ||
|
||
log(s"Using ${absoluteSourceDirectory.getAbsolutePath} directory as a source") | ||
|
||
val destinationArtistDirectory = new File( | ||
absoluteDestinationDirectory.getAbsolutePath, | ||
absoluteSourceDirectory.getName | ||
) | ||
|
||
// Source: https://stackoverflow.com/a/7264833 | ||
def getFileTree(f: File): Stream[File] = f #:: { | ||
if (f.isDirectory) | ||
f.listFiles().toStream.flatMap(getFileTree) | ||
else | ||
Stream.empty | ||
} | ||
|
||
def getSourceDirectoryTree = getFileTree(absoluteSourceDirectory) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package tech.profitware.audioconverter | ||
|
||
trait Logger { | ||
def log(s: String): Unit | ||
} | ||
|
||
trait SimpleLogger extends Logger { | ||
def log(s: String) = println(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package tech.profitware.audioconverter | ||
|
||
import java.io.File | ||
|
||
object Main extends App { | ||
val sourceDirectoryFile = new File(if (args.length > 0) args(0) else ".") | ||
val sourceDirectory = | ||
if (sourceDirectoryFile.exists) | ||
sourceDirectoryFile.getAbsolutePath | ||
else "." | ||
|
||
val directory = new Directory(sourceDirectory) with MusicDestinationDirectory with SimpleLogger | ||
val converter = new Converter(directory) with FFMpegFlacToAlacConversion with SimpleLogger | ||
|
||
if (converter.isPresent) | ||
converter.convert() | ||
else | ||
converter.alertNotInstalled() | ||
} |
20 changes: 20 additions & 0 deletions
20
src/test/scala/tech/profitware/audioconverter/DirectorySpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package tech.profitware.audioconverter | ||
|
||
import org.scalatest._ | ||
import java.io.File | ||
|
||
class DirectorySpec extends FlatSpec with Matchers { | ||
val currentDirectory = new Directory(".") with MusicDestinationDirectory with SimpleLogger | ||
|
||
"Current directory" should "be absolute" in { | ||
currentDirectory.absoluteSourceDirectory.getAbsolutePath shouldEqual new File(".").getAbsolutePath | ||
} | ||
|
||
"Music destination directory" should "end with Music" in { | ||
currentDirectory.absoluteDestinationDirectory.getName shouldEqual "Music" | ||
} | ||
|
||
"Artist destination directory" should "be equal to current source directory name" in { | ||
currentDirectory.destinationArtistDirectory.getName shouldEqual new File(".").getName | ||
} | ||
} |