summaryrefslogtreecommitdiff
path: root/app/app.go
diff options
context:
space:
mode:
authorHeinrich 'Henrik' Langos <gumbo2000@noreply@mills.io>2023-01-30 19:30:06 +0100
committerHeinrich 'Henrik' Langos <gumbo2000@noreply@mills.io>2023-02-12 12:23:49 +0100
commitb492b372406c1ad457bef31779395f0ba2c28e20 (patch)
tree84c6c0e2c6705eeb8241ae1e8181d1f14f0c7086 /app/app.go
parent400b97abcf68d2b52e827e21a04c00f564bd95f1 (diff)
Refactor down-scaling of uploaded videos
Diffstat (limited to 'app/app.go')
-rw-r--r--app/app.go69
1 files changed, 46 insertions, 23 deletions
diff --git a/app/app.go b/app/app.go
index 617cb17..3028edf 100644
--- a/app/app.go
+++ b/app/app.go
@@ -345,37 +345,34 @@ func (a *App) uploadHandler(respWriter http.ResponseWriter, request *http.Reques
// TODO: Make this a background job
// Resize for lower quality options
- // this seems to be working in the library directly, instead of in the upload directory
- // so this will trigger a heck of a lot imports until it is done.
- // scaled transcoded files should definitely be written to the upload directory
- // and only be moved to the library shelf when they are done.
for size, suffix := range a.Config.Transcoder.Sizes {
log.
WithField("size", size).
- WithField("vf", filepath.Base(newVideoPath)).
+ WithField("vf", filepath.Base(uploadedFile.Name())).
Info("resizing video for lower quality playback")
- sf := fmt.Sprintf(
+ scaledFileName := fmt.Sprintf(
"%s#%s.mp4",
- strings.TrimSuffix(newVideoPath, filepath.Ext(newVideoPath)),
+ strings.TrimSuffix(transcodedVideoPath, filepath.Ext(transcodedVideoPath)),
suffix,
)
-
- if err := utils.RunCmd(
+ _, err = createScaledVideo(
+ uploadedFile.Name(), scaledFileName,
a.Config.Transcoder.Timeout,
- "ffmpeg",
- "-y",
- "-i", newVideoPath,
- "-s", size,
- "-c:v", "libx264",
- "-c:a", "aac",
- "-crf", "18",
- "-strict", "-2",
- "-loglevel", "quiet",
- "-metadata", fmt.Sprintf("title=%s", videoTitleFromUpload),
- "-metadata", fmt.Sprintf("comment=%s", videoDescriptionFromUpload),
- sf,
- ); err != nil {
- err := fmt.Errorf("error transcoding video: %w", err)
+ videoTitleFromUpload, videoDescriptionFromUpload,
+ size)
+ if err != nil {
+ log.Error(err)
+ http.Error(respWriter, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ targetFilename := fmt.Sprintf(
+ "%s#%s.mp4",
+ strings.TrimSuffix(newVideoPath, filepath.Ext(newVideoPath)),
+ suffix,
+ )
+ log.Debugf("Moving %s to %s", scaledFileName, targetFilename)
+ if err := os.Rename(scaledFileName, targetFilename); err != nil {
+ err := fmt.Errorf("error moving scaled video: %w", err)
log.Error(err)
http.Error(respWriter, err.Error(), http.StatusInternalServerError)
return
@@ -388,6 +385,32 @@ func (a *App) uploadHandler(respWriter http.ResponseWriter, request *http.Reques
}
}
+func createScaledVideo(videoFile string, scaledVideoFile string,
+ timeout int,
+ videoTitle string, videoDescription string,
+ size string) (ok bool, err error) {
+
+ if err := utils.RunCmd(
+ timeout,
+ "ffmpeg",
+ "-y",
+ "-i", videoFile,
+ "-s", size,
+ "-c:v", "libx264",
+ "-c:a", "aac",
+ "-crf", "18",
+ "-strict", "-2",
+ "-loglevel", "verbose",
+ "-metadata", fmt.Sprintf("title=%s", videoTitle),
+ "-metadata", fmt.Sprintf("comment=%s", videoDescription),
+ scaledVideoFile,
+ ); err != nil {
+ err := fmt.Errorf("error transcoding video: %w", err)
+ return false, err
+ }
+ return true, nil
+}
+
func createVideo(videoFile string, transcodedVideoPath string,
timeout int, videoTitle, videoDescription string) (ok bool, err error) {