func doGenerateSpeechFiles(sentences []string, option option) ([]string, error) { sLen := len(sentences) files := make([]string, 0) filePrefix := utils.RandomString(16) reply := make(chan int, sLen) timeout := false hasError := false for i, sentence := range sentences { option.LimitCh <- 1 fileName := fmt.Sprintf("%s%s-%d.mp3", option.SpeechFileDir, filePrefix, i) go getSpeech(sentence, option.Speaker, fileName, reply, option.LimitCh) files = append(files, fileName) } L: for i := 0; i < sLen; i++ { select { case <-time.After(time.Duration(GenerateSpeechTimeout)): timeout = true break L case r := <-reply: if r == -1 { hasError = true break L } } } if timeout { return []string{}, errors.New("Generate speech timeout!") } else if hasError { return []string{}, errors.New("Generate speech error!") } return files, nil }
func mp3ToFlac(mp3File string) (string, error) { flacPath, _ := filepath.Abs(utils.RandomString(7) + ".flac") _, convertErr := exec.Command("ffmpeg", "-i", mp3File, flacPath).Output() if convertErr != nil { return "", convertErr } return flacPath, nil }
func downloadFile(fileUrl string) (string, error) { ext := filepath.Ext(fileUrl) fileName := utils.RandomString(7) + ext f, createFileErr := os.Create(fileName) defer f.Close() if createFileErr != nil { return "", createFileErr } resp, downloadFileErr := http.Get(fileUrl) defer resp.Body.Close() if downloadFileErr != nil { return "", downloadFileErr } _, writeFileErr := io.Copy(f, resp.Body) if writeFileErr != nil { return "", writeFileErr } p, _ := filepath.Abs(f.Name()) return p, nil }
func (self *PiAssistant) getCommandFile(fileUrl string) (string, error) { ext := filepath.Ext(fileUrl) fileName := utils.RandomString(7) + ext return utils.DownloadHttpFile(fileUrl, fileName) }