0

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 ( import (
"io/ioutil" "io/ioutil"
"os"
"os/exec" "os/exec"
"path" "path"
"strconv" "strconv"
@ -81,6 +82,7 @@ func fireThumbnailGenerator(segmentPath string, variantIndex int) error {
mostRecentFile := path.Join(framePath, names[0]) mostRecentFile := path.Join(framePath, names[0])
ffmpegPath := utils.ValidatedFfmpegPath(data.GetFfMpegPath()) ffmpegPath := utils.ValidatedFfmpegPath(data.GetFfMpegPath())
outputFileTemp := path.Join(config.WebRoot, "tempthumbnail.jpg")
thumbnailCmdFlags := []string{ thumbnailCmdFlags := []string{
ffmpegPath, ffmpegPath,
@ -90,12 +92,15 @@ func fireThumbnailGenerator(segmentPath string, variantIndex int) error {
"-i", mostRecentFile, // Input "-i", mostRecentFile, // Input
"-f image2", // format "-f image2", // format
"-vframes 1", // Single frame "-vframes 1", // Single frame
outputFile, outputFileTemp,
} }
ffmpegCmd := strings.Join(thumbnailCmdFlags, " ") ffmpegCmd := strings.Join(thumbnailCmdFlags, " ")
if _, err := exec.Command("sh", "-c", ffmpegCmd).Output(); err != nil { if _, err := exec.Command("sh", "-c", ffmpegCmd).Output(); err != nil {
return err return err
} else {
// rename temp file
os.Rename(outputFileTemp, outputFile)
} }
// If YP support is enabled also create an animated GIF preview // 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) { func makeAnimatedGifPreview(sourceFile string, outputFile string) {
ffmpegPath := utils.ValidatedFfmpegPath(data.GetFfMpegPath()) 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/ // Filter is pulled from https://engineering.giphy.com/how-to-make-gifs-with-ffmpeg/
animatedGifFlags := []string{ animatedGifFlags := []string{
@ -117,11 +123,14 @@ func makeAnimatedGifPreview(sourceFile string, outputFile string) {
"-i", sourceFile, // Input "-i", sourceFile, // Input
"-t 1", // Output is one second in length "-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\"", "-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, " ") ffmpegCmd := strings.Join(animatedGifFlags, " ")
if _, err := exec.Command("sh", "-c", ffmpegCmd).Output(); err != nil { if _, err := exec.Command("sh", "-c", ffmpegCmd).Output(); err != nil {
log.Errorln(err) log.Errorln(err)
} else {
// rename temp file
os.Rename(outputFileTemp, outputFile)
} }
} }