From fecbf43afde5b4e9f5d72223d658d9e469a2f855 Mon Sep 17 00:00:00 2001 From: milarin Date: Mon, 16 Jan 2023 00:00:40 +0100 Subject: [PATCH] video encoding implemented --- encode_video.go | 43 +++++++++++++++++++++++++++++++++++++++++++ go.mod | 4 ++-- go.sum | 4 ++++ handle_file.go | 23 ++++++++++++++--------- 4 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 encode_video.go diff --git a/encode_video.go b/encode_video.go new file mode 100644 index 0000000..67e3f30 --- /dev/null +++ b/encode_video.go @@ -0,0 +1,43 @@ +package main + +import ( + "fmt" + "io" + "os" + "os/exec" + "strings" + "time" +) + +func EncodeVideo(w io.Writer, encArgs, oldFile, newFile string) error { + if encArgs == "" { + fmt.Fprintf(w, "\trename file\n\t from: '%s'\n\t to: '%s'\n", oldFile, newFile) + return os.Rename(oldFile, newFile) + } + + start := time.Now() + + fmt.Fprintf(w, "\tencode file\n\t from: '%s'\n\t to: '%s\n'", oldFile, newFile) + + fullArgs := []string{"-y", "-i", oldFile} + fullArgs = append(fullArgs, strings.Split(encArgs, " ")...) + fullArgs = append(fullArgs, newFile) + + cmd := exec.Command("ffmpeg", fullArgs...) + + if err := cmd.Start(); err != nil { + return err + } + + if err := cmd.Wait(); err != nil { + return err + } + + if err := os.Remove(oldFile); err != nil { + return err + } + + fmt.Fprintf(w, "\t done (took %s)\n", time.Since(start).Truncate(100*time.Millisecond)) + + return nil +} diff --git a/go.mod b/go.mod index db99771..fcdde2a 100644 --- a/go.mod +++ b/go.mod @@ -8,8 +8,8 @@ require ( git.milar.in/milarin/envvars/v2 v2.0.0 git.milar.in/milarin/gmath v0.0.3 git.milar.in/nyaanime/logic v0.0.0-20230115195351-8ed3e407ee72 - git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3 - git.milar.in/nyaanime/parsers v0.0.0-20230115135225-d80026a240a2 + git.milar.in/nyaanime/model v0.0.0-20230115221845-51c282652aef + git.milar.in/nyaanime/parsers v0.0.0-20230115222957-333b1931d05d github.com/fatih/color v1.13.0 github.com/fsnotify/fsnotify v1.6.0 github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible diff --git a/go.sum b/go.sum index 372c596..5b39e3b 100644 --- a/go.sum +++ b/go.sum @@ -16,8 +16,12 @@ git.milar.in/nyaanime/logic v0.0.0-20230115195351-8ed3e407ee72 h1:/kSY7P/NNaUmkx git.milar.in/nyaanime/logic v0.0.0-20230115195351-8ed3e407ee72/go.mod h1:cLuIBDTtpXdzprOIsz73r6ZCE5s0k+o6HA7fH9OuW/M= git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3 h1:mXcEA47FQQzeSDXE3UvhNfIt4fBfpDSq1/f0r+jbHpY= git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3/go.mod h1:kPWLDvFrhc1Uf77gxsBOxNeJ5JTVF2HhVs1IdVcw0tg= +git.milar.in/nyaanime/model v0.0.0-20230115221845-51c282652aef h1:Bp1cMHgGnz5SAVWSL1hk1w7F75LGAkfwHqoTZnNQNbY= +git.milar.in/nyaanime/model v0.0.0-20230115221845-51c282652aef/go.mod h1:kPWLDvFrhc1Uf77gxsBOxNeJ5JTVF2HhVs1IdVcw0tg= git.milar.in/nyaanime/parsers v0.0.0-20230115135225-d80026a240a2 h1:Q95JBR9mXENAjRhvzPAsFjPfxY0ljUiLVlhfAO4q6UY= git.milar.in/nyaanime/parsers v0.0.0-20230115135225-d80026a240a2/go.mod h1:GG4vtUIfxopZc/+Y8OAa//vWJw/m6aeoGN7fw6SLiEM= +git.milar.in/nyaanime/parsers v0.0.0-20230115222957-333b1931d05d h1:pAcVBBw1vc+184F3jnGB5oDJFKCEOTrC6zA8CahFBDg= +git.milar.in/nyaanime/parsers v0.0.0-20230115222957-333b1931d05d/go.mod h1:fqiJqSWxh1YVJkKhvQql/21HmQzv4elhC3SpH/2TybI= github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= diff --git a/handle_file.go b/handle_file.go index ddeceb3..09ffc10 100644 --- a/handle_file.go +++ b/handle_file.go @@ -90,7 +90,8 @@ func OrganizeAnimeEpisode(w io.Writer, parsedFile *model.ParsedFile) error { start := time.Now() oldFile := parsedFile.File - newFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), filepath.Ext(parsedFile.File)) + newFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), "part") + encodedFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), filepath.Ext(parsedFile.File)) lockFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), "lock") fmt.Fprintf(w, "\tmove file\n\t from: '%s'\n\t to: '%s'\n", oldFile, newFile) @@ -128,16 +129,20 @@ func OrganizeAnimeEpisode(w io.Writer, parsedFile *model.ParsedFile) error { return err } - if err = os.Remove(oldFile); err != nil { - return err - } - - if err = os.Remove(lockFile); err != nil && !errors.Is(err, os.ErrNotExist) { - return err - } - fmt.Fprintf(w, "\t done (copied %s in %s)\n", FormatBytes(written), time.Since(start).Truncate(100*time.Millisecond)) + if err := EncodeVideo(w, parsedFile.Parser.FileEncoding, newFile, encodedFile); err != nil { + return err + } + + if err := os.Remove(oldFile); err != nil { + return err + } + + if err := os.Remove(lockFile); err != nil && !errors.Is(err, os.ErrNotExist) { + return err + } + return nil }