M3U file to pls file (go language implementation)

Source: Internet
Author: User
Tags split sprintf

M3U is a file format, similar to the following:

#EXTINF:, CCTV 1
Mms://live.cctv.com/cctv_live1
#EXTINF:, CCTV 2
Mms://live.cctv.com/live12

For example, the first line is #extinf:,cctv 1, then a comma-separated 2nd string is the file name, and the second row is the file path

The corresponding pls file is as follows:

[Playlist]
numberofentries=116
File1=mms://live.cctv.com/cctv_live1
TITLE1=CCTV 1
File2=mms://live.cctv.com/live12
TITLE2=CCTV 2

It will indicate how many files, filenames, and file paths (shown in key=val this form) are in total.

Here is the code that uses the program to convert a m3u file to a pls file:

Package main import ("FMT" "OS" "strings") type Node struct{File string Title string} func Read File (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 cont  ent 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" Newconte NT + = 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]")}}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.