Created
December 14, 2018 08:29
-
-
Save whtiehack/f4ae181cdc4775c2f4f458b6b1318782 to your computer and use it in GitHub Desktop.
转换网易云音乐缓存文件uc,转换成mp3
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
package main | |
import ( | |
"bufio" | |
"fmt" | |
"io" | |
"io/ioutil" | |
"os" | |
"strings" | |
"time" | |
) | |
// 转换网易云 缓存到mp3 | |
func main() { | |
cachePath := "C:\\\\Users\\Administrator\\AppData\\Local\\Netease\\CloudMusic\\Cache\\Cache" | |
targetPath := "F:\\\\CloudMusic" | |
files, _ := ioutil.ReadDir(cachePath) | |
// 启动多少个 goroutine | |
ch := make(chan string, 20) | |
startTime := time.Now() | |
count := 0 | |
for _, file := range files { | |
if !file.IsDir() && strings.HasSuffix(file.Name(), ".uc") { | |
ch <- file.Name() | |
go func(fileName string) { | |
fmt.Println("parse :", fileName) | |
err := decodeFile(cachePath, fileName, targetPath, fileName+".mp3") | |
//err := decodeFileStream(cachePath, fileName, targetPath, fileName+".mp3") | |
count++ | |
fmt.Println("parse end:", <-ch, " err:", err) | |
}(file.Name()) | |
} | |
} | |
fmt.Println("all end", time.Since(startTime), "count:", count) | |
} | |
func decodeFile(dirName, fileName, newDirName, newName string) error { | |
bytes, err := ioutil.ReadFile(dirName + "\\" + fileName) | |
if err != nil { | |
return err | |
} | |
for i := 0; i < len(bytes); i++ { | |
bytes[i] ^= 0xa3 | |
} | |
return ioutil.WriteFile(newDirName+"\\"+newName, bytes, 0777) | |
} | |
// too slower | |
func decodeFileStream(dirName, fileName, newDirName, newName string) error { | |
fs, err := os.OpenFile(dirName+"\\"+fileName, os.O_RDONLY, 0777) | |
if err != nil { | |
return err | |
} | |
defer fs.Close() | |
fsw, err := os.OpenFile(newDirName+"\\"+newName, os.O_RDWR|os.O_CREATE, 0777) | |
if err != nil { | |
return err | |
} | |
nr := bufio.NewReader(fs) | |
nw := bufio.NewWriter(fsw) | |
defer fsw.Close() | |
bytes := [30 * 1024]byte{} | |
for { | |
n, err := nr.Read(bytes[:]) | |
if err != nil { | |
if err == io.EOF { | |
break | |
} | |
return err | |
} | |
if n == 0 { | |
break | |
} | |
for i := 0; i < n; i++ { | |
bytes[i] ^= 0xa3 | |
} | |
_, err = nw.Write(bytes[:n]) | |
if err != nil { | |
return err | |
} | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment