-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.go
More file actions
98 lines (77 loc) · 1.94 KB
/
processor.go
File metadata and controls
98 lines (77 loc) · 1.94 KB
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
package main
import (
"fmt"
"github.com/labstack/gommon/log"
"os/exec"
"strconv"
"syscall"
"time"
)
import (
"bytes"
)
type popenInput struct {
args []string
stdin *bytes.Buffer
timeout time.Duration
}
type popenOutput struct {
exitCode int
stdout bytes.Buffer
stderr bytes.Buffer
}
var ffmpegError = fmt.Errorf("ffmpeg command failed")
func resizeMP3(params *requestParams) error {
input := popenInput{stdin: bytes.NewBuffer(params.mp3Original), timeout: config.ServerConvertTimeout}
//Set input
input.args = []string{"-f", "mp3", "-i", "pipe:"}
//Bitrate filter
input.args = append(input.args, "-b:a", strconv.Itoa(params.reBitrate))
//SampleRate filter
input.args = append(input.args, "-ar", strconv.Itoa(params.reOutSampleRate))
//Remove all except audio
input.args = append(input.args, "-map", "a")
//Crop
if params.reDuration > 0 {
input.args = append(input.args, "-t", strconv.Itoa(params.reDuration))
}
//Set output
input.args = append(input.args, "-f", "mp3", "pipe:")
out, err := ffMpegPopen(&input)
if err != nil {
return err
}
if out.exitCode != 0 {
log.Infof("ffmpeg with args: '%s' failed with exit code: %d", input.args, out.exitCode)
return ffmpegError
}
params.mp3Resized = out.stdout.Bytes()
return nil
}
func ffMpegPopen(input *popenInput) (output popenOutput, err error) {
var timer *time.Timer
cmd := exec.Command(config.FFmpegBinary, input.args...)
cmd.Stdin = input.stdin
cmd.Stdout = &output.stdout
cmd.Stderr = &output.stderr
err = cmd.Start()
if err != nil {
return
}
timer = time.AfterFunc(input.timeout, func() {
if input.timeout > time.Duration(0) {
cmd.Process.Kill()
}
})
err = cmd.Wait()
timer.Stop()
if err != nil {
exitError := err.(*exec.ExitError)
ws := exitError.Sys().(syscall.WaitStatus)
output.exitCode = ws.ExitStatus()
} else {
ws := cmd.ProcessState.Sys().(syscall.WaitStatus)
output.exitCode = ws.ExitStatus()
}
return output, nil
}