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 mainimport ( "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]") }}