video aspect ratios categorized

This commit is contained in:
2025-03-16 10:24:09 +02:00
parent 1d0c56f493
commit 3c13056111
2 changed files with 57 additions and 1 deletions

View File

@@ -113,9 +113,29 @@ func (cfg *apiConfig) handlerUploadVideo(w http.ResponseWriter, r *http.Request)
return
}
ratio, err := getVideoAspectRatio(tempFile.Name())
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't get aspect ratio", err)
return
}
var prefix string
switch ratio {
case "16:9":
prefix = "landscape"
break
case "9:16":
prefix = "portrait"
break
default:
prefix = "other"
break
}
tempFile.Seek(0, io.SeekStart)
fileName := videoPathName + extension
fileName := prefix + "/" + videoPathName + extension
_, err = cfg.s3Client.PutObject(context.Background(), &s3.PutObjectInput{
Bucket: &cfg.s3Bucket,

36
main.go
View File

@@ -1,12 +1,16 @@
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"log"
"net/http"
"os"
"os/exec"
"github.com/bootdotdev/learn-file-storage-s3-golang-starter/internal/database"
@@ -32,6 +36,38 @@ type thumbnail struct {
mediaType string
}
func getVideoAspectRatio(filePath string) (string, error) {
cmd := exec.Command("ffprobe", "-v", "error", "-print_format", "json", "-show_streams", filePath)
var buffer bytes.Buffer
cmd.Stdout = &buffer
err := cmd.Run()
if err != nil {
return "", err
}
type Stream struct {
Index int `json:"index"`
Width int `json:"width"`
Height int `json:"height"`
DisplayAspectRatio string `json:"display_aspect_ratio"`
}
type VideoInfo struct {
Streams []Stream `json:"streams"`
}
var videoInfo VideoInfo
err = json.Unmarshal(buffer.Bytes(), &videoInfo)
if err != nil {
fmt.Println(err)
return "", err
}
return videoInfo.Streams[0].DisplayAspectRatio, nil
}
func main() {
godotenv.Load(".env")