Transformations.scala 1.55 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * Extracts media files from Fedora repository
 * Copyright (C) 2020  Memoriav
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
19
package org.memobase
20

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
21
import java.io.IOException
22
23
24

import org.apache.logging.log4j.scala.Logging

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
25
import scala.util.Try
26

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
27
object Transformations extends Logging {
28

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
29
  import sys.process._
30

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
31
32
33
34
35
36
37
38
39
  private def wrapCommand(command: String): Try[Int] = Try {
    val stderr = StringBuilder.newBuilder
    val errorCode = command ! ProcessLogger(logger.debug(_), stderr append _)
    if (errorCode > 0) {
      throw new IOException(s"application ${command.split(' ')(0)} exited with code $errorCode: ${stderr.toString()}")
    } else {
      errorCode
    }
  }
40

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
41
42
43
44
45
46
  def mp3ToMp4(sourceFile: String, destFile: String): Try[String] = {
    val externalCommand = s"ffmpeg -i $sourceFile -acodec copy -loglevel warning -hide_banner -y -movflags faststart $destFile"
    Try {
      wrapCommand(externalCommand).get
      destFile
    }
47
  }
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
48

49
}