forked from vishen/go-chromecast
/
playlist.go
229 lines (209 loc) · 6.16 KB
/
playlist.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright © 2018 Jonathan Pentecost <pentecostjonathan@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"bufio"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/SaschaZ/go-chromecast/ui"
)
type mediaFile struct {
filename string
possibleNumbers []int
}
// playlistCmd represents the playlist command
var playlistCmd = &cobra.Command{
Use: "playlist <directory>",
Short: "Load and play media on the chromecast",
Long: `Load and play media files on the chromecast, this will
start a streaming server locally and serve the media file to the
chromecast.
If the media file is an unplayable media type by the chromecast, this
will attempt to transcode the media file to mp4 using ffmpeg. This requires
that ffmpeg is installed.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
exit("requires exactly one argument, should be the folder to play media from")
}
if fileInfo, err := os.Stat(args[0]); err != nil {
exit("unable to find %q: %v", args[0], err)
} else if !fileInfo.Mode().IsDir() {
exit("%q is not a directory", args[0])
}
app, err := castApplication(cmd, args)
if err != nil {
exit("unable to get cast application: %v", err)
}
contentType, _ := cmd.Flags().GetString("content-type")
transcode, _ := cmd.Flags().GetBool("transcode")
forcePlay, _ := cmd.Flags().GetBool("force-play")
continuePlaying, _ := cmd.Flags().GetBool("continue")
selection, _ := cmd.Flags().GetBool("select")
files, err := ioutil.ReadDir(args[0])
if err != nil {
exit("unable to list files from %q: %v", args[0], err)
}
filesToPlay := make([]mediaFile, 0, len(files))
for _, f := range files {
if !forcePlay && !app.PlayableMediaType(filepath.Join(args[0], f.Name())) {
continue
}
foundNum := false
numPos := 0
foundNumbers := []int{}
for i, c := range f.Name() {
if c < '0' || c > '9' {
if foundNum {
val, _ := strconv.Atoi(f.Name()[numPos:i])
foundNumbers = append(foundNumbers, val)
}
foundNum = false
continue
}
if !foundNum {
numPos = i
foundNum = true
}
}
filesToPlay = append(filesToPlay, mediaFile{
filename: f.Name(),
possibleNumbers: foundNumbers,
})
}
sort.Slice(filesToPlay, func(i, j int) bool {
iNum := filesToPlay[i].possibleNumbers
jNum := filesToPlay[j].possibleNumbers
if len(iNum) == 0 {
return false
}
if len(jNum) == 0 {
return true
}
max := len(iNum)
if len(iNum) < len(jNum) {
max = len(jNum)
}
for vi := 0; vi < max; vi++ {
if len(iNum) <= vi {
return false
}
if len(jNum) <= vi {
return true
}
if iNum[vi] == jNum[vi] {
continue
}
if iNum[vi] > jNum[vi] {
return false
}
return true
}
return true
})
filenames := make([]string, len(filesToPlay))
for i, f := range filesToPlay {
filename := filepath.Join(args[0], f.filename)
filenames[i] = filename
}
indexToPlayFrom := 0
if selection {
outputInfo("Will play the following items, select where to start from:")
for i, f := range filenames {
lastPlayed := "never"
if lp, ok := app.PlayedItems()[f]; ok {
t := time.Unix(lp.Started, 0)
lastPlayed = t.String()
}
outputInfo("%d) %s: last played %q", i+1, f, lastPlayed)
}
reader := bufio.NewReader(os.Stdin)
for {
outputInfo("Enter selection: ")
text, err := reader.ReadString('\n')
if err != nil {
outputError("reading console: %v", err)
continue
}
i, err := strconv.Atoi(strings.TrimSpace(text))
if err != nil {
continue
} else if i < 1 || i > len(filenames) {
continue
}
indexToPlayFrom = i - 1
break
}
} else if continuePlaying {
var lastPlayedStartUnix int64 = 0
var lastPlayedEndUnix int64 = 0
lastPlayedIndex := 0
for i, f := range filenames {
p, ok := app.PlayedItems()[f]
if ok && p.Started > lastPlayedStartUnix {
lastPlayedStartUnix = p.Started
lastPlayedEndUnix = p.Finished
lastPlayedIndex = i
}
}
if lastPlayedIndex > 0 {
if lastPlayedStartUnix < lastPlayedEndUnix {
if len(filenames) > lastPlayedIndex {
// lastPlayedIndex += 1
} else {
lastPlayedIndex = 0
}
}
}
indexToPlayFrom = lastPlayedIndex
}
s := "Attemping to play the following media:"
for _, f := range filenames[indexToPlayFrom:] {
s += "- " + f + " "
}
outputInfo(s)
// Optionally run a UI when playing this media:
runWithUI, _ := cmd.Flags().GetBool("with-ui")
if runWithUI {
go func() {
if err := app.QueueLoad(filenames[indexToPlayFrom:], contentType, transcode); err != nil {
exit("unable to play playlist on cast application: %v", err)
}
}()
ccui, err := ui.NewUserInterface(app)
if err != nil {
exit("unable to prepare a new user-interface: %v", err)
}
if err := ccui.Run(); err != nil {
exit("unable to run ui: %v", err)
}
}
if err := app.QueueLoad(filenames[indexToPlayFrom:], contentType, transcode); err != nil {
exit("unable to play playlist on cast application: %v", err)
}
},
}
func init() {
rootCmd.AddCommand(playlistCmd)
playlistCmd.Flags().Bool("continue", true, "continue playing from the last known media")
playlistCmd.Flags().Bool("select", false, "choose which media to start the playlist from")
playlistCmd.Flags().Bool("transcode", true, "transcode the media to mp4 if media type is unrecognised")
playlistCmd.Flags().Bool("force-play", false, "attempt to play a media type even if it is unrecognised")
playlistCmd.Flags().StringP("content-type", "c", "", "content-type to serve the media file as")
}