-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia.go
173 lines (146 loc) · 2.92 KB
/
media.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
package avtools
import (
"fmt"
"log"
"strings"
"time"
"github.com/ohzqq/dur"
"github.com/ohzqq/fidi"
)
type Media struct {
fidi.File
Filename string
tags map[string]string
Tagz map[string]string
chapters []*Chapter
Chaps []*Chapter
streams []map[string]string
Streamz []Stream
Input fidi.File
Output fidi.File
Ini fidi.File
Cue fidi.File
Cover fidi.File
HasCover bool
}
type Stream struct {
CodecType string
CodecName string
Index string
IsCover bool
}
type Chapter struct {
StartTime Time
EndTime Time
StartStamp dur.Timestamp
EndStamp dur.Timestamp
ChapTitle string
Tags map[string]string
}
type ChapterMeta interface {
Start() time.Duration
End() time.Duration
Title() string
}
type Metaz interface {
Chapters() []ChapterMeta
Tags() map[string]string
Streams() []map[string]string
Source() fidi.File
}
type Meta interface {
Chapters() []*Chapter
Tags() map[string]string
Streams() []map[string]string
}
func NewMedia() *Media {
media := Media{
tags: make(map[string]string),
}
return &media
}
func NewChapter(chap ChapterMeta) *Chapter {
ss, err := dur.New(chap.Start())
if err != nil {
log.Fatal(err)
}
to, err := dur.New(chap.End())
if err != nil {
log.Fatal(err)
}
return &Chapter{
ChapTitle: chap.Title(),
StartStamp: ss,
EndStamp: to,
Tags: make(map[string]string),
}
}
func NewChapters(chaps []ChapterMeta) []*Chapter {
var ch []*Chapter
for _, c := range chaps {
ch = append(ch, NewChapter(c))
}
return ch
}
func (m *Media) SetMeta(meta Meta) *Media {
for key, val := range meta.Tags() {
m.tags[key] = val
}
m.chapters = meta.Chapters()
m.streams = meta.Streams()
return m
}
func (m Media) Chapters() []*Chapter {
return m.chapters
}
func (m *Media) SetChapters(chaps []*Chapter) {
m.chapters = chaps
}
func (m Media) Tags() map[string]string {
return m.tags
}
func (m Media) Streams() []map[string]string {
return m.streams
}
func (m Media) GetTag(key string) string {
if val, ok := m.Tagz[key]; ok {
return val
}
return ""
}
func (ch Chapter) Timebase() string {
return "1/1000"
}
func (ch *Chapter) SS(ss string) *Chapter {
dur := ParseStamp(ss)
ch.StartTime = Timestamp(dur)
return ch
}
func (ch *Chapter) To(to string) *Chapter {
dur := ParseStamp(to)
ch.EndTime = Timestamp(dur)
return ch
}
func (ch Chapter) Start() dur.Timestamp {
return ch.StartStamp
}
func (ch Chapter) End() dur.Timestamp {
return ch.StartStamp
}
func (ch Chapter) Title() string {
return ch.ChapTitle
}
func IsPlainText(mtype string) error {
if strings.Contains(mtype, "text/plain") {
return nil
}
return fmt.Errorf("needs to be plain text file")
}
//func (ch Chapter) Dur() (Time, error) {
// if ch.end.Duration == 0 {
// return ch.end, fmt.Errorf("end time is needed to calculate duration")
// }
// t := ch.end.Duration - ch.start.Duration
// stamp := NewTime(t, float64(ch.base))
// return stamp, nil
//}