From cc549c817f52a99519fb90e426ade4f485d70f38 Mon Sep 17 00:00:00 2001 From: bytedream Date: Thu, 19 Aug 2021 23:42:41 +0200 Subject: [PATCH] Fixed #3 --- Makefile | 6 ++-- cmd/crunchyroll-go/cmd/download.go | 50 ++++++++++++++++-------------- cmd/crunchyroll-go/cmd/utils.go | 19 +++++++++--- format.go | 10 +++--- 4 files changed, 49 insertions(+), 36 deletions(-) diff --git a/Makefile b/Makefile index 10e7cc2..2610180 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,16 @@ -VERSION=1.0 +VERSION=1.0.1 BINARY_NAME=crunchy VERSION_BINARY_NAME=$(BINARY_NAME)-v$(VERSION) build: - cd cmd/crunchyroll-go && CGO_ENABLED=0 go build -o $(BINARY_NAME) + cd cmd/crunchyroll-go && go build -o $(BINARY_NAME) mv cmd/crunchyroll-go/$(BINARY_NAME) . test: go test -v . release: - cd cmd/crunchyroll-go && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $(VERSION_BINARY_NAME)_linux + cd cmd/crunchyroll-go && GOOS=linux GOARCH=amd64 go build -o $(VERSION_BINARY_NAME)_linux cd cmd/crunchyroll-go && GOOS=windows GOARCH=amd64 go build -o $(VERSION_BINARY_NAME)_windows.exe cd cmd/crunchyroll-go && GOOS=darwin GOARCH=amd64 go build -o $(VERSION_BINARY_NAME)_darwin diff --git a/cmd/crunchyroll-go/cmd/download.go b/cmd/crunchyroll-go/cmd/download.go index 3b82f33..bcdb4d8 100644 --- a/cmd/crunchyroll-go/cmd/download.go +++ b/cmd/crunchyroll-go/cmd/download.go @@ -12,6 +12,7 @@ import ( "os/exec" "os/signal" "path" + "path/filepath" "sort" "strconv" "strings" @@ -398,12 +399,12 @@ func findFormat(formats []*crunchyroll.Format) (format *crunchyroll.Format) { return } -func downloadFormat(format *crunchyroll.Format, dir, fname string, info information) (_ bool) { - filename := freeFileName(path.Join(dir, fname)) +func downloadFormat(format *crunchyroll.Format, dir, fname string, info information) bool { + filename := freeFileName(filepath.Join(dir, fname)) ext := path.Ext(filename) out.Debugf("Download filename: %s\n", filename) - if filename != path.Join(dir, fname) { - out.Errf("The file %s already exist, renaming the download file to %s\n", path.Join(dir, fname), filename) + if filename != filepath.Join(dir, fname) { + out.Errf("The file %s already exist, renaming the download file to %s\n", filepath.Join(dir, fname), filename) } if ext != ".ts" { if !hasFFmpeg() { @@ -419,7 +420,7 @@ func downloadFormat(format *crunchyroll.Format, dir, fname string, info informat out.Errf("Failed to get %s subtitles\n", info.Subtitle) return false } - subtitleFilename = freeFileName(path.Join(dir, fmt.Sprintf("%s.%s", strings.TrimRight(path.Base(filename), ext), subtitle.Format))) + subtitleFilename = freeFileName(filepath.Join(dir, fmt.Sprintf("%s.%s", strings.TrimRight(path.Base(filename), ext), subtitle.Format))) out.Debugf("Subtitles will be saved as `%s`\n", subtitleFilename) } @@ -436,7 +437,7 @@ func downloadFormat(format *crunchyroll.Format, dir, fname string, info informat defer file.Close() if err != nil { out.Errf("Could not create file `%s` to download episode `%s` (%s): %s, skipping\n", filename, info.Title, info.OriginalURL, err) - return + return false } cleanup[0] = filename @@ -454,13 +455,16 @@ func downloadFormat(format *crunchyroll.Format, dir, fname string, info informat }() err = format.Download(file, downloadProgress) + // newline to avoid weird output + fmt.Println() + // make the goroutine stop sigs <- sigusr1 } else { tempDir, err := os.MkdirTemp("", "crunchy_") if err != nil { out.Errln("Failed to create temp download dir. Skipping") - return + return false } sigs := make(chan os.Signal, 1) signal.Notify(sigs, os.Interrupt, syscall.SIGTERM, sigusr1) @@ -473,29 +477,29 @@ func downloadFormat(format *crunchyroll.Format, dir, fname string, info informat } }() - var filenames []string + var segmentCount int err = format.DownloadSegments(tempDir, 4, func(segment *m3u8.MediaSegment, current, total int, file *os.File, err error) error { - filenames = append(filenames, file.Name()) + segmentCount++ return downloadProgress(segment, current, total, file, err) }) + // newline to avoid weird output + fmt.Println() - sort.Slice(filenames, func(i, j int) bool { - iNum, err := strconv.Atoi(strings.Split(path.Base(filenames[i]), ".")[0]) - if err != nil { - return false - } - jNum, err := strconv.Atoi(strings.Split(path.Base(filenames[j]), ".")[0]) - if err != nil { - return false - } - return iNum < jNum - }) + f, _ := os.CreateTemp("", "*.txt") + for i := 0; i < segmentCount; i++ { + fmt.Fprintf(f, "file '%s.ts'\n", filepath.Join(tempDir, strconv.Itoa(i))) + } + defer os.Remove(f.Name()) + f.Close() cmd := exec.Command("ffmpeg", - "-i", fmt.Sprintf("concat:%s", strings.Join(filenames, "|")), - "-codec", "copy", + "-f", "concat", + "-safe", "0", + "-i", f.Name(), + "-c", "copy", filename) err = cmd.Run() + sigs <- sigusr1 } if err != nil { @@ -513,7 +517,7 @@ func downloadFormat(format *crunchyroll.Format, dir, fname string, info informat } else { subtitle, ok := utils.SubtitleByLocale(format, info.Subtitle) if !ok { - out.Errln("Failed to get %s subtitles\n", info.Subtitle) + out.Errf("Failed to get %s subtitles\n", info.Subtitle) return false } if err := subtitle.Download(file); err != nil { diff --git a/cmd/crunchyroll-go/cmd/utils.go b/cmd/crunchyroll-go/cmd/utils.go index 4774310..2afe2f8 100644 --- a/cmd/crunchyroll-go/cmd/utils.go +++ b/cmd/crunchyroll-go/cmd/utils.go @@ -11,7 +11,7 @@ import ( "net/url" "os" "os/exec" - "path" + "path/filepath" "runtime" "strconv" "strings" @@ -19,7 +19,7 @@ import ( "time" ) -var sessionIDPath = path.Join(os.TempDir(), ".crunchy") +var sessionIDPath = filepath.Join(os.TempDir(), ".crunchy") type progress struct { status bool @@ -89,11 +89,20 @@ func (l *logger) StartProgress(message string) { l.progressWG.Lock() select { case p := <-l.progress: - fmt.Print("\r") + // clearing the last line + fmt.Printf("\r%s\r", strings.Repeat(" ", len(l.InfoLog.Prefix())+len(message)+2)) if p.status { - l.InfoLog.Printf("✅ %s", p.message) + successTag := "✔" + if runtime.GOOS == "windows" { + successTag = "~" + } + l.InfoLog.Printf("%s %s", successTag, p.message) } else { - l.ErrLog.Printf("❌ %s", p.message) + errorTag := "✘" + if runtime.GOOS == "windows" { + errorTag = "!" + } + l.ErrLog.Printf("%s %s", errorTag, p.message) } l.progress = nil l.progressWG.Unlock() diff --git a/format.go b/format.go index 5c462e8..8be0e58 100644 --- a/format.go +++ b/format.go @@ -9,7 +9,7 @@ import ( "io/ioutil" "net/http" "os" - "path" + "path/filepath" "sort" "strconv" "strings" @@ -108,7 +108,7 @@ func (f *Format) DownloadSegments(outputDir string, goroutines int, onSegmentDow break default: var file *os.File - file, err = f.downloadSegment(segment, path.Join(outputDir, fmt.Sprintf("%d.ts", i+j)), block, iv) + file, err = f.downloadSegment(segment, filepath.Join(outputDir, fmt.Sprintf("%d.ts", i+j)), block, iv) if err != nil { quit <- true break @@ -169,13 +169,13 @@ func (f *Format) downloadSegment(segment *m3u8.MediaSegment, filename string, bl // some mpeg stream things. see the link beneath for more information // https://github.com/oopsguy/m3u8/blob/4150e93ec8f4f8718875a02973f5d792648ecb97/dl/dowloader.go#L135 - syncByte := uint8(71) //0x47 + /*syncByte := uint8(71) //0x47 for k := 0; k < len(content); k++ { if content[k] == syncByte { content = content[k:] break } - } + }*/ file, err := os.Create(filename) if err != nil { @@ -212,7 +212,7 @@ func (f *Format) mergeSegments(tempPath string, output *os.File) error { }) for _, file := range dir { - bodyAsBytes, err := ioutil.ReadFile(path.Join(tempPath, file.Name())) + bodyAsBytes, err := ioutil.ReadFile(filepath.Join(tempPath, file.Name())) if err != nil { return err }