From 4d68f10de10234bf34df36159cd8a773a1f82b48 Mon Sep 17 00:00:00 2001 From: milarin Date: Thu, 16 Feb 2023 15:33:08 +0100 Subject: [PATCH] limit file encoding to a single file at a time --- go.mod | 1 + go.sum | 2 ++ main.go | 7 +------ video_encoder.go | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 video_encoder.go diff --git a/go.mod b/go.mod index 6b1074b..16549db 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.20 require ( git.milar.in/milarin/adverr v1.1.0 + git.milar.in/milarin/channel v0.0.16 git.milar.in/milarin/envvars/v2 v2.0.0 git.milar.in/milarin/slices v0.0.6 github.com/gorilla/mux v1.8.0 diff --git a/go.sum b/go.sum index 6c5b70a..04feed7 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ git.milar.in/milarin/adverr v1.1.0 h1:jD9WnOvs40lfMhvqQ7cllOaRJNBMWr1f07/s9jAadp0= git.milar.in/milarin/adverr v1.1.0/go.mod h1:joU9sBb7ySyNv4SpTXB0Z4o1mjXsArBw4N27wjgzj9E= +git.milar.in/milarin/channel v0.0.16 h1:gyxwUFdw2pJUusdNhgnuTb2Tmb5MQAbTYcyxa3wp494= +git.milar.in/milarin/channel v0.0.16/go.mod h1:We83LTI8S7u7II3pD+A2ChCDWJfCkcBUCUqii9HjTtM= git.milar.in/milarin/envvars/v2 v2.0.0 h1:DWRQCWaHqzDD8NGpSgv5tYLuF9A/dVFPAtTvz3oiIqE= git.milar.in/milarin/envvars/v2 v2.0.0/go.mod h1:HkdEi+gG2lJSmVq547bTlQV4qQ0hO333bE8IrE0B9yY= git.milar.in/milarin/slices v0.0.6 h1:AQoSarZ58WHYol9c6woWJSe8wFpPC2RC4cvIlZpfg9s= diff --git a/main.go b/main.go index 9db6cb2..46d0a23 100644 --- a/main.go +++ b/main.go @@ -193,12 +193,7 @@ func GetEncodeFileHandler(w http.ResponseWriter, r *http.Request) { fmt.Printf("encode file to %s: '%s' -> '%s'\n", format, inputFilePath, outputFilePath) - cmd := exec.Command("ffmpeg", "-i", inputFilePath, "-vn", outputFilePath) - if err := cmd.Start(); err != nil { - InternalServerError(w, err) - return - } - if err := cmd.Wait(); err != nil { + if err := EncodeVideo2Mp3(inputFilePath, outputFilePath); err != nil { InternalServerError(w, err) return } diff --git a/video_encoder.go b/video_encoder.go new file mode 100644 index 0000000..65a8307 --- /dev/null +++ b/video_encoder.go @@ -0,0 +1,33 @@ +package main + +import ( + "os/exec" + "sync" + + "git.milar.in/milarin/channel" +) + +var EncodeRunner = channel.NewLimitedRunner(1) + +func EncodeVideo2Mp3(inputFile, outputFile string) error { + var err error + + wg := &sync.WaitGroup{} + + wg.Add(1) + EncodeRunner.Run(func() { + defer wg.Done() + + cmd := exec.Command("ffmpeg", "-i", inputFile, "-vn", outputFile) + if err = cmd.Start(); err != nil { + return + } + + if err = cmd.Wait(); err != nil { + return + } + }) + + wg.Wait() + return err +}