m3u檔案轉pls檔案(go語言實現)

來源:互聯網
上載者:User

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



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.