-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransformer.go
557 lines (482 loc) · 14.6 KB
/
transformer.go
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// Copyright © 2013-2018 Pierre Neidhardt <[email protected]>
// Use of this file is governed by the license that can be found in LICENSE.
package main
import (
"bytes"
"crypto/md5"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"sync"
"github.com/wtolson/go-taglib"
"github.com/yookoala/realpath"
)
var visitedDstCovers = struct {
v map[dstCoverKey]bool
sync.RWMutex
}{v: map[dstCoverKey]bool{}}
// transformer applies the changes resulting from the script run.
// If the audio stream needs to be transcoded, it calls FFmpeg to apply all the changes.
// Otherwise, it copies / renames the file and changes metadata with TagLib if necessary.
type transformer struct{}
func (t *transformer) Init() {}
func (t *transformer) Close() {}
func (t *transformer) Run(fr *FileRecord) error {
input := &fr.input
for track := 0; track < input.trackCount; track++ {
output := &fr.output[track]
if fr.status[track] == statusFail {
continue
}
err := os.MkdirAll(filepath.Dir(output.Path), 0777)
if err != nil {
fr.error.Print(err)
continue
}
// Create file if necessary.
if fr.status[track] == statusExist {
// If output.Path == input.path && output.Removesource, we process
// in-place.
if output.Write == existWriteSkip && (output.Path != input.path || !output.Removesource) {
if output.Removesource {
// If the user has explicitly requested WriteSkip and
// Removesource, it's probably because the exisintg files
// have priority over the input files.
fr.info.Printf("Remove source %q", input.path)
err := os.Remove(input.path)
if err != nil {
fr.error.Println(err)
return err
}
}
continue
} else if output.Write == existWriteSuffix && (!output.Removesource || output.Path != input.path) {
output.Path, err = mkTemp(output.Path)
if err != nil {
fr.error.Print(err)
continue
}
} else if output.Write == existWriteOver && !output.Removesource && output.Path == input.path {
continue
}
} else {
// 'output.Path' does not exist.
st, err := os.Stat(input.path)
if err != nil {
fr.error.Print(err)
// This error will probably happen for the remaining files of the loop.
// Let's return now.
return err
}
f, err := os.OpenFile(output.Path, os.O_CREATE|os.O_EXCL, st.Mode())
if err != nil {
// Either the parent folder is not writable, or a race condition happened:
// another file with the same path was created between existence check and
// creation.
fr.error.Print(err)
continue
}
f.Close()
}
// If encoding changed, use FFmpeg. Otherwise, copy/rename the file to
// speed up the process. If tags have changed but not the encoding, we use
// taglib to set them.
var encodingChanged = false
if input.trackCount > 1 {
// Split cue-sheet.
encodingChanged = true
}
if fr.Format.FormatName != output.Format {
encodingChanged = true
}
if len(output.Parameters) != 2 ||
output.Parameters[0] != "-c:a" ||
output.Parameters[1] != "copy" {
encodingChanged = true
}
// TODO: TagLib does not support arbitrary tags from its C interface.
// It can tag inplace which offers a significant speedup. The
// 'taglibSupported' is a workaround used to check whether FFmpeg should be
// used or not to ensure correct results.
var taglibFormats = map[string]bool{
"album": true,
"artist": true,
"comment": true,
"genre": true,
"title": true,
// 'date' and 'track' are handled separately because TagLib only supports
// integers for those tags.
}
var taglibSupported = true
for k, v := range input.tags {
if k != "encoder" && output.Tags[k] != v {
if k == "date" || k == "track" {
if _, err := strconv.Atoi(v); err != nil {
taglibSupported = false
break
}
} else if !taglibFormats[k] {
taglibSupported = false
break
}
}
}
if taglibSupported {
for k, v := range output.Tags {
if k != "encoder" && input.tags[k] != v {
if k == "date" || k == "track" {
if _, err := strconv.Atoi(v); err != nil {
taglibSupported = false
break
}
} else if !taglibFormats[k] {
taglibSupported = false
break
}
}
}
}
// Copy embeddedCovers, externalCovers and onlineCover.
// We must process covers now because the input file can be removed after audio processing.
for stream, cover := range output.EmbeddedCovers {
inputSource := bytes.NewBuffer(fr.embeddedCoverCache[stream])
transferCovers(fr, cover, "embedded "+strconv.Itoa(stream), inputSource, input.embeddedCovers[stream].checksum)
}
for file, cover := range output.ExternalCovers {
inputPath := filepath.Join(filepath.Dir(input.path), file)
inputSource, err := os.Open(inputPath)
if err != nil {
return err
}
transferCovers(fr, cover, "external '"+file+"'", inputSource, input.externalCovers[file].checksum)
inputSource.Close()
}
{
inputSource := bytes.NewBuffer(fr.onlineCoverCache)
transferCovers(fr, output.OnlineCover, "online", inputSource, input.onlineCover.checksum)
}
// TODO: Add to condition: `|| output.format == "taglib-unsupported-format"`.
if encodingChanged || !taglibSupported {
err = transformStream(fr, track)
} else {
err = transformMetadata(fr, track)
}
if err != nil {
fr.error.Print(err)
continue
}
}
return nil
}
func transformStream(fr *FileRecord, track int) error {
input := &fr.input
output := &fr.output[track]
// Store encoding parameters.
ffmpegParameters := []string{}
// Be verbose only when running a single process. Otherwise output gets
// would get messy.
if options.Cores > 1 {
ffmpegParameters = append(ffmpegParameters, "-v", "warning")
} else {
ffmpegParameters = append(ffmpegParameters, "-v", "error")
}
// By default, FFmpeg reads stdin while running. Disable this feature to
// avoid unexpected problems.
ffmpegParameters = append(ffmpegParameters, "-nostdin")
// FFmpeg should always overwrite: if a temp file is created to avoid
// overwriting, FFmpeg should clobber it.
ffmpegParameters = append(ffmpegParameters, "-y")
ffmpegParameters = append(ffmpegParameters, "-i", input.path)
// Stream codec.
ffmpegParameters = append(ffmpegParameters, output.Parameters...)
// Get cuesheet splitting parameters.
if len(input.cuesheet.Files) > 0 {
d, _ := strconv.ParseFloat(fr.Streams[input.audioIndex].Duration, 64)
start, duration := ffmpegSplitTimes(input.cuesheet, input.cuesheetFile, track, d)
ffmpegParameters = append(ffmpegParameters, "-ss", start, "-t", duration)
}
// If there are no covers, do not copy any video stream to avoid errors.
if fr.Format.NbStreams < 2 {
ffmpegParameters = append(ffmpegParameters, "-vn")
}
// Remove non-cover streams and extra audio streams.
// Must add all streams first.
ffmpegParameters = append(ffmpegParameters, "-map", "0")
for i := 0; i < fr.Format.NbStreams; i++ {
if (fr.Streams[i].CodecType == "video" && fr.Streams[i].CodecName != "image2" && fr.Streams[i].CodecName != "png" && fr.Streams[i].CodecName != "mjpeg") ||
(fr.Streams[i].CodecType == "audio" && i > input.audioIndex) ||
(fr.Streams[i].CodecType != "audio" && fr.Streams[i].CodecType != "video") {
ffmpegParameters = append(ffmpegParameters, "-map", "-0:"+strconv.Itoa(i))
}
}
// Remove subtitles if any.
ffmpegParameters = append(ffmpegParameters, "-sn")
// '-map_metadata -1' clears all metadata first.
ffmpegParameters = append(ffmpegParameters, "-map_metadata", "-1")
for tag, value := range output.Tags {
ffmpegParameters = append(ffmpegParameters, "-metadata", tag+"="+value)
}
// Format.
ffmpegParameters = append(ffmpegParameters, "-f", output.Format)
// Output file.
// FFmpeg cannot transcode inplace, so we force creating a temp file if
// necessary.
dst := output.Path
if input.path == output.Path {
var err error
dst, err = mkTemp(output.Path)
if err != nil {
fr.error.Print(err)
return err
}
}
ffmpegParameters = append(ffmpegParameters, dst)
fr.debug.Printf("FFmpeg parameters: track #%v %q", track, ffmpegParameters)
cmd := exec.Command("ffmpeg", ffmpegParameters...)
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fr.error.Printf(stderr.String())
return err
}
if input.path == output.Path {
fr.debug.Printf("Rename %q to %q to transform inplace", dst, input.path)
err = os.Rename(dst, input.path)
if err != nil {
fr.error.Print(err)
return err
}
} else if output.Removesource {
fr.info.Printf("Remove source %q", input.path)
err := os.Remove(input.path)
if err != nil {
fr.error.Println(err)
return err
}
}
return nil
}
func transformMetadata(fr *FileRecord, track int) error {
input := &fr.input
output := &fr.output[track]
var err error
if input.path != output.Path {
// Rename or copy file.
if output.Removesource {
fr.debug.Printf("Rename %q to %q", input.path, output.Path)
err = os.Rename(input.path, output.Path)
}
if err != nil || !output.Removesource {
// If renaming failed, it might be because of a cross-device
// destination. We try to copy instead.
fr.debug.Printf("Copy %q to %q", input.path, output.Path)
err := CopyFile(output.Path, input.path)
if err != nil {
fr.error.Println(err)
return err
}
if output.Removesource {
fr.debug.Printf("Remove source %q", input.path)
err = os.Remove(input.path)
if err != nil {
fr.error.Println(err)
}
}
}
}
var tagsChanged = false
for k, v := range input.tags {
if k != "encoder" && output.Tags[k] != v {
tagsChanged = true
break
}
}
if !tagsChanged {
for k, v := range output.Tags {
if k != "encoder" && input.tags[k] != v {
tagsChanged = true
break
}
}
}
if tagsChanged {
fr.debug.Print("Set tags with TagLib")
f, err := taglib.Read(output.Path)
if err != nil {
fr.error.Print(err)
return err
}
defer f.Close()
if output.Tags["album"] != "" {
f.SetAlbum(output.Tags["album"])
}
if output.Tags["artist"] != "" {
f.SetArtist(output.Tags["artist"])
}
if output.Tags["comment"] != "" {
f.SetComment(output.Tags["comment"])
}
if output.Tags["genre"] != "" {
f.SetGenre(output.Tags["genre"])
}
if output.Tags["title"] != "" {
f.SetTitle(output.Tags["title"])
}
if output.Tags["track"] != "" {
t, _ := strconv.Atoi(output.Tags["track"])
// There is no need to check for errors as the caller has already.
f.SetTrack(t)
}
if output.Tags["date"] != "" {
t, _ := strconv.Atoi(output.Tags["date"])
// There is no need to check for errors as the caller has already.
f.SetYear(t)
}
err = f.Save()
if err != nil {
fr.error.Print(err)
}
}
return nil
}
// mkTemp creates a temp file by appending a random suffix to 'dst' while
// preserving its extension. Return the name of the temp file.
func mkTemp(dst string) (temp string, err error) {
f, err := TempFile(filepath.Dir(dst), StripExt(filepath.Base(dst))+"_", "."+Ext(dst))
if err != nil {
return "", err
}
temp = f.Name()
f.Close()
return temp, nil
}
// Create a new destination file 'dst'. See makeTrackDst.
// As a special case, if the checksums match in input and dst, return "", nil.
// TODO: Test how memoization scales with visitedDstCovers.
func makeCoverDst(fr *FileRecord, dst string, inputPath string, checksum string) (string, error) {
if st, err := os.Stat(dst); err == nil || !os.IsNotExist(err) {
// 'dst' exists.
// Realpath is required for cache key uniqueness.
dst, err = realpath.Realpath(dst)
if err != nil {
return "", err
}
visitedDstCovers.RLock()
visited := visitedDstCovers.v[dstCoverKey{path: dst, checksum: checksum}]
visitedDstCovers.RUnlock()
if visited {
return "", nil
}
visitedDstCovers.Lock()
visitedDstCovers.v[dstCoverKey{path: dst, checksum: checksum}] = true
visitedDstCovers.Unlock()
// Compute checksum of existing cover and early-out if equal.
fd, err := os.Open(dst)
if err != nil {
return "", err
}
defer fd.Close()
// TODO: Cache checksums.
hi := st.Size()
if hi > coverChecksumBlock {
hi = coverChecksumBlock
}
buf := [coverChecksumBlock]byte{}
_, err = (*fd).ReadAt(buf[:hi], 0)
if err != nil && err != io.EOF {
return "", err
}
dstChecksum := fmt.Sprintf("%x", md5.Sum(buf[:hi]))
if checksum == dstChecksum {
return "", nil
}
// If not inplace, create a temp file.
f, err := TempFile(filepath.Dir(dst), StripExt(filepath.Base(dst))+"_", "."+Ext(dst))
if err != nil {
return "", err
}
dst = f.Name()
f.Close()
} else {
// 'dst' does not exist.
st, err := os.Stat(inputPath)
if err != nil {
return "", err
}
err = os.MkdirAll(filepath.Dir(dst), 0777)
if err != nil {
return "", err
}
fd, err := os.OpenFile(dst, os.O_CREATE|os.O_EXCL, st.Mode())
if err != nil {
// Either the parent folder is not writable, or a race condition happened:
// file was created between existence check and file creation.
return "", err
}
fd.Close()
// Save to cache.
dst, err = realpath.Realpath(dst)
if err != nil {
return "", err
}
visitedDstCovers.Lock()
visitedDstCovers.v[dstCoverKey{path: dst, checksum: checksum}] = true
visitedDstCovers.Unlock()
}
return dst, nil
}
func transferCovers(fr *FileRecord, cover outputCover, coverName string, inputSource io.Reader, checksum string) {
if cover.Path == "" {
return
}
if len(cover.Parameters) == 0 || cover.Format == "" {
coverNewPath, err := makeCoverDst(fr, cover.Path, fr.input.path, checksum)
if err != nil {
fr.error.Print(err)
return
}
if coverNewPath == "" {
fr.debug.Printf("Cover %s skipped, identical file exists in %v", coverName, cover.Path)
return
}
fd, err := os.OpenFile(coverNewPath, os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
fr.warning.Println(err)
return
}
fr.info.Printf("Cover %v -> %s", coverName, coverNewPath)
if _, err = io.Copy(fd, inputSource); err != nil {
fr.warning.Println(err)
return
}
fd.Close()
} else {
coverNewPath, err := makeCoverDst(fr, cover.Path, fr.input.path, checksum)
if err != nil {
fr.error.Print(err)
return
}
if coverNewPath == "" {
fr.debug.Printf("Cover %s skipped, identical file exists in %v", coverName, cover.Path)
return
}
cmdArray := []string{"-nostdin", "-v", "error", "-y", "-i", "-", "-an", "-sn"}
cmdArray = append(cmdArray, cover.Parameters...)
cmdArray = append(cmdArray, "-f", cover.Format, coverNewPath)
fr.info.Printf("Cover %v -> %s", coverName, coverNewPath)
fr.debug.Printf("FFmpeg parameters: %q", cmdArray)
cmd := exec.Command("ffmpeg", cmdArray...)
var stderr bytes.Buffer
cmd.Stderr = &stderr
cmd.Stdin = inputSource
_, err = cmd.Output()
if err != nil {
fr.warning.Printf(stderr.String())
return
}
}
}