2020-06-23 03:11:56 +02:00
|
|
|
package ffmpeg
|
2020-06-10 10:16:17 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
2020-06-19 02:47:44 +02:00
|
|
|
"strings"
|
2020-06-10 10:16:17 +02:00
|
|
|
"time"
|
2020-06-22 22:41:53 +02:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2020-06-23 03:11:56 +02:00
|
|
|
|
|
|
|
"github.com/gabek/owncast/config"
|
2020-06-10 10:16:17 +02:00
|
|
|
)
|
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
//StartThumbnailGenerator starts generating thumbnails
|
|
|
|
func StartThumbnailGenerator(chunkPath string) {
|
2020-06-10 10:16:17 +02: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 03:11:56 +02:00
|
|
|
|
2020-06-10 10:16:17 +02:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2020-06-23 03:11:56 +02:00
|
|
|
if err := fireThumbnailGenerator(chunkPath); err != nil {
|
|
|
|
log.Errorln("Unable to generate thumbnail:", err)
|
|
|
|
}
|
2020-06-10 10:16:17 +02:00
|
|
|
case <-quit:
|
2020-06-23 03:11:56 +02:00
|
|
|
//TODO: evaluate if this is ever stopped
|
|
|
|
log.Println("thumbnail generator has stopped")
|
2020-06-10 10:16:17 +02:00
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
func fireThumbnailGenerator(chunkPath string) error {
|
2020-06-19 02:47:44 +02:00
|
|
|
// JPG takes less time to encode than PNG
|
|
|
|
outputFile := path.Join("webroot", "thumbnail.jpg")
|
2020-06-10 10:16:17 +02:00
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
framePath := path.Join(chunkPath, "0")
|
|
|
|
files, err := ioutil.ReadDir(framePath)
|
2020-06-10 10:16:17 +02:00
|
|
|
if err != nil {
|
2020-06-23 03:11:56 +02:00
|
|
|
return err
|
2020-06-10 10:16:17 +02: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 03:11:56 +02:00
|
|
|
return nil
|
2020-06-10 10:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mostRecentFile := path.Join(framePath, names[0])
|
|
|
|
|
2020-06-19 02:47:44 +02:00
|
|
|
thumbnailCmdFlags := []string{
|
2020-06-23 03:11:56 +02:00
|
|
|
config.Config.FFMpegPath,
|
2020-06-19 02:47:44 +02:00
|
|
|
"-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, " ")
|
2020-06-10 10:16:17 +02:00
|
|
|
|
|
|
|
// fmt.Println(ffmpegCmd)
|
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
if _, err := exec.Command("sh", "-c", ffmpegCmd).Output(); err != nil {
|
|
|
|
return err
|
2020-06-22 22:41:53 +02:00
|
|
|
}
|
2020-06-23 03:11:56 +02:00
|
|
|
|
|
|
|
return nil
|
2020-06-10 10:16:17 +02:00
|
|
|
}
|