diff --git a/handler_upload_video.go b/handler_upload_video.go index acb6174..d4964c6 100644 --- a/handler_upload_video.go +++ b/handler_upload_video.go @@ -135,12 +135,26 @@ func (cfg *apiConfig) handlerUploadVideo(w http.ResponseWriter, r *http.Request) tempFile.Seek(0, io.SeekStart) + converted, err := processVideoForFastStart(tempFile.Name()) + if err != nil { + respondWithError(w, http.StatusInternalServerError, "Couldn't process video", err) + return + } + fileName := prefix + "/" + videoPathName + extension + cFile, err := os.ReadFile(converted) + if err != nil { + respondWithError(w, http.StatusInternalServerError, "Couldn't read converted file", err) + return + } + + cReader := bytes.NewReader(cFile) + _, err = cfg.s3Client.PutObject(context.Background(), &s3.PutObjectInput{ Bucket: &cfg.s3Bucket, Key: &fileName, - Body: tempFile, + Body: cReader, ContentType: &mimeType, }) if err != nil { diff --git a/main.go b/main.go index dc835bf..40fc39a 100644 --- a/main.go +++ b/main.go @@ -68,6 +68,18 @@ func getVideoAspectRatio(filePath string) (string, error) { } +func processVideoForFastStart(filePath string) (string, error) { + newName := filePath + ".processing" + cmd := exec.Command("ffmpeg", "-i", filePath, "-c", "copy", "-movflags", "faststart", "-f", "mp4", newName) + var buffer bytes.Buffer + cmd.Stdout = &buffer + err := cmd.Run() + if err != nil { + return "", err + } + return newName, nil +} + func main() { godotenv.Load(".env")