9b7784634b
* Spike: Ping YP service with instance details * WIP: Add to the config to support YP * Add YP response endpoint * Handle YP errors. Use config. Off by default * Show message about YP support on launch * Add animated gif preview when generating thumb * Increase quality of preview gif and only create it if YP is enabled * Do not allow re-registration by clearing the key * Make large and small logos actually structured * Change log level * Fix default YP service URL * Point to default hostname * Set default value for YP to false
116 lines
2.8 KiB
Go
116 lines
2.8 KiB
Go
package ffmpeg
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os/exec"
|
|
"path"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/gabek/owncast/config"
|
|
)
|
|
|
|
//StartThumbnailGenerator starts generating thumbnails
|
|
func StartThumbnailGenerator(chunkPath string, variantIndex int) {
|
|
// Every 20 seconds create a thumbnail from the most
|
|
// recent video segment.
|
|
ticker := time.NewTicker(20 * time.Second)
|
|
quit := make(chan struct{})
|
|
|
|
go func() {
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
if err := fireThumbnailGenerator(chunkPath, variantIndex); err != nil {
|
|
log.Errorln("Unable to generate thumbnail:", err)
|
|
}
|
|
case <-quit:
|
|
//TODO: evaluate if this is ever stopped
|
|
log.Debug("thumbnail generator has stopped")
|
|
ticker.Stop()
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
func fireThumbnailGenerator(chunkPath string, variantIndex int) error {
|
|
// JPG takes less time to encode than PNG
|
|
outputFile := path.Join("webroot", "thumbnail.jpg")
|
|
previewGifFile := path.Join("webroot", "preview.gif")
|
|
|
|
framePath := path.Join(chunkPath, strconv.Itoa(variantIndex))
|
|
files, err := ioutil.ReadDir(framePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var modTime time.Time
|
|
var names []string
|
|
for _, fi := range files {
|
|
if path.Ext(fi.Name()) != ".ts" {
|
|
continue
|
|
}
|
|
|
|
if fi.Mode().IsRegular() {
|
|
if !fi.ModTime().Before(modTime) {
|
|
if fi.ModTime().After(modTime) {
|
|
modTime = fi.ModTime()
|
|
names = names[:0]
|
|
}
|
|
names = append(names, fi.Name())
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(names) == 0 {
|
|
return nil
|
|
}
|
|
|
|
mostRecentFile := path.Join(framePath, names[0])
|
|
|
|
thumbnailCmdFlags := []string{
|
|
config.Config.GetFFMpegPath(),
|
|
"-y", // Overwrite file
|
|
"-threads 1", // Low priority processing
|
|
"-t 1", // Pull from frame 1
|
|
"-i", mostRecentFile, // Input
|
|
"-f image2", // format
|
|
"-vframes 1", // Single frame
|
|
outputFile,
|
|
}
|
|
|
|
ffmpegCmd := strings.Join(thumbnailCmdFlags, " ")
|
|
if _, err := exec.Command("sh", "-c", ffmpegCmd).Output(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// If YP support is enabled also create an animated GIF preview
|
|
if config.Config.YP.Enabled {
|
|
makeAnimatedGifPreview(mostRecentFile, previewGifFile)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func makeAnimatedGifPreview(sourceFile string, outputFile string) {
|
|
// Filter is pulled from https://engineering.giphy.com/how-to-make-gifs-with-ffmpeg/
|
|
animatedGifFlags := []string{
|
|
config.Config.GetFFMpegPath(),
|
|
"-y", // Overwrite file
|
|
"-threads 1", // Low priority processing
|
|
"-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,
|
|
}
|
|
|
|
ffmpegCmd := strings.Join(animatedGifFlags, " ")
|
|
if _, err := exec.Command("sh", "-c", ffmpegCmd).Output(); err != nil {
|
|
log.Errorln(err)
|
|
}
|
|
}
|