-
Notifications
You must be signed in to change notification settings - Fork 33
/
process.go
92 lines (84 loc) · 1.98 KB
/
process.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
package streamer
import (
"fmt"
"os"
"os/exec"
)
// IProcess is an interface around the FFMPEG process
type IProcess interface {
Spawn(path, URI string) *exec.Cmd
}
// ProcessLoggingOpts describes options for process logging
type ProcessLoggingOpts struct {
Enabled bool // Option to set logging for transcoding processes
Directory string // Directory for the logs
MaxSize int // Maximum size of kept logging files in megabytes
MaxBackups int // Maximum number of old log files to retain
MaxAge int // Maximum number of days to retain an old log file.
Compress bool // Indicates if the log rotation should compress the log files
}
// Process is the main type for creating new processes
type Process struct {
keepFiles bool
audio bool
}
// Type check
var _ IProcess = (*Process)(nil)
// NewProcess creates a new process able to spawn transcoding FFMPEG processes
func NewProcess(
keepFiles bool,
audio bool,
) *Process {
return &Process{keepFiles, audio}
}
// getHLSFlags are for getting the flags based on the config context
func (p Process) getHLSFlags() string {
if p.keepFiles {
return "append_list"
}
return "delete_segments+append_list"
}
// Spawn creates a new FFMPEG cmd
func (p Process) Spawn(path, URI string) *exec.Cmd {
os.MkdirAll(path, os.ModePerm)
processCommands := []string{
"-y",
"-fflags",
"nobuffer",
"-rtsp_transport",
"tcp",
"-i",
URI,
"-f",
"lavfi",
"-i",
"anullsrc=channel_layout=stereo:sample_rate=44100",
"-vsync",
"0",
"-copyts",
"-vcodec",
"copy",
"-movflags",
"frag_keyframe+empty_moov",
}
if (!p.audio) {
processCommands = append(processCommands, "-an")
}
processCommands = append(processCommands,
"-hls_flags",
p.getHLSFlags(),
"-f",
"hls",
"-segment_list_flags",
"live",
"-hls_time",
"1",
"-hls_list_size",
"3",
"-hls_segment_filename",
fmt.Sprintf("%s/%%d.ts", path),
fmt.Sprintf("%s/index.m3u8", path),
)
cmd := exec.Command("ffmpeg", processCommands...)
return cmd
}