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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
//
// 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 (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
type fetchConverter interface {
io.WriteCloser
GetResult(ctx *Context, res *Result) (result string, err error)
}
type fetchConverterResult struct {
output string
err error
loudnessCorr float64
}
func newFetchConverter(ctx *Context, filename string) (fetchConverter, string, error) {
switch ctx.FetchConverter {
case "null":
// no support for loudness evaluation - leave normalization to Rivendell
return newNullFetchConverter(ctx, filename)
case "ffmpeg":
// no support for loudness evaluation - leave normalization to Rivendell
return newFFMpegFetchConverter(ctx, filename)
case "bs1770":
ctx.NormalizationLevel = 0 // disable Rivendell normalization
return newBS1770FetchConverter(ctx, filename)
case "ffmpeg-bs1770":
ctx.NormalizationLevel = 0 // disable Rivendell normalization
return newFFMpegBS1770FetchConverter(ctx, filename)
}
return nil, "", errors.New("unknown fetch converter type: " + ctx.FetchConverter)
}
//
// NUll Converter aka File Writer
//
type nullFetchConverter struct {
file *os.File
}
func newNullFetchConverter(ctx *Context, filename string) (n *nullFetchConverter, newFilename string, err error) {
n = &nullFetchConverter{}
newFilename = filepath.Dir(filename) + "/conv-null" + filepath.Ext(filename)
ctx.stdlog.Printf("null-converter: opening file '%s' -> '%s'", filename, newFilename)
n.file, err = os.OpenFile(newFilename, 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(ctx *Context, res *Result) (result string, err error) {
return "", nil
}
//
// FFMpeg Converter: converts all files into flac
//
type ffmpegFetchConverter struct {
cmd *exec.Cmd
pipe io.WriteCloser
result chan fetchConverterResult
}
func newFFMpegFetchConverter(ctx *Context, filename string) (ff *ffmpegFetchConverter, filenameFlac string, err error) {
ff = &ffmpegFetchConverter{}
filenameFlac = filepath.Dir(filename) + "/conv-ffmpeg.flac"
ctx.stdlog.Printf("ffmpeg-converter: starting ffmpeg for file '%s' -> '%s'", filename, filenameFlac)
ff.cmd = exec.Command("ffmpeg", "-loglevel", "warning", "-i", "-", "-map_metadata", "0")
for key, value := range ctx.ExtraMetaData {
ff.cmd.Args = append(ff.cmd.Args, "-metadata", fmt.Sprintf("%s=%s", key, value))
}
ff.cmd.Args = append(ff.cmd.Args, "-ar", strconv.FormatUint(uint64(ctx.conf.SampleRate), 10), "-ac", strconv.FormatUint(uint64(ctx.Channels), 10), "-f", "flac", filenameFlac)
if ff.pipe, err = ff.cmd.StdinPipe(); err != nil {
return nil, "", err
}
ff.result = make(chan fetchConverterResult, 1)
go func() {
output, err := ff.cmd.CombinedOutput()
ff.result <- fetchConverterResult{strings.TrimSpace(string(output)), err, 0.0}
}()
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(ctx *Context, res *Result) (result string, err error) {
if ff.result != nil {
select {
case r := <-ff.result:
ctx.LoudnessCorr = r.loudnessCorr
return r.output, r.err
case <-ctx.Cancel:
ff.cmd.Process.Kill()
res.ResponseCode = http.StatusNoContent
res.ErrorString = "canceled"
return "", errors.New("canceled")
}
}
return "", nil
}
//
// BS1770 Converter: calculates loudness correction value using ITU BS1770 (EBU R128)
//
type bs1770FetchConverter struct {
cmd *exec.Cmd
file *os.File
pipe io.WriteCloser
multi io.Writer
result chan fetchConverterResult
}
func newBS1770FetchConverter(ctx *Context, filename string) (bs *bs1770FetchConverter, newFilename string, err error) {
bs = &bs1770FetchConverter{}
newFilename = filepath.Dir(filename) + "/conv-bs1770" + filepath.Ext(filename)
ctx.stdlog.Printf("bs1770-converter: starting bs1770gain for file '%s' -> '%s'", filename, newFilename)
bs.file, err = os.OpenFile(newFilename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
bs.cmd = exec.Command("bs1770gain", "--ebu", "-i", "--xml", "-")
if bs.pipe, err = bs.cmd.StdinPipe(); err != nil {
return nil, "", err
}
bs.multi = io.MultiWriter(bs.file, bs.pipe)
var bsStdout, bsStderr bytes.Buffer
bs.cmd.Stdout = &bsStdout
bs.cmd.Stderr = &bsStderr
bs.result = make(chan fetchConverterResult, 1)
go func() {
if err := bs.cmd.Run(); err != nil {
bs.result <- fetchConverterResult{strings.TrimSpace(string(bsStderr.String())), err, 0.0}
}
res, err := newBS1770ResultFromXML(&bsStdout)
if err != nil {
bs.result <- fetchConverterResult{bsStdout.String(), err, 0.0}
return
}
if len(res.Album.Tracks) == 0 {
bs.result <- fetchConverterResult{bsStdout.String(), fmt.Errorf("bs1770gain returned no/invalid result"), 0.0}
return
}
bs.result <- fetchConverterResult{"", nil, res.Album.Tracks[0].Integrated.LU}
}()
return
}
func (bs *bs1770FetchConverter) Write(p []byte) (n int, err error) {
return bs.multi.Write(p)
}
func (bs *bs1770FetchConverter) Close() (err error) {
errPipe := bs.pipe.Close()
errFile := bs.file.Close()
if errFile != nil {
return errFile
}
return errPipe
}
func (bs *bs1770FetchConverter) GetResult(ctx *Context, res *Result) (result string, err error) {
if bs.result != nil {
select {
case r := <-bs.result:
ctx.LoudnessCorr = r.loudnessCorr
return r.output, r.err
case <-ctx.Cancel:
bs.cmd.Process.Kill()
res.ResponseCode = http.StatusNoContent
res.ErrorString = "canceled"
return "", errors.New("canceled")
}
}
return "", nil
}
//
// FFMpeg/BS1770 Converter: converts all files into flac and calculates loudness correction value
// using ITU BS1770 (EBU R128)
//
type ffmpegBS1770FetchConverter struct {
ffmpeg *exec.Cmd
bs1770 *exec.Cmd
pipe io.WriteCloser
resultFF chan fetchConverterResult
resultBS chan fetchConverterResult
}
func newFFMpegBS1770FetchConverter(ctx *Context, filename string) (ff *ffmpegBS1770FetchConverter, filenameFlac string, err error) {
ff = &ffmpegBS1770FetchConverter{}
filenameFlac = filepath.Dir(filename) + "/conv-ffmpeg-bs1770.flac"
ctx.stdlog.Printf("ffmpeg-bs1770-converter: starting ffmpeg and bs1770gain for file '%s' -> '%s'", filename, filenameFlac)
ff.ffmpeg = exec.Command("ffmpeg", "-loglevel", "warning", "-i", "pipe:0", "-map_metadata", "0")
for key, value := range ctx.ExtraMetaData {
ff.ffmpeg.Args = append(ff.ffmpeg.Args, "-metadata", fmt.Sprintf("%s=%s", key, value))
}
ff.ffmpeg.Args = append(ff.ffmpeg.Args, "-ar", strconv.FormatUint(uint64(ctx.conf.SampleRate), 10), "-ac", strconv.FormatUint(uint64(ctx.Channels), 10), "-f", "flac", filenameFlac)
ff.ffmpeg.Args = append(ff.ffmpeg.Args, "-ar", strconv.FormatUint(uint64(ctx.conf.SampleRate), 10), "-ac", strconv.FormatUint(uint64(ctx.Channels), 10), "-f", "flac", "pipe:1")
if ff.pipe, err = ff.ffmpeg.StdinPipe(); err != nil {
return nil, "", err
}
var ffStderr bytes.Buffer
ff.ffmpeg.Stderr = &ffStderr
ff.bs1770 = exec.Command("bs1770gain", "--ebu", "-i", "--xml", "-")
var ffstdout io.WriteCloser
if ffstdout, err = ff.bs1770.StdinPipe(); err != nil {
return nil, "", err
}
ff.ffmpeg.Stdout = ffstdout
var bsStdout, bsStderr bytes.Buffer
ff.bs1770.Stdout = &bsStdout
ff.bs1770.Stderr = &bsStderr
ff.resultFF = make(chan fetchConverterResult, 1)
ff.resultBS = make(chan fetchConverterResult, 1)
go func() {
err := ff.ffmpeg.Run()
ffstdout.Close()
ff.resultFF <- fetchConverterResult{strings.TrimSpace(string(ffStderr.String())), err, 0.0}
}()
go func() {
if err := ff.bs1770.Run(); err != nil {
ff.resultBS <- fetchConverterResult{strings.TrimSpace(string(bsStderr.String())), err, 0.0}
}
res, err := newBS1770ResultFromXML(&bsStdout)
if err != nil {
ff.resultBS <- fetchConverterResult{bsStdout.String(), err, 0.0}
return
}
if len(res.Album.Tracks) == 0 {
ff.resultBS <- fetchConverterResult{bsStdout.String(), fmt.Errorf("bs1770gain returned no/invalid result"), 0.0}
return
}
ff.resultBS <- fetchConverterResult{"", nil, res.Album.Tracks[0].Integrated.LU}
}()
return
}
func (ff *ffmpegBS1770FetchConverter) Write(p []byte) (n int, err error) {
return ff.pipe.Write(p)
}
func (ff *ffmpegBS1770FetchConverter) Close() (err error) {
return ff.pipe.Close()
}
func (ff *ffmpegBS1770FetchConverter) GetResult(ctx *Context, res *Result) (result string, err error) {
if ff.resultFF == nil || ff.resultBS == nil {
return "", nil
}
var rff, rbs *fetchConverterResult
for {
select {
case r := <-ff.resultFF:
rff = &r
case r := <-ff.resultBS:
rbs = &r
case <-ctx.Cancel:
ff.ffmpeg.Process.Kill()
ff.bs1770.Process.Kill()
res.ResponseCode = http.StatusNoContent
res.ErrorString = "canceled"
return "", errors.New("canceled")
}
if rff != nil && rbs != nil {
break
}
}
if rff.err != nil {
return rff.output, fmt.Errorf("ffmpeg: %v", rff.err)
}
if rbs.err != nil {
return rbs.output, fmt.Errorf("bs1770gain: %v", rbs.err)
}
ctx.LoudnessCorr = rbs.loudnessCorr
return "", nil
}
|