video aspect ratios categorized
This commit is contained in:
@@ -113,9 +113,29 @@ func (cfg *apiConfig) handlerUploadVideo(w http.ResponseWriter, r *http.Request)
|
|||||||
return
|
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)
|
tempFile.Seek(0, io.SeekStart)
|
||||||
|
|
||||||
fileName := videoPathName + extension
|
fileName := prefix + "/" + videoPathName + extension
|
||||||
|
|
||||||
_, err = cfg.s3Client.PutObject(context.Background(), &s3.PutObjectInput{
|
_, err = cfg.s3Client.PutObject(context.Background(), &s3.PutObjectInput{
|
||||||
Bucket: &cfg.s3Bucket,
|
Bucket: &cfg.s3Bucket,
|
||||||
|
|||||||
36
main.go
36
main.go
@@ -1,12 +1,16 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"github.com/aws/aws-sdk-go-v2/config"
|
"github.com/aws/aws-sdk-go-v2/config"
|
||||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
|
||||||
"github.com/bootdotdev/learn-file-storage-s3-golang-starter/internal/database"
|
"github.com/bootdotdev/learn-file-storage-s3-golang-starter/internal/database"
|
||||||
|
|
||||||
@@ -32,6 +36,38 @@ type thumbnail struct {
|
|||||||
mediaType string
|
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() {
|
func main() {
|
||||||
godotenv.Load(".env")
|
godotenv.Load(".env")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user