-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.go
82 lines (67 loc) · 1.65 KB
/
player.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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"syscall"
"github.com/LucasSnatiago/golofiplayer/internal/videos"
)
func ChooseMusic() string {
music_option := 0
for music_option < 1 || music_option > videos.GetNumMusicLinks() {
fmt.Print(videos.HELP_MESSAGE)
fmt.Scanf("%d", &music_option)
}
var music_link string
switch music_option {
case 1:
music_link = videos.LOFI
case 2:
music_link = videos.LOFI_PIANO
case 3:
music_link = videos.LOFI_DARK
case 4:
music_link = videos.LOFI_SYNCWAVE
case 5:
music_link = videos.LOFI_HIPHOP
case 6:
music_link = videos.BOYCE_AVENUE
}
return music_link
}
func Play() error {
song := ChooseMusic()
ytdlp := exec.Command("yt-dlp", "-f", "bestaudio", "--yes-playlist", song, "-o", "-")
ffplay := exec.Command("ffplay", "-nodisp", "-volume", "25", "-i", "-")
ytdlpOut, err := ytdlp.StdoutPipe()
if err != nil {
return fmt.Errorf("failed to create pipe for yt-dlp stdout: %v", err)
}
defer ytdlpOut.Close()
ffplay.Stdin = ytdlpOut
if err := ytdlp.Start(); err != nil {
return fmt.Errorf("failed to start yt-dlp: %v", err)
}
defer func() {
if err := ytdlp.Process.Kill(); err != nil {
log.Printf("failed to kill yt-dlp process: %v", err)
}
ytdlp.Wait()
}()
if err := ffplay.Start(); err != nil {
return fmt.Errorf("failed to start ffplay: %v", err)
}
defer func() {
if err := ffplay.Process.Kill(); err != nil {
log.Printf("failed to kill ffplay process: %v", err)
}
ffplay.Wait()
}()
// Esperar até que um sinal seja recebido para finalizar a execução
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
return nil
}