2020-06-23 04:11:56 +03:00
|
|
|
package ffmpeg
|
2020-06-10 11:16:17 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"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
|
|
|
|
|
|
|
"github.com/gabek/owncast/config"
|
2020-06-10 11:16:17 +03:00
|
|
|
)
|
|
|
|
|
2020-06-23 04:11:56 +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.
|
|
|
|
ticker := time.NewTicker(20 * time.Second)
|
|
|
|
quit := make(chan struct{})
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2020-06-10 11:16:17 +03:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.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-06-23 04:11:56 +03:00
|
|
|
//TODO: evaluate if this is ever stopped
|
2020-07-07 07:27:31 +03:00
|
|
|
log.Debug("thumbnail generator has stopped")
|
2020-06-10 11:16:17 +03:00
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-07-07 05:45:58 +03:00
|
|
|
func fireThumbnailGenerator(chunkPath string, variantIndex int) error {
|
2020-06-19 03:47:44 +03:00
|
|
|
// JPG takes less time to encode than PNG
|
|
|
|
outputFile := path.Join("webroot", "thumbnail.jpg")
|
2020-06-10 11:16:17 +03:00
|
|
|
|
2020-07-07 05:45:58 +03:00
|
|
|
framePath := path.Join(chunkPath, 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])
|
|
|
|
|
2020-06-19 03:47:44 +03:00
|
|
|
thumbnailCmdFlags := []string{
|
2020-06-23 04:11:56 +03:00
|
|
|
config.Config.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
|
|
|
|
outputFile,
|
|
|
|
}
|
|
|
|
|
|
|
|
ffmpegCmd := strings.Join(thumbnailCmdFlags, " ")
|
2020-06-10 11:16:17 +03:00
|
|
|
|
|
|
|
// fmt.Println(ffmpegCmd)
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
if _, err := exec.Command("sh", "-c", ffmpegCmd).Output(); err != nil {
|
|
|
|
return err
|
2020-06-22 23:41:53 +03:00
|
|
|
}
|
2020-06-23 04:11:56 +03:00
|
|
|
|
|
|
|
return nil
|
2020-06-10 11:16:17 +03:00
|
|
|
}
|