-
Notifications
You must be signed in to change notification settings - Fork 0
/
id3tags.go
69 lines (64 loc) · 1.33 KB
/
id3tags.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
package id3tags
import (
"os"
)
//Mp3 ...Contains metadata of the file like title, artist, album, year info along with file name and its path
type Mp3 struct {
FilePath string
// Path string
Title string
Artist string
Album string
Year string
}
func getLastBytes(filename string) ([]byte, error) {
file, err := os.Open(filename)
if err != nil {
panic(err)
}
_, err = file.Seek(-int64(128), os.SEEK_END)
if err != nil {
panic(err)
}
b := make([]byte, 128)
_, err = file.Read(b)
if err!=nil{
panic(err)
}
return b, nil
}
func setLastBytes(filename string, b []byte)(error){
file, err:=os.OpenFile(filename,os.O_RDWR,0655)
if err!=nil{
panic(err)
}
_,err = file.Seek(-int64(128),os.SEEK_END)
if err!=nil{
panic(err)
}
_,err=file.Write(b)
if err!=nil{
panic(err)
}
return nil
}
//GetID3Tags ...Extracts mp3 metadata from the file
func (m *Mp3) GetID3Tags() {
b, _ := getLastBytes(m.FilePath)
if string(b[:3]) == "TAG" {
m.Title = string(b[3:33])
m.Artist = string(b[33:63])
m.Album = string(b[63:93])
m.Year = string(b[93:97])
}
}
//SetID3Tags ...Writes tag metadata to file
func (m *Mp3) SetID3Tags() {
b := make([]byte,128)
copy(b[:],"TAG")
copy(b[3:33],m.Title)
copy(b[33:63],m.Artist)
copy(b[63:93],m.Album)
copy(b[93:97],m.Year)
setLastBytes(m.FilePath,b)
}