-
Notifications
You must be signed in to change notification settings - Fork 52
/
example_test.go
298 lines (253 loc) · 7.1 KB
/
example_test.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
package id3v2_test
import (
"fmt"
"io/ioutil"
"log"
"math/big"
"os"
"sync"
"github.com/bogem/id3v2"
)
func Example() {
// Open file and parse tag in it.
tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true})
if err != nil {
log.Fatal("Error while opening mp3 file: ", err)
}
defer tag.Close()
// Read frames.
fmt.Println(tag.Artist())
fmt.Println(tag.Title())
// Set simple text frames.
tag.SetArtist("Artist")
tag.SetTitle("Title")
// Set comment frame.
comment := id3v2.CommentFrame{
Encoding: id3v2.EncodingUTF8,
Language: "eng",
Description: "My opinion",
Text: "Very good song",
}
tag.AddCommentFrame(comment)
// Write tag to file.
if err = tag.Save(); err != nil {
log.Fatal("Error while saving a tag: ", err)
}
}
func Example_concurrent() {
tagPool := sync.Pool{New: func() interface{} { return id3v2.NewEmptyTag() }}
var wg sync.WaitGroup
wg.Add(100)
for i := 0; i < 100; i++ {
go func() {
defer wg.Done()
tag := tagPool.Get().(*id3v2.Tag)
defer tagPool.Put(tag)
file, err := os.Open("file.mp3")
if err != nil {
log.Fatal("Error while opening file:", err)
}
defer file.Close()
if err := tag.Reset(file, id3v2.Options{Parse: true}); err != nil {
log.Fatal("Error while reseting tag to file:", err)
}
fmt.Println(tag.Artist() + " - " + tag.Title())
}()
}
wg.Wait()
}
func ExampleTag_AddAttachedPicture() {
tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true})
if tag == nil || err != nil {
log.Fatal("Error while opening mp3 file: ", err)
}
artwork, err := ioutil.ReadFile("artwork.jpg")
if err != nil {
log.Fatal("Error while reading artwork file", err)
}
pic := id3v2.PictureFrame{
Encoding: id3v2.EncodingUTF8,
MimeType: "image/jpeg",
PictureType: id3v2.PTFrontCover,
Description: "Front cover",
Picture: artwork,
}
tag.AddAttachedPicture(pic)
}
func ExampleTag_AddCommentFrame() {
tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true})
if tag == nil || err != nil {
log.Fatal("Error while opening mp3 file: ", err)
}
comment := id3v2.CommentFrame{
Encoding: id3v2.EncodingUTF8,
Language: "eng",
Description: "My opinion",
Text: "Very good song",
}
tag.AddCommentFrame(comment)
}
func ExampleTag_AddUnsynchronisedLyricsFrame() {
tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true})
if tag == nil || err != nil {
log.Fatal("Error while opening mp3 file: ", err)
}
uslt := id3v2.UnsynchronisedLyricsFrame{
Encoding: id3v2.EncodingUTF8,
Language: "ger",
ContentDescriptor: "Deutsche Nationalhymne",
Lyrics: "Einigkeit und Recht und Freiheit...",
}
tag.AddUnsynchronisedLyricsFrame(uslt)
}
func ExampleTag_GetFrames() {
tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true})
if tag == nil || err != nil {
log.Fatal("Error while opening mp3 file: ", err)
}
pictures := tag.GetFrames(tag.CommonID("Attached picture"))
for _, f := range pictures {
pic, ok := f.(id3v2.PictureFrame)
if !ok {
log.Fatal("Couldn't assert picture frame")
}
// Do something with picture frame.
// For example, print description of picture frame:
fmt.Println(pic.Description)
}
}
func ExampleTag_GetLastFrame() {
tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true})
if tag == nil || err != nil {
log.Fatal("Error while opening mp3 file: ", err)
}
bpmFramer := tag.GetLastFrame(tag.CommonID("BPM"))
if bpmFramer != nil {
bpm, ok := bpmFramer.(id3v2.TextFrame)
if !ok {
log.Fatal("Couldn't assert bpm frame")
}
fmt.Println(bpm.Text)
}
}
func ExampleCommentFrame_get() {
tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true})
if tag == nil || err != nil {
log.Fatal("Error while opening mp3 file: ", err)
}
comments := tag.GetFrames(tag.CommonID("Comments"))
for _, f := range comments {
comment, ok := f.(id3v2.CommentFrame)
if !ok {
log.Fatal("Couldn't assert comment frame")
}
// Do something with comment frame.
// For example, print the text:
fmt.Println(comment.Text)
}
}
func ExampleCommentFrame_add() {
tag := id3v2.NewEmptyTag()
comment := id3v2.CommentFrame{
Encoding: id3v2.EncodingUTF8,
Language: "eng",
Description: "My opinion",
Text: "Very good song",
}
tag.AddCommentFrame(comment)
}
func ExamplePictureFrame_get() {
tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true})
if tag == nil || err != nil {
log.Fatal("Error while opening mp3 file: ", err)
}
pictures := tag.GetFrames(tag.CommonID("Attached picture"))
for _, f := range pictures {
pic, ok := f.(id3v2.PictureFrame)
if !ok {
log.Fatal("Couldn't assert picture frame")
}
// Do something with picture frame.
// For example, print the description:
fmt.Println(pic.Description)
}
}
func ExamplePictureFrame_add() {
tag := id3v2.NewEmptyTag()
artwork, err := ioutil.ReadFile("artwork.jpg")
if err != nil {
log.Fatal("Error while reading artwork file", err)
}
pic := id3v2.PictureFrame{
Encoding: id3v2.EncodingUTF8,
MimeType: "image/jpeg",
PictureType: id3v2.PTFrontCover,
Description: "Front cover",
Picture: artwork,
}
tag.AddAttachedPicture(pic)
}
func ExampleTextFrame_get() {
tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true})
if tag == nil || err != nil {
log.Fatal("Error while opening mp3 file: ", err)
}
tf := tag.GetTextFrame(tag.CommonID("Mood"))
fmt.Println(tf.Text)
}
func ExampleTextFrame_add() {
tag := id3v2.NewEmptyTag()
textFrame := id3v2.TextFrame{
Encoding: id3v2.EncodingUTF8,
Text: "Happy",
}
tag.AddFrame(tag.CommonID("Mood"), textFrame)
}
func ExampleUnsynchronisedLyricsFrame_get() {
tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true})
if tag == nil || err != nil {
log.Fatal("Error while opening mp3 file: ", err)
}
uslfs := tag.GetFrames(tag.CommonID("Unsynchronised lyrics/text transcription"))
for _, f := range uslfs {
uslf, ok := f.(id3v2.UnsynchronisedLyricsFrame)
if !ok {
log.Fatal("Couldn't assert USLT frame")
}
// Do something with USLT frame.
// For example, print the lyrics:
fmt.Println(uslf.Lyrics)
}
}
func ExampleUnsynchronisedLyricsFrame_add() {
tag := id3v2.NewEmptyTag()
uslt := id3v2.UnsynchronisedLyricsFrame{
Encoding: id3v2.EncodingUTF8,
Language: "ger",
ContentDescriptor: "Deutsche Nationalhymne",
Lyrics: "Einigkeit und Recht und Freiheit...",
}
tag.AddUnsynchronisedLyricsFrame(uslt)
}
func ExamplePopularimeterFrame_add() {
tag := id3v2.NewEmptyTag()
popmFrame := id3v2.PopularimeterFrame{
Email: "foo@bar.com",
Rating: 128,
Counter: big.NewInt(10000000000000000),
}
tag.AddFrame(tag.CommonID("Popularimeter"), popmFrame)
}
func ExamplePopularimeterFrame_get() {
tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true})
if tag == nil || err != nil {
log.Fatal("Error while opening mp3 file: ", err)
}
f := tag.GetLastFrame(tag.CommonID("Popularimeter"))
popm, ok := f.(id3v2.PopularimeterFrame)
if !ok {
log.Fatal("Couldn't assert POPM frame")
}
// do something with POPM Frame
fmt.Printf("Email: %s, Rating: %d, Counter: %d", popm.Email, popm.Rating, popm.Counter)
}