internal/player.go
149 lines · 3792 bytes
1package internal
2
3import (
4 // "fmt"
5 "crypto/rand"
6 "encoding/json"
7 "fmt"
8 "net"
9 "os/exec"
10)
11var logFile = "debug.log"
12
13func StartVideo(link string, args []string) (string, error) {
14 // Generate a random number for the socket path
15 randomBytes := make([]byte, 4)
16 _, err := rand.Read(randomBytes)
17 if err != nil {
18 Log("Failed to generate random number", logFile)
19 return "", fmt.Errorf("failed to generate random number: %w", err)
20 }
21 randomNumber := fmt.Sprintf("%x", randomBytes)
22
23 // Create the mpv socket path with the random number
24 mpvSocketPath := fmt.Sprintf("/tmp/curd/curd_mpvsocket_%s", randomNumber)
25 argsStr := ""
26 if len(args) > 0 {
27 argsStr = " " + joinArgs(args)
28 }
29
30 // Build the complete command string
31 command := fmt.Sprintf("mpv%s --no-terminal --really-quiet --input-ipc-server=%s %s", argsStr, mpvSocketPath, link)
32
33 // Execute the command using exec
34 cmd := exec.Command("bash", "-c", command)
35 cmd.Start()
36
37 return mpvSocketPath, nil
38}
39
40// Helper function to join args with a space
41func joinArgs(args []string) string {
42 result := ""
43 for i, arg := range args {
44 if i > 0 {
45 result += " "
46 }
47 result += arg
48 }
49 return result
50}
51
52func MPVSendCommand(ipcSocketPath string, command []interface{}) (interface{}, error) {
53 conn, err := net.Dial("unix", ipcSocketPath)
54 if err != nil {
55 return nil, err
56 }
57 defer conn.Close()
58
59 commandStr, err := json.Marshal(map[string]interface{}{
60 "command": command,
61 })
62 if err != nil {
63 return nil, err
64 }
65
66 // Send the command
67 conn.Write(append(commandStr, '\n'))
68
69 // Receive the response
70 buf := make([]byte, 4096)
71 n, err := conn.Read(buf)
72 if err != nil {
73 return nil, err
74 }
75
76 var response map[string]interface{}
77 if err := json.Unmarshal(buf[:n], &response); err != nil {
78 return nil, err
79 }
80
81 if data, exists := response["data"]; exists {
82 return data, nil
83 }
84
85 return nil, nil
86}
87
88func SeekMPV(ipcSocketPath string, time int) (interface{}, error) {
89 command := []interface{}{"seek", time, "absolute"}
90 return MPVSendCommand(ipcSocketPath, command)
91}
92
93func GetMPVPausedStatus(ipcSocketPath string) (bool, error) {
94 status, err := MPVSendCommand(ipcSocketPath, []interface{}{"get_property", "pause"})
95 if err != nil || status == nil {
96 return false, err
97 }
98
99 paused, ok := status.(bool)
100 if ok {
101 return paused, nil
102 }
103 return false, nil
104}
105
106func GetMPVPlaybackSpeed(ipcSocketPath string) (float64, error) {
107 speed, err := MPVSendCommand(ipcSocketPath, []interface{}{"get_property", "speed"})
108 if err != nil || speed == nil {
109 fmt.Println("Failed to get playback speed.")
110 return 0, err
111 }
112
113 currentSpeed, ok := speed.(float64)
114 if ok {
115 return currentSpeed, nil
116 }
117
118 return 0, nil
119}
120
121func GetPercentageWatched(ipcSocketPath string) (float64, error) {
122 currentTime, err := MPVSendCommand(ipcSocketPath, []interface{}{"get_property", "time-pos"})
123 if err != nil || currentTime == nil {
124 return 0, err
125 }
126
127 duration, err := MPVSendCommand(ipcSocketPath, []interface{}{"get_property", "duration"})
128 if err != nil || duration == nil {
129 return 0, err
130 }
131
132 currTime, ok1 := currentTime.(float64)
133 dur, ok2 := duration.(float64)
134
135 if ok1 && ok2 && dur > 0 {
136 percentageWatched := (currTime / dur) * 100
137 return percentageWatched, nil
138 }
139
140 return 0, nil
141}
142
143func PercentageWatched(playbackTime int, duration int) *float64 {
144 if duration > 0 {
145 percentage := (float64(playbackTime) / float64(duration)) * 100
146 return &percentage
147 }
148 return nil
149}