summaryrefslogtreecommitdiff
path: root/rhimport/converter.go
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2016-06-30 01:56:16 (GMT)
committerChristian Pointner <equinox@helsinki.at>2016-06-30 01:56:16 (GMT)
commit471c9224bd1952671179263ebce8a15656d474d1 (patch)
treefa0fb8d6f295588ed2d7247b5c28edabc1808cb3 /rhimport/converter.go
parent7538b0e7d2fc5b0e0ed2d26efd67ce1ffc9c006c (diff)
added converter fetcher
Diffstat (limited to 'rhimport/converter.go')
-rw-r--r--rhimport/converter.go115
1 files changed, 115 insertions, 0 deletions
diff --git a/rhimport/converter.go b/rhimport/converter.go
new file mode 100644
index 0000000..4b68faa
--- /dev/null
+++ b/rhimport/converter.go
@@ -0,0 +1,115 @@
+//
+// rhimportd
+//
+// The Radio Helsinki Rivendell Import Daemon
+//
+//
+// Copyright (C) 2015-2016 Christian Pointner <equinox@helsinki.at>
+//
+// This file is part of rhimportd.
+//
+// rhimportd is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// any later version.
+//
+// rhimportd 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 General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with rhimportd. If not, see <http://www.gnu.org/licenses/>.
+//
+
+package rhimport
+
+import (
+ "io"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "strings"
+)
+
+type Converter interface {
+ io.WriteCloser
+ GetResult() (result string, err error)
+}
+
+type ConverterResult struct {
+ output string
+ err error
+}
+
+//
+// NUll Converter aka File Writer
+//
+
+type NullConverter struct {
+ file *os.File
+}
+
+func NewNullConverter(filename string) (n *NullConverter, newFilename string, err error) {
+ n = &NullConverter{}
+ rhl.Printf("null-converter: opening file '%s'", filename)
+ newFilename = filename
+ n.file, err = os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
+ return
+}
+
+func (c *NullConverter) Write(p []byte) (n int, err error) {
+ return c.file.Write(p)
+}
+
+func (c *NullConverter) Close() (err error) {
+ return c.file.Close()
+}
+
+func (c *NullConverter) GetResult() (result string, err error) {
+ return "", nil
+}
+
+//
+// FFMpeg Converter: converts all files into flac
+//
+
+type FFMpegConverter struct {
+ cmd *exec.Cmd
+ pipe io.WriteCloser
+ result chan ConverterResult
+}
+
+func NewFFMpegConverter(filename string) (ff *FFMpegConverter, filenameFlac string, err error) {
+ ff = &FFMpegConverter{}
+ ext := filepath.Ext(filename)
+ filenameFlac = strings.TrimSuffix(filename, ext) + ".flac"
+ rhl.Printf("ffmpeg-converter: starting ffmpeg for file '%s' (had extension: '%s')", filenameFlac, ext)
+ ff.cmd = exec.Command("ffmpeg", "-loglevel", "warning", "-i", "-", "-map_metadata", "0", "-f", "flac", filenameFlac)
+ if ff.pipe, err = ff.cmd.StdinPipe(); err != nil {
+ return nil, "", err
+ }
+
+ ff.result = make(chan ConverterResult, 1)
+ go func() {
+ output, err := ff.cmd.CombinedOutput()
+ ff.result <- ConverterResult{strings.TrimSpace(string(output)), err}
+ }()
+ return
+}
+
+func (ff *FFMpegConverter) Write(p []byte) (n int, err error) {
+ return ff.pipe.Write(p)
+}
+
+func (ff *FFMpegConverter) Close() (err error) {
+ return ff.pipe.Close()
+}
+
+func (ff *FFMpegConverter) GetResult() (result string, err error) {
+ if ff.result != nil {
+ r := <-ff.result
+ return r.output, r.err
+ }
+ return "", nil
+}