video encoding implemented

This commit is contained in:
milarin 2023-01-16 00:00:40 +01:00
parent dc524dbd42
commit fecbf43afd
4 changed files with 63 additions and 11 deletions

43
encode_video.go Normal file
View File

@ -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
}

4
go.mod
View File

@ -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

4
go.sum
View File

@ -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=

View File

@ -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
}