forked from remko/go-mkvparse
/
mkvdir2mrss.go
345 lines (309 loc) · 9.36 KB
/
mkvdir2mrss.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package main
////////////////////////////////////////////////////////////////////////////////
// This example parses all MKV files in a dir, and generates a (Media) RSS feed
// for them. Cover art is extracted, and put in the same dir as the output feed.
//
// Usage:
// ./mkvdir2mrss --baseURL http://localhost --out=feeds/feed.xml Movies/
////////////////////////////////////////////////////////////////////////////////
import (
"crypto/sha1"
"encoding/xml"
"flag"
"fmt"
"github.com/remko/go-mkvparse"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"time"
)
////////////////////////////////////////////////////////////////////////////////
// Parsing
////////////////////////////////////////////////////////////////////////////////
type MediaFile struct {
title string
artist string
duration time.Duration
cover []byte
}
type MediaParser struct {
duration float64
timecodeScale int64
currentTagGlobal bool
currentTagName *string
currentTagValue *string
currentAttachmentData []byte
currentAttachmentFileName string
mediaFile *MediaFile
}
func (p *MediaParser) HandleMasterBegin(id mkvparse.ElementID, info mkvparse.ElementInfo) (bool, error) {
if id == mkvparse.TagElement {
p.currentTagGlobal = true
} else if id == mkvparse.SimpleTagElement {
p.currentTagName = nil
p.currentTagValue = nil
}
return true, nil
}
func (p *MediaParser) HandleMasterEnd(id mkvparse.ElementID, info mkvparse.ElementInfo) error {
if id == mkvparse.SimpleTagElement && p.currentTagGlobal && p.currentTagName != nil && p.currentTagValue != nil {
if strings.ToLower(*p.currentTagName) == "artist" {
p.mediaFile.artist = *p.currentTagValue
}
} else if id == mkvparse.AttachedFileElement {
if p.currentAttachmentFileName == "cover.jpg" {
p.mediaFile.cover = p.currentAttachmentData
}
}
return nil
}
func (p *MediaParser) HandleString(id mkvparse.ElementID, value string, info mkvparse.ElementInfo) error {
if id == mkvparse.TagNameElement {
p.currentTagName = &value
} else if id == mkvparse.TagStringElement {
p.currentTagValue = &value
} else if id == mkvparse.TitleElement {
p.mediaFile.title = value
} else if id == mkvparse.FileNameElement {
p.currentAttachmentFileName = value
}
return nil
}
func (p *MediaParser) HandleInteger(id mkvparse.ElementID, value int64, info mkvparse.ElementInfo) error {
if (id == mkvparse.TagTrackUIDElement || id == mkvparse.TagEditionUIDElement || id == mkvparse.TagChapterUIDElement || id == mkvparse.TagAttachmentUIDElement) && value != 0 {
p.currentTagGlobal = false
} else if id == mkvparse.TimecodeScaleElement {
p.timecodeScale = value
}
return nil
}
func (p *MediaParser) HandleFloat(id mkvparse.ElementID, value float64, info mkvparse.ElementInfo) error {
if id == mkvparse.DurationElement {
p.duration = value
}
return nil
}
func (p *MediaParser) HandleDate(id mkvparse.ElementID, value time.Time, info mkvparse.ElementInfo) error {
return nil
}
func (p *MediaParser) HandleBinary(id mkvparse.ElementID, value []byte, info mkvparse.ElementInfo) error {
if id == mkvparse.FileDataElement {
p.currentAttachmentData = value
}
return nil
}
var supportedMediaFileRE = regexp.MustCompile(`(?i)\.mk[av]$`)
var unsupportedMediaFileRE = regexp.MustCompile(`(?i)\.(mp4|m4v|avi|mpg)$`)
func parseFile(path string) (*MediaFile, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
handler := MediaParser{
duration: -1.0,
timecodeScale: 1000000,
mediaFile: &MediaFile{
duration: -1,
},
}
err = mkvparse.ParseSections(file, &handler, mkvparse.InfoElement, mkvparse.TagsElement, mkvparse.TracksElement, mkvparse.AttachmentsElement)
if err != nil {
return nil, err
}
if handler.duration >= 0 {
handler.mediaFile.duration = time.Duration(int64(handler.duration * float64(handler.timecodeScale)))
} else {
handler.mediaFile.duration = -1
}
return handler.mediaFile, nil
}
////////////////////////////////////////////////////////////////////////////////
// RSS Generation
////////////////////////////////////////////////////////////////////////////////
type RSSMediaContent struct {
XMLName xml.Name `xml:"media:content"`
URL string `xml:"url,attr"`
FileSize int64 `xml:"fileSize,attr"`
Duration int `xml:"duration,attr"`
Type string `xml:"type,attr"`
}
type RSSEnclosure struct {
XMLName xml.Name `xml:"enclosure"`
URL string `xml:"url,attr"`
Length int64 `xml:"length,attr"`
Type string `xml:"type,attr"`
}
type RSSMediaCredit struct {
XMLName xml.Name `xml:"media:credit"`
Role string `xml:"role,attr"`
Value string `xml:",chardata"`
}
type RSSMediaThumbnail struct {
XMLName xml.Name `xml:"media:thumbnail"`
URL string `xml:"url,attr"`
}
type RSSItem struct {
XMLName xml.Name `xml:"item"`
PubDate string `xml:"pubDate"`
Title string `xml:"title"`
Author string `xml:"author,omitempty"`
Enclosure *RSSEnclosure
MediaContent *RSSMediaContent
MediaCredit *RSSMediaCredit
MediaThumbnail *RSSMediaThumbnail
ITunesDuration string `xml:"itunes:duration"`
}
type RSSChannel struct {
XMLName xml.Name `xml:"channel"`
Title string `xml:"title"`
Items []*RSSItem `xml:"items"`
}
type RSSFeed struct {
XMLName xml.Name `xml:"rss"`
Version string `xml:"version,attr"`
MediaNS string `xml:"xmlns:media,attr"`
ITunesNS string `xml:"xmlns:itunes,attr"`
Channel *RSSChannel `xml:"channel"`
}
func formatDuration(d time.Duration) string {
d = d.Round(time.Second)
h := d / time.Hour
d -= h * time.Hour
m := d / time.Minute
d -= m * time.Minute
s := d / time.Second
return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
}
func run() error {
var noDirs bool
baseURL := flag.String("baseURL", "", "Base URL")
outFile := flag.String("out", "", "Output RSS Feed file")
flag.BoolVar(&noDirs, "noDirs", false, "Don't include dirnames in titles")
flag.Parse()
dirs := flag.Args()
if baseURL == nil || outFile == nil || len(dirs) < 1 {
return fmt.Errorf("Missing parameters")
}
baseDir, _ := os.Getwd()
outDir, outFilename := filepath.Split(*outFile)
outDir, err := filepath.Abs(outDir)
if err != nil {
return err
}
extension := filepath.Ext(outFilename)
title := outFilename[0 : len(outFilename)-len(extension)]
feed := RSSFeed{
Version: "2.0",
MediaNS: "http://search.yahoo.com/mrss/",
ITunesNS: "http://www.itunes.com/dtds/podcast-1.0.dtd",
Channel: &RSSChannel{
Title: title,
},
}
for _, dir := range dirs {
absDir, err := filepath.Abs(dir)
if err != nil {
return err
}
err = filepath.Walk(absDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("Error walking %s: %v", path, err)
}
if !info.Mode().IsRegular() {
return nil
}
publicPath, err := filepath.Rel(baseDir, path)
if err != nil {
return err
}
filename, err := filepath.Rel(absDir, path)
if err != nil {
return err
}
extension := filepath.Ext(filename)
name := filename[0 : len(filename)-len(extension)]
if noDirs {
name = filepath.Base(name)
}
mediaURL := fmt.Sprintf("%s/%s", *baseURL, strings.Replace(url.PathEscape(publicPath), "%2F", "/", -1))
if supportedMediaFileRE.MatchString(path) {
file, err := parseFile(path)
if err != nil {
return fmt.Errorf("Error loading %s: %v", path, err)
} else {
item := &RSSItem{
Title: name,
PubDate: info.ModTime().Format(time.RFC822),
ITunesDuration: formatDuration(file.duration),
Author: file.artist,
MediaContent: &RSSMediaContent{
FileSize: info.Size(),
Duration: int(file.duration / time.Second),
URL: mediaURL,
Type: "video/x-matroska",
},
}
item.Enclosure = &RSSEnclosure{
URL: item.MediaContent.URL,
Length: item.MediaContent.FileSize,
Type: item.MediaContent.Type,
}
if len(file.artist) > 0 {
item.MediaCredit = &RSSMediaCredit{
Role: "author",
Value: file.artist,
}
}
if len(file.cover) > 0 {
thumbFile := filepath.Join(outDir, fmt.Sprintf("%x.jpg", sha1.Sum(file.cover)))
if _, err := os.Stat(thumbFile); os.IsNotExist(err) {
err := ioutil.WriteFile(thumbFile, file.cover, 0644)
if err != nil {
return err
}
}
publicThumbFile, err := filepath.Rel(baseDir, thumbFile)
if err != nil {
return err
}
item.MediaThumbnail = &RSSMediaThumbnail{
URL: fmt.Sprintf("%s/%s", *baseURL, strings.Replace(url.PathEscape(publicThumbFile), "%2F", "/", -1)),
}
}
feed.Channel.Items = append(feed.Channel.Items, item)
}
} else if unsupportedMediaFileRE.MatchString(path) {
// Fallback to basic information for unsupported media files
item := &RSSItem{
Title: name,
PubDate: info.ModTime().Format(time.RFC822),
Enclosure: &RSSEnclosure{
URL: mediaURL,
Length: info.Size(),
Type: "video/mp4",
},
}
feed.Channel.Items = append(feed.Channel.Items, item)
}
return nil
})
if err != nil {
return err
}
}
output, err := xml.MarshalIndent(feed, " ", " ")
if err != nil {
return err
}
return ioutil.WriteFile(*outFile, output, 0644)
}
func main() {
if err := run(); err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
}