m3u是一种文件格式,类似于下:
#EXTINF:,CCTV 1
mms://live.cctv.com/cctv_live1
#EXTINF:,CCTV 2
mms://live.cctv.com/live12
比如第一行是#EXTINF:,CCTV 1,那么用逗号隔开的第2个字符串就是文件名称;而第二行则是文件路径
对应的pls文件如下:
[playlist]
NumberOfEntries=116
File1=mms://live.cctv.com/cctv_live1
Title1=CCTV 1
File2=mms://live.cctv.com/live12
Title2=CCTV 2
它会指明总共有多少个个文件,文件名和文件路径(以key=val这种形式展示)
下面就是利用程序将m3u文件转为pls文件的代码:
package main import ( "fmt" "os" "strings" ) type Node struct{ File string Title string } func readFile(filepath string,MAXSIZE int) string{ file,err := os.Open(filepath) if err != nil{ fmt.Println(err) return "" } var buffer = make([] byte,MAXSIZE) cur,err := file.Read(buffer) if err != nil{ fmt.Println(err) return "" } var content string = string(buffer[0:cur]) file.Close() return content } func writeFile(filepath string,content string){ file,err := os.Create(filepath) if err != nil{ fmt.Println(err) return } _,err = file.WriteString(content) file.Close() if err != nil{ fmt.Println(err) } } func transM3u2Pls(filepath string,MAXSIZE int){ var dstpath string = "./result.pls" var content string = readFile(filepath,MAXSIZE) l := make([]Node,1000) var cur Node var index int = 0 for _,line := range strings.Split(content,"\n"){ if index % 2 == 0{ lines := strings.Split(line,",") cur.Title = lines[1] }else{ cur.File = line l[index/2] = cur } index++ } var newcontent = "[playlist]\n" newcontent += fmt.Sprintf("NumberOfEntries=%d\n",(index/2)) for i:=0;i<index/2;i++{ newcontent += fmt.Sprintf("File%d=%s\n",i+1,l[i].File) newcontent += fmt.Sprintf("Title%d=%s\n",i+1,l[i].Title) } writeFile(dstpath,newcontent) fmt.Println("done!") } func main(){ if len(os.Args) > 1{ filepath := os.Args[1] const MAXSIZE int = 10240 if strings.HasSuffix(filepath,"m3u"){ transM3u2Pls(filepath,MAXSIZE) }else{ fmt.Println("file format not suffort(Not .m3u).") } }else{ fmt.Println("Usage:./test1 [filepath]") } }