limit file encoding to a single file at a time

This commit is contained in:
milarin 2023-02-16 15:33:08 +01:00
parent ce95a8b193
commit 4d68f10de1
4 changed files with 37 additions and 6 deletions

1
go.mod
View File

@ -4,6 +4,7 @@ go 1.20
require ( require (
git.milar.in/milarin/adverr v1.1.0 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/envvars/v2 v2.0.0
git.milar.in/milarin/slices v0.0.6 git.milar.in/milarin/slices v0.0.6
github.com/gorilla/mux v1.8.0 github.com/gorilla/mux v1.8.0

2
go.sum
View File

@ -1,5 +1,7 @@
git.milar.in/milarin/adverr v1.1.0 h1:jD9WnOvs40lfMhvqQ7cllOaRJNBMWr1f07/s9jAadp0= 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/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 h1:DWRQCWaHqzDD8NGpSgv5tYLuF9A/dVFPAtTvz3oiIqE=
git.milar.in/milarin/envvars/v2 v2.0.0/go.mod h1:HkdEi+gG2lJSmVq547bTlQV4qQ0hO333bE8IrE0B9yY= git.milar.in/milarin/envvars/v2 v2.0.0/go.mod h1:HkdEi+gG2lJSmVq547bTlQV4qQ0hO333bE8IrE0B9yY=
git.milar.in/milarin/slices v0.0.6 h1:AQoSarZ58WHYol9c6woWJSe8wFpPC2RC4cvIlZpfg9s= git.milar.in/milarin/slices v0.0.6 h1:AQoSarZ58WHYol9c6woWJSe8wFpPC2RC4cvIlZpfg9s=

View File

@ -193,12 +193,7 @@ func GetEncodeFileHandler(w http.ResponseWriter, r *http.Request) {
fmt.Printf("encode file to %s: '%s' -> '%s'\n", format, inputFilePath, outputFilePath) fmt.Printf("encode file to %s: '%s' -> '%s'\n", format, inputFilePath, outputFilePath)
cmd := exec.Command("ffmpeg", "-i", inputFilePath, "-vn", outputFilePath) if err := EncodeVideo2Mp3(inputFilePath, outputFilePath); err != nil {
if err := cmd.Start(); err != nil {
InternalServerError(w, err)
return
}
if err := cmd.Wait(); err != nil {
InternalServerError(w, err) InternalServerError(w, err)
return return
} }

33
video_encoder.go Normal file
View File

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