Golang Rename unicode file name -
how rename file in unicode format, gaps? when try rename standard library function, file not located.
old: /usr/home/spouk/go/src/simple/agraba - know smiling (original mix).mp4 new: /usr/home/spouk/go/src/simple/agraba_-_i_know_you_are_smiling_now__original_mix_.mp4 [rename] rename agraba - know smiling (original mix).mp4 /usr/home/spouk/go/src/simple/agraba_-_i_know_you_are_smiling_now__original_mix_.mp4: no such file or directory
someone tell me use or acquainted direction of solution problem. tnx.
package main import ( "flag" d "github.com/fiam/gounidecode/unidecode" "fmt" "path/filepath" "os" "strings" "strconv" "os/exec" ) type ( file struct { originpath string convertpath string } filesstack struct { files []file inpath string outpath string } ) func newfilesstack() *filesstack { n := new(filesstack) n.files = []file{} return n } func (f *filesstack) runetoascii(r rune) string { if r < 128 { return string(r) } else { return "\\u" + strconv.formatint(int64(r), 16) } } func (f *filesstack) translitercyr(str string) string { var result []string _, sym := range d.unidecode(str) { if (f.runetoascii(sym) == " ") || (f.runetoascii(sym) == "'") || (f.runetoascii(sym) == "`") || (f.runetoascii(sym) == ")") || (f.runetoascii(sym) == "(") { result = append(result, "_") } else { result = append(result, f.runetoascii(sym)) } } return strings.join(result, "") } func (f *filesstack) walkfunction(path string, info os.fileinfo, err error) error { if !info.isdir() { res, err := filepath.abs(filepath.dir(info.name())) if err != nil { fmt.printf(err.error()) } ext := filepath.ext(info.name()) if ext == ".mp4" { file := new(file) //make new file name oldfile := filepath.join(res, info.name()) newname := filepath.join(res, f.translitercyr(info.name())) file.originpath = filepath.join(res, newname) fmt.printf("old: %s\nnew: %s\n", oldfile, newname) //rename file if err_rename := os.rename(info.name(), newname); err_rename != nil { fmt.printf("[rename] %s\n", err_rename.error()) } tmpname := []string{} name := strings.trimright(info.name(), filepath.ext(info.name())) _, sym := range name { if f.runetoascii(sym) != " " { tmpname = append(tmpname, string(sym)) } } word := strings.join(tmpname, "") oo, _ := filepath.abs(f.outpath) rr := strings.join([]string{f.translitercyr(word), ".mp3"}, "") //fmt.printf("res: %v %v\n", rr, word) file.convertpath = filepath.join(oo, rr) //fmt.printf("infile: %v\n", file.originpath) //fmt.printf("outfile: %v\n", file.convertpath) f.files = append(f.files, *file) } } return nil } func (f *filesstack) parsefilesmp4() { //парсинг файлов if err := filepath.walk(f.inpath, f.walkfunction); err != nil { fmt.printf(err.error()) } //конвертация _, file := range f.files { fmt.printf("start convertation %s \n", file.originpath) command := fmt.sprintf("ffmpeg -i %s -f mp3 %s", file.originpath, file.convertpath) //fmt.printf("command %v\n", command) cmd, err_convert := exec.command(command).output() if err_convert != nil { fmt.printf(err_convert.error()) } fmt.printf("result convert: %v\n", cmd) } } // note, variables pointers var ( inpath = flag.string("inpath", ".", "директория для поиска всех файлов с расширением .mp4") outpath = flag.string("outpath", ".", "директория для сохранения .mp3") ) func main() { //make files structy f := newfilesstack() //parse arguments flag.parse() fmt.printf("inpath: %s\noutpath: %s\n", *inpath, *outpath) //set arguments f.inpath = *inpath f.outpath = *outpath f.parsefilesmp4() }
Comments
Post a Comment