safely generate the thumbnail.jpg & preview.gif (#1279)

This commit is contained in:
Christian 2021-07-28 23:21:02 +02:00 committed by GitHub
parent e72b0c640c
commit cb7a9b89ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,6 +2,7 @@ package transcoder
import (
"io/ioutil"
"os"
"os/exec"
"path"
"strconv"
@ -81,6 +82,7 @@ func fireThumbnailGenerator(segmentPath string, variantIndex int) error {
mostRecentFile := path.Join(framePath, names[0])
ffmpegPath := utils.ValidatedFfmpegPath(data.GetFfMpegPath())
outputFileTemp := path.Join(config.WebRoot, "tempthumbnail.jpg")
thumbnailCmdFlags := []string{
ffmpegPath,
@ -90,12 +92,15 @@ func fireThumbnailGenerator(segmentPath string, variantIndex int) error {
"-i", mostRecentFile, // Input
"-f image2", // format
"-vframes 1", // Single frame
outputFile,
outputFileTemp,
}
ffmpegCmd := strings.Join(thumbnailCmdFlags, " ")
if _, err := exec.Command("sh", "-c", ffmpegCmd).Output(); err != nil {
return err
} else {
// rename temp file
os.Rename(outputFileTemp, outputFile)
}
// If YP support is enabled also create an animated GIF preview
@ -108,6 +113,7 @@ func fireThumbnailGenerator(segmentPath string, variantIndex int) error {
func makeAnimatedGifPreview(sourceFile string, outputFile string) {
ffmpegPath := utils.ValidatedFfmpegPath(data.GetFfMpegPath())
outputFileTemp := path.Join(config.WebRoot, "temppreview.gif")
// Filter is pulled from https://engineering.giphy.com/how-to-make-gifs-with-ffmpeg/
animatedGifFlags := []string{
@ -117,11 +123,14 @@ func makeAnimatedGifPreview(sourceFile string, outputFile string) {
"-i", sourceFile, // Input
"-t 1", // Output is one second in length
"-filter_complex", "\"[0:v] fps=8,scale=w=480:h=-1:flags=lanczos,split [a][b];[a] palettegen=stats_mode=full [p];[b][p] paletteuse=new=1\"",
outputFile,
outputFileTemp,
}
ffmpegCmd := strings.Join(animatedGifFlags, " ")
if _, err := exec.Command("sh", "-c", ffmpegCmd).Output(); err != nil {
log.Errorln(err)
} else {
// rename temp file
os.Rename(outputFileTemp, outputFile)
}
}