[bugfix] moves file rename to earlier in media pipeline so ffmpeg calls ALWAYS have extension (#3146)

This commit is contained in:
kim 2024-07-28 08:31:49 +00:00 committed by GitHub
parent 87ee64afa0
commit 58f8082795
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 55 additions and 23 deletions

View file

@ -37,27 +37,25 @@ import (
) )
// ffmpegClearMetadata generates a copy (in-place) of input media with all metadata cleared. // ffmpegClearMetadata generates a copy (in-place) of input media with all metadata cleared.
func ffmpegClearMetadata(ctx context.Context, filepath string, ext string) error { func ffmpegClearMetadata(ctx context.Context, filepath string) error {
var outpath string
// Get directory from filepath. // Get directory from filepath.
dirpath := path.Dir(filepath) dirpath := path.Dir(filepath)
// Update filepath to add extension. // Generate cleaned output path MAINTAINING extension.
filepathExt := filepath + "." + ext if i := strings.IndexByte(filepath, '.'); i != -1 {
outpath = filepath[:i] + "_cleaned" + filepath[i:]
// First we need to rename filepath to have extension. } else {
if err := os.Rename(filepath, filepathExt); err != nil { return gtserror.New("input file missing extension")
return gtserror.Newf("error renaming to %s - >%s: %w", filepath, filepathExt, err)
} }
// Generate cleaned output path with ext.
outpath := filepath + "_cleaned." + ext
// Clear metadata with ffmpeg. // Clear metadata with ffmpeg.
if err := ffmpeg(ctx, dirpath, if err := ffmpeg(ctx, dirpath,
"-loglevel", "error", "-loglevel", "error",
// Input file path. // Input file path.
"-i", filepathExt, "-i", filepath,
// Drop all metadata. // Drop all metadata.
"-map_metadata", "-1", "-map_metadata", "-1",
@ -85,13 +83,18 @@ func ffmpegClearMetadata(ctx context.Context, filepath string, ext string) error
// ffmpegGenerateThumb generates a thumbnail webp from input media of any type, useful for any media. // ffmpegGenerateThumb generates a thumbnail webp from input media of any type, useful for any media.
func ffmpegGenerateThumb(ctx context.Context, filepath string, width, height int) (string, error) { func ffmpegGenerateThumb(ctx context.Context, filepath string, width, height int) (string, error) {
var outpath string
// Generate thumb output path REPLACING extension.
if i := strings.IndexByte(filepath, '.'); i != -1 {
outpath = filepath[:i] + "_thumb.webp"
} else {
return "", gtserror.New("input file missing extension")
}
// Get directory from filepath. // Get directory from filepath.
dirpath := path.Dir(filepath) dirpath := path.Dir(filepath)
// Generate output frame file path.
outpath := filepath + "_thumb.webp"
// Thumbnail size scaling argument. // Thumbnail size scaling argument.
scale := strconv.Itoa(width) + ":" + scale := strconv.Itoa(width) + ":" +
strconv.Itoa(height) strconv.Itoa(height)
@ -141,12 +144,18 @@ func ffmpegGenerateThumb(ctx context.Context, filepath string, width, height int
// ffmpegGenerateStatic generates a static png from input image of any type, useful for emoji. // ffmpegGenerateStatic generates a static png from input image of any type, useful for emoji.
func ffmpegGenerateStatic(ctx context.Context, filepath string) (string, error) { func ffmpegGenerateStatic(ctx context.Context, filepath string) (string, error) {
var outpath string
// Generate thumb output path REPLACING extension.
if i := strings.IndexByte(filepath, '.'); i != -1 {
outpath = filepath[:i] + "_static.png"
} else {
return "", gtserror.New("input file missing extension")
}
// Get directory from filepath. // Get directory from filepath.
dirpath := path.Dir(filepath) dirpath := path.Dir(filepath)
// Generate output static file path.
outpath := filepath + "_static.png"
// Generate static with ffmpeg. // Generate static with ffmpeg.
if err := ffmpeg(ctx, dirpath, if err := ffmpeg(ctx, dirpath,
"-loglevel", "error", "-loglevel", "error",

View file

@ -19,6 +19,7 @@ package media
import ( import (
"context" "context"
"os"
errorsv2 "codeberg.org/gruf/go-errors/v2" errorsv2 "codeberg.org/gruf/go-errors/v2"
"codeberg.org/gruf/go-runners" "codeberg.org/gruf/go-runners"
@ -169,6 +170,18 @@ func (p *ProcessingEmoji) store(ctx context.Context) error {
return gtserror.Newf("unsupported emoji filetype: %s (%s)", fileType, ext) return gtserror.Newf("unsupported emoji filetype: %s (%s)", fileType, ext)
} }
// Add file extension to path.
newpath := temppath + "." + ext
// Before ffmpeg processing, rename to set file ext.
if err := os.Rename(temppath, newpath); err != nil {
return gtserror.Newf("error renaming to %s - >%s: %w", temppath, newpath, err)
}
// Update path var
// AFTER successful.
temppath = newpath
// Generate a static image from input emoji path. // Generate a static image from input emoji path.
staticpath, err = ffmpegGenerateStatic(ctx, temppath) staticpath, err = ffmpegGenerateStatic(ctx, temppath)
if err != nil { if err != nil {

View file

@ -19,6 +19,7 @@ package media
import ( import (
"context" "context"
"os"
errorsv2 "codeberg.org/gruf/go-errors/v2" errorsv2 "codeberg.org/gruf/go-errors/v2"
"codeberg.org/gruf/go-runners" "codeberg.org/gruf/go-runners"
@ -185,15 +186,24 @@ func (p *ProcessingMedia) store(ctx context.Context) error {
// Set media type from ffprobe format data. // Set media type from ffprobe format data.
p.media.Type, ext = result.GetFileType() p.media.Type, ext = result.GetFileType()
switch p.media.Type {
// Add file extension to path.
newpath := temppath + "." + ext
// Before ffmpeg processing, rename to set file ext.
if err := os.Rename(temppath, newpath); err != nil {
return gtserror.Newf("error renaming to %s - >%s: %w", temppath, newpath, err)
}
// Update path var
// AFTER successful.
temppath = newpath
switch p.media.Type {
case gtsmodel.FileTypeImage, case gtsmodel.FileTypeImage,
gtsmodel.FileTypeVideo: gtsmodel.FileTypeVideo:
// Pass file through ffmpeg clearing // Pass file through ffmpeg clearing metadata (e.g. EXIF).
// any excess metadata (e.g. EXIF). if err := ffmpegClearMetadata(ctx, temppath); err != nil {
if err := ffmpegClearMetadata(ctx,
temppath, ext,
); err != nil {
return gtserror.Newf("error cleaning metadata: %w", err) return gtserror.Newf("error cleaning metadata: %w", err)
} }