fix: replace broken ffmpeg webp encoder with native gif2webp bypass
This commit is contained in:
+16
-6
@@ -35,18 +35,21 @@ func ConvertImageToWebp(imgData []byte) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ConvertVideoToWebp(vidData []byte) ([]byte, error) {
|
func ConvertVideoToWebp(vidData []byte) ([]byte, error) {
|
||||||
webpPath := filepath.Join("temp", fmt.Sprintf("%s.webp", uuid.New().String()))
|
id := uuid.New().String()
|
||||||
|
gifPath := filepath.Join("temp", fmt.Sprintf("%s.gif", id))
|
||||||
|
webpPath := filepath.Join("temp", fmt.Sprintf("%s.webp", id))
|
||||||
|
|
||||||
|
defer os.Remove(gifPath)
|
||||||
defer os.Remove(webpPath)
|
defer os.Remove(webpPath)
|
||||||
|
|
||||||
ffmpegCmd := exec.Command("ffmpeg",
|
ffmpegCmd := exec.Command("ffmpeg",
|
||||||
"-y", "-hide_banner", "-loglevel", "error",
|
"-y", "-hide_banner", "-loglevel", "error",
|
||||||
"-i", "pipe:0", "-f", "mp4",
|
"-i", "pipe:0",
|
||||||
"-ss", "00:00:00", "-t", "00:00:15",
|
"-ss", "00:00:00", "-t", "00:00:15",
|
||||||
"-vf", "fps=10,scale=720:-1:flags=lanczos:force_original_aspect_ratio=increase,crop=512:512,pad=512:512:(ow-iw)/2:(oh-ih)/2:color=#00000000,setsar=1",
|
"-vf", "fps=10,scale=720:-1:flags=lanczos:force_original_aspect_ratio=increase,crop=512:512,pad=512:512:(ow-iw)/2:(oh-ih)/2:color=#00000000,setsar=1",
|
||||||
"-compression_level", "6",
|
"-loop", "0",
|
||||||
"-q:v", "60", "-loop", "0",
|
"-an",
|
||||||
"-preset", "picture", "-an",
|
"-f", "gif", gifPath,
|
||||||
"-f", "webp", webpPath,
|
|
||||||
)
|
)
|
||||||
var stderr bytes.Buffer
|
var stderr bytes.Buffer
|
||||||
ffmpegCmd.Stderr = &stderr
|
ffmpegCmd.Stderr = &stderr
|
||||||
@@ -55,5 +58,12 @@ func ConvertVideoToWebp(vidData []byte) ([]byte, error) {
|
|||||||
return nil, fmt.Errorf("ffmpeg failed: %w, stderr: %s", err, stderr.String())
|
return nil, fmt.Errorf("ffmpeg failed: %w, stderr: %s", err, stderr.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gif2webpCmd := exec.Command("gif2webp", "-quiet", "-q", "60", "-m", "6", gifPath, "-o", webpPath)
|
||||||
|
var g2wStderr bytes.Buffer
|
||||||
|
gif2webpCmd.Stderr = &g2wStderr
|
||||||
|
if err := gif2webpCmd.Run(); err != nil {
|
||||||
|
return nil, fmt.Errorf("gif2webp failed: %w, stderr: %s", err, g2wStderr.String())
|
||||||
|
}
|
||||||
|
|
||||||
return os.ReadFile(webpPath)
|
return os.ReadFile(webpPath)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user