-
Notifications
You must be signed in to change notification settings - Fork 0
/
music.go
129 lines (113 loc) · 2.92 KB
/
music.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Copyright (c) 2017 Ethan Wells
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"errors"
"io"
"net/http"
"time"
"github.com/bwmarrin/discordgo"
"github.com/jonas747/dca"
"github.com/rylio/ytdl"
)
type ObjectResponse struct {
Resp *http.Response
Name string
}
type Streamer struct {
Url string
GuildID string
ChannelID string
S *discordgo.Session
}
const (
initZingMp3 string = "zing"
initYoutube string = "youtube"
initSoundCloud string = "soundcloud"
initFacebook string = "facebook"
initVimeo string = "vimeo"
)
var (
blocker = make(chan bool, 1)
Stop = make(map[string]bool)
pause = make(map[string]bool)
exitQ = false
Streams = make(map[string]*Streamer)
)
func (s *Streamer) Stream() error {
return Stream(s.Url, s.GuildID, s.ChannelID, s.S)
}
func UrlFromSearch(tag string) (string, error) {
return YTSearch(tag)
}
func Stream(videoURL, guildID, channelID string, s *discordgo.Session) error {
options := dca.StdEncodeOptions
options.RawOutput = true
options.Bitrate = 96
options.Application = "lowdelay"
videoInfo, err := ytdl.GetVideoInfo(videoURL)
if err != nil {
return errors.New("Get video info " + err.Error())
}
formats := videoInfo.Formats.Best(ytdl.FormatAudioBitrateKey)
if len(formats) < 1 {
return errors.New("link error (no formats)")
}
format := formats[0]
downloadURL, err := videoInfo.GetDownloadURL(format)
if err != nil {
return errors.New("Download URL " + err.Error())
}
encodingSession, err := dca.EncodeFile(downloadURL.String(), options)
if err != nil {
return errors.New("Encoding session " + err.Error())
}
defer func() {
encodingSession.Cleanup()
<-blocker
Stop[guildID] = false
}()
blocker <- true
if exitQ {
return nil
}
var vc *discordgo.VoiceConnection
defer func() {
vc.Speaking(false)
vc.Disconnect()
}()
vc, err = s.ChannelVoiceJoin(guildID, channelID, false, true)
if err != nil {
return errors.New("Join voice channel " + err.Error())
}
vc.Speaking(true)
done := make(chan error)
session := dca.NewStream(encodingSession, vc, done)
go func() {
err = <-done
Stop[guildID] = true
}()
for !Stop[guildID] {
time.Sleep(250 * time.Millisecond)
session.SetPaused(pause[guildID])
vc.Speaking(pause[guildID])
}
if err != nil && err != io.EOF {
return err
}
return nil
}