Created
September 26, 2012 00:31
-
-
Save wendal/3785288 to your computer and use it in GitHub Desktop.
GOLANG读取MP3文件的ID3信息
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//from http://0x55aa.sinaapp.com/%E7%AE%97%E6%B3%95-%E7%BC%96%E7%A8%8B/676.html?1348618006 | |
package main | |
import ( | |
"fmt" | |
"os" | |
"errors" | |
"strings" | |
) | |
//no tag | |
var ErrFormat = errors.New("No TAG!") | |
type Mp3 struct{ | |
//the file attr | |
Name string | |
Size int64 | |
//the mp3 attr | |
Title string | |
Artist string | |
Album string | |
Year string | |
Comment string | |
Genre uint8 | |
} | |
func (m *Mp3) PrintInfo(){ | |
fmt.Printf("%v:\n",m.Name) | |
fmt.Printf("size:%v\ntitle:%v\nartist:%v\nalbum:%v\nyear:%v\ncomment:%v\n",m.Size,m.Title,m.Artist,m.Album,m.Year,m.Comment) | |
fmt.Printf("genre:%v",m.Genre) | |
} | |
func (m *Mp3) GetInfo(filename string) (err error){ | |
id3 := make([]byte,128) | |
//for read access. | |
f,err := os.Open(filename) | |
if err != nil{ | |
return | |
} | |
defer f.Close() | |
//return FileInfo | |
fileInfo,err := f.Stat() | |
if err != nil{ | |
return | |
} | |
//the file size,int64 | |
m.Size = fileInfo.Size() | |
//the file name,string. eq filename | |
m.Name = fileInfo.Name() | |
//if | |
_,err = f.ReadAt(id3,m.Size-128) | |
if err != nil{ | |
return | |
} | |
tag := string(id3[:3]) | |
if tag != "TAG"{ | |
//err "No ID3~" | |
return ErrFormat | |
} | |
m.Title = strings.Trim(string(id3[3:33]),"\x00") | |
m.Album = strings.Trim(string(id3[33:63]),"\x00") | |
m.Artist = strings.Trim(string(id3[63:93]),"\x00") | |
m.Year = string(id3[93:97]) | |
m.Comment = strings.Trim(string(id3[97:127]),"\x00") | |
m.Genre = uint8(id3[127]) | |
return nil | |
} | |
func main(){ | |
m := new(Mp3) | |
f,err := os.Open(".") | |
if err != nil{ | |
fmt.Print(err) | |
} | |
defer f.Close() | |
names,err := f.Readdirnames(-1) | |
if err != nil{ | |
fmt.Print(err) | |
} | |
for _,name := range names{ | |
a := strings.Split(name,".") | |
fileExt := strings.ToLower(a[len(a)-1]) | |
//fmt.Print(i,fileExt) | |
if fileExt == "mp3"{ | |
err = m.GetInfo(name) | |
if err != nil{ | |
fmt.Print(name,err) | |
} | |
m.PrintInfo() | |
} | |
} | |
//fmt.Printf("%v",names) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment