summaryrefslogtreecommitdiff
path: root/rhimport/converter.go
blob: 418d27b1c31e36ae31fd58a0e00d122543e5a994 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
//  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 (
	"errors"
	"io"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

type FetchConverter interface {
	io.WriteCloser
	GetResult() (result string, err error)
}

type ConverterResult struct {
	output string
	err    error
}

func NewFetchConverter(convType, filename string) (FetchConverter, string, error) {
	switch convType {
	case "null":
		return NewNullFetchConverter(filename)
	case "ffmpeg":
		return NewFFMpegFetchConverter(filename)
	}
	return nil, "", errors.New("unknown fetch converter type: " + convType)
}

//
// NUll Converter aka File Writer
//

type NullFetchConverter struct {
	file *os.File
}

func NewNullFetchConverter(filename string) (n *NullFetchConverter, newFilename string, err error) {
	n = &NullFetchConverter{}
	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 *NullFetchConverter) Write(p []byte) (n int, err error) {
	return c.file.Write(p)
}

func (c *NullFetchConverter) Close() (err error) {
	return c.file.Close()
}

func (c *NullFetchConverter) GetResult() (result string, err error) {
	return "", nil
}

//
// FFMpeg Converter: converts all files into flac
//

type FFMpegFetchConverter struct {
	cmd    *exec.Cmd
	pipe   io.WriteCloser
	result chan ConverterResult
}

func NewFFMpegFetchConverter(filename string) (ff *FFMpegFetchConverter, filenameFlac string, err error) {
	ff = &FFMpegFetchConverter{}
	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 *FFMpegFetchConverter) Write(p []byte) (n int, err error) {
	return ff.pipe.Write(p)
}

func (ff *FFMpegFetchConverter) Close() (err error) {
	return ff.pipe.Close()
}

func (ff *FFMpegFetchConverter) GetResult() (result string, err error) {
	if ff.result != nil {
		r := <-ff.result
		return r.output, r.err
	}
	return "", nil
}