Skip to content

Commit

Permalink
improoved download
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirari04 committed Jan 18, 2024
1 parent 4f82509 commit 93ff57b
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions controllers/DownloadVideoController.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"os"
"os/exec"
"regexp"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -50,16 +49,15 @@ func DownloadVideoController(c echo.Context) error {
}
files := []string{}
streamIndex := 0
// add video
for _, quality := range dbLink.File.Qualitys {
if quality.Name == requestValidation.QUALITY {
files = append(files, "-i", fmt.Sprintf(
"%s/%s",
quality.Path,
quality.OutputFile,
))
streamIndex++
}

// add subtitles
for _, subtitle := range dbLink.File.Subtitles {
files = append(files, "-i", fmt.Sprintf(
"%s/%s",
subtitle.Path,
subtitle.OutputFile,
))
streamIndex++
}

// add audios
Expand All @@ -72,18 +70,20 @@ func DownloadVideoController(c echo.Context) error {
streamIndex++
}

// add subtitles
for _, subtitle := range dbLink.File.Subtitles {
files = append(files, "-i", fmt.Sprintf(
"%s/%s",
subtitle.Path,
subtitle.OutputFile,
))
streamIndex++
// add video
for _, quality := range dbLink.File.Qualitys {
if quality.Name == requestValidation.QUALITY {
files = append(files, "-i", fmt.Sprintf(
"%s/%s",
quality.Path,
quality.OutputFile,
))
streamIndex++
}
}

for i := 0; i < streamIndex; i++ {
files = append(files, "-map", fmt.Sprintf("%d:0", i))
files = append(files, "-map", fmt.Sprintf("%d", i))
}

tmpFilePath := fmt.Sprintf("%s/%s-tmp-enc.mkv", config.ENV.FolderVideoUploadsPriv, uuid.NewString())
Expand All @@ -99,28 +99,30 @@ func DownloadVideoController(c echo.Context) error {

// wait until file exists
var tmpFile *os.File
var try = 0
for {
if try > 10 {
c.Logger().Error("Failed to receive output file from ffmpeg")
return nil
}
if tmpFile == nil {
f, err := os.Open(tmpFilePath)
if err != nil {
try++
time.Sleep(time.Second * 1)
continue
}
tmpFile = f
break
}
}
// pipe, err := cmd.StdoutPipe()
// if err != nil {
// c.Logger().Error("Failed to create stdout pipe", err)
// return nil
// }
defer tmpFile.Close()

fileName := regexp.MustCompile(`[^a-zA-Z0-9]+`).ReplaceAllString(dbLink.Name, "-")
if !strings.HasSuffix(fileName, ".mkv") {
fileName = fileName + ".mkv"
}
fileName := fmt.Sprintf(
"%s[%s].mkv",
regexp.MustCompile(`[^a-zA-Z0-9]+`).ReplaceAllString(dbLink.Name, "-"),
requestValidation.QUALITY,
)

c.Response().Header().Add("Content-Type", "video/x-matroska")
c.Response().Header().Add("Transfer-Encoding", "chunked")
Expand All @@ -140,9 +142,9 @@ func DownloadVideoController(c echo.Context) error {
timeStart := time.Now().UnixMilli()
n, err := io.CopyN(c.Response().Writer, tmpFile, speedA)
if err != nil {
// if err.Error() != "EOF" {
c.Logger().Error("Failed to write to buffer", err)
// }
if err.Error() != "EOF" {
c.Logger().Error("Failed to write to buffer", err)
}
break
}
if n > 0 {
Expand Down

0 comments on commit 93ff57b

Please sign in to comment.