-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ffmpeg.go
76 lines (65 loc) · 1.23 KB
/
ffmpeg.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
package main
import (
"errors"
"os"
"os/exec"
"sync"
"syscall"
"time"
)
func New(url []string, path string) *FFmpeg {
cmd := exec.Command("/ffmpeg", url...)
return &FFmpeg{
cmd,
&sync.RWMutex{},
false,
time.Now(),
path,
}
}
type FFmpeg struct {
cmd *exec.Cmd
mu *sync.RWMutex
running bool
hit time.Time
path string
}
func (f *FFmpeg) Start() error {
if f.running {
// log.Printf("running.....")
return nil
}
f.cmd.Stdout = os.Stdout
f.cmd.Stderr = os.Stderr
f.cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
Pdeathsig: syscall.SIGKILL,
}
f.cmd.Dir = f.path
// log.Printf("Starting FFmpeg process: %s", f.cmd.Dir)
f.running = true
f.cmd.Start()
// log.Printf("%v", f.cmd.Start())
f.Hit()
return errors.New("FFmpeg process started")
}
func (f *FFmpeg) Stop() error {
if f.running == false {
return nil
}
f.running = false
return f.cmd.Process.Signal(syscall.SIGKILL)
}
func (f *FFmpeg) Wait() error {
return f.cmd.Wait()
}
func (f *FFmpeg) IsRunning() bool {
return f.running
}
func (f *FFmpeg) Hit() {
f.hit = time.Now()
}
func (f *FFmpeg) HitExpired() bool {
// return time.Now().Sub(f.hit).Minutes() > 0
return time.Now().Sub(f.hit).Seconds() > 120 && f.running
}