2021-02-19 10:05:52 +03:00
|
|
|
package transcoder
|
2020-06-10 11:16:17 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2021-07-29 00:21:02 +03:00
|
|
|
"os"
|
2020-06-10 11:16:17 +03:00
|
|
|
"os/exec"
|
|
|
|
"path"
|
2020-07-07 05:45:58 +03:00
|
|
|
"strconv"
|
2020-06-19 03:47:44 +03:00
|
|
|
"strings"
|
2020-06-10 11:16:17 +03:00
|
|
|
"time"
|
2020-06-22 23:41:53 +03:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2020-10-05 20:07:09 +03:00
|
|
|
"github.com/owncast/owncast/config"
|
2021-02-19 10:05:52 +03:00
|
|
|
"github.com/owncast/owncast/core/data"
|
|
|
|
"github.com/owncast/owncast/utils"
|
2020-06-10 11:16:17 +03:00
|
|
|
)
|
|
|
|
|
2020-10-15 00:07:38 +03:00
|
|
|
var _timer *time.Ticker
|
|
|
|
|
|
|
|
func StopThumbnailGenerator() {
|
|
|
|
if _timer != nil {
|
|
|
|
_timer.Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// StartThumbnailGenerator starts generating thumbnails.
|
2020-07-07 05:45:58 +03:00
|
|
|
func StartThumbnailGenerator(chunkPath string, variantIndex int) {
|
2020-06-10 11:16:17 +03:00
|
|
|
// Every 20 seconds create a thumbnail from the most
|
|
|
|
// recent video segment.
|
2020-10-15 00:07:38 +03:00
|
|
|
_timer = time.NewTicker(20 * time.Second)
|
2020-06-10 11:16:17 +03:00
|
|
|
quit := make(chan struct{})
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2020-06-10 11:16:17 +03:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2020-10-15 00:07:38 +03:00
|
|
|
case <-_timer.C:
|
2020-07-07 05:45:58 +03:00
|
|
|
if err := fireThumbnailGenerator(chunkPath, variantIndex); err != nil {
|
2020-06-23 04:11:56 +03:00
|
|
|
log.Errorln("Unable to generate thumbnail:", err)
|
|
|
|
}
|
2020-06-10 11:16:17 +03:00
|
|
|
case <-quit:
|
2020-07-07 07:27:31 +03:00
|
|
|
log.Debug("thumbnail generator has stopped")
|
2020-10-15 00:07:38 +03:00
|
|
|
_timer.Stop()
|
2020-06-10 11:16:17 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-10-15 00:07:38 +03:00
|
|
|
func fireThumbnailGenerator(segmentPath string, variantIndex int) error {
|
2020-06-19 03:47:44 +03:00
|
|
|
// JPG takes less time to encode than PNG
|
2020-10-04 00:35:03 +03:00
|
|
|
outputFile := path.Join(config.WebRoot, "thumbnail.jpg")
|
|
|
|
previewGifFile := path.Join(config.WebRoot, "preview.gif")
|
2020-06-10 11:16:17 +03:00
|
|
|
|
2020-10-15 00:07:38 +03:00
|
|
|
framePath := path.Join(segmentPath, strconv.Itoa(variantIndex))
|
2020-06-23 04:11:56 +03:00
|
|
|
files, err := ioutil.ReadDir(framePath)
|
2020-06-10 11:16:17 +03:00
|
|
|
if err != nil {
|
2020-06-23 04:11:56 +03:00
|
|
|
return err
|
2020-06-10 11:16:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-06-23 04:11:56 +03:00
|
|
|
return nil
|
2020-06-10 11:16:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mostRecentFile := path.Join(framePath, names[0])
|
2021-02-19 10:05:52 +03:00
|
|
|
ffmpegPath := utils.ValidatedFfmpegPath(data.GetFfMpegPath())
|
2021-07-29 00:21:02 +03:00
|
|
|
outputFileTemp := path.Join(config.WebRoot, "tempthumbnail.jpg")
|
2020-06-10 11:16:17 +03:00
|
|
|
|
2020-06-19 03:47:44 +03:00
|
|
|
thumbnailCmdFlags := []string{
|
2021-02-19 10:05:52 +03:00
|
|
|
ffmpegPath,
|
2020-06-19 03:47:44 +03:00
|
|
|
"-y", // Overwrite file
|
|
|
|
"-threads 1", // Low priority processing
|
2020-07-07 08:39:07 +03:00
|
|
|
"-t 1", // Pull from frame 1
|
2020-06-19 03:47:44 +03:00
|
|
|
"-i", mostRecentFile, // Input
|
|
|
|
"-f image2", // format
|
|
|
|
"-vframes 1", // Single frame
|
2021-07-29 00:21:02 +03:00
|
|
|
outputFileTemp,
|
2020-06-19 03:47:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ffmpegCmd := strings.Join(thumbnailCmdFlags, " ")
|
2020-06-23 04:11:56 +03:00
|
|
|
if _, err := exec.Command("sh", "-c", ffmpegCmd).Output(); err != nil {
|
|
|
|
return err
|
2021-07-29 00:21:02 +03:00
|
|
|
} else {
|
|
|
|
// rename temp file
|
|
|
|
os.Rename(outputFileTemp, outputFile)
|
2020-06-22 23:41:53 +03:00
|
|
|
}
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2020-10-02 09:55:38 +03:00
|
|
|
// If YP support is enabled also create an animated GIF preview
|
2021-02-19 10:05:52 +03:00
|
|
|
if data.GetDirectoryEnabled() {
|
2020-10-02 09:55:38 +03:00
|
|
|
makeAnimatedGifPreview(mostRecentFile, previewGifFile)
|
|
|
|
}
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
return nil
|
2020-06-10 11:16:17 +03:00
|
|
|
}
|
2020-10-02 09:55:38 +03:00
|
|
|
|
|
|
|
func makeAnimatedGifPreview(sourceFile string, outputFile string) {
|
2021-02-19 10:05:52 +03:00
|
|
|
ffmpegPath := utils.ValidatedFfmpegPath(data.GetFfMpegPath())
|
2021-07-29 00:21:02 +03:00
|
|
|
outputFileTemp := path.Join(config.WebRoot, "temppreview.gif")
|
2021-02-19 10:05:52 +03:00
|
|
|
|
2020-10-02 09:55:38 +03:00
|
|
|
// Filter is pulled from https://engineering.giphy.com/how-to-make-gifs-with-ffmpeg/
|
|
|
|
animatedGifFlags := []string{
|
2021-02-19 10:05:52 +03:00
|
|
|
ffmpegPath,
|
2020-10-02 09:55:38 +03:00
|
|
|
"-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\"",
|
2021-07-29 00:21:02 +03:00
|
|
|
outputFileTemp,
|
2020-10-02 09:55:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ffmpegCmd := strings.Join(animatedGifFlags, " ")
|
|
|
|
if _, err := exec.Command("sh", "-c", ffmpegCmd).Output(); err != nil {
|
|
|
|
log.Errorln(err)
|
2021-07-29 00:21:02 +03:00
|
|
|
} else {
|
|
|
|
// rename temp file
|
|
|
|
os.Rename(outputFileTemp, outputFile)
|
2020-10-02 09:55:38 +03:00
|
|
|
}
|
|
|
|
}
|