Rework vstream handle

This commit is contained in:
Zoe Roux 2024-01-15 23:07:17 +01:00
parent 892060334c
commit 9df5eb4758
2 changed files with 16 additions and 9 deletions

View File

@ -12,13 +12,13 @@ import (
"sync" "sync"
) )
type TranscodeStream interface { type StreamHandle interface {
getTranscodeArgs(segments string) []string getTranscodeArgs(segments string) []string
getOutPath() string getOutPath() string
} }
type Stream struct { type Stream struct {
TranscodeStream handle StreamHandle
file *FileStream file *FileStream
Clients []string Clients []string
// channel open if the segment is not ready. closed if ready. // channel open if the segment is not ready. closed if ready.
@ -35,8 +35,9 @@ type Stream struct {
lock sync.RWMutex lock sync.RWMutex
} }
func NewStream(file *FileStream) Stream { func NewStream(file *FileStream, handle StreamHandle) Stream {
ret := Stream{ ret := Stream{
handle: handle,
file: file, file: file,
Clients: make([]string, 0), Clients: make([]string, 0),
segments: make([]chan struct{}, len(file.Keyframes)), segments: make([]chan struct{}, len(file.Keyframes)),
@ -92,6 +93,12 @@ func (ts *Stream) run(start int32) error {
} }
segments_str := strings.Join(segments, ",") segments_str := strings.Join(segments, ",")
outpath := ts.handle.getOutPath()
err := os.MkdirAll(filepath.Dir(outpath), 0o644)
if err != nil {
return err
}
args := []string{ args := []string{
"-nostats", "-hide_banner", "-loglevel", "warning", "-nostats", "-hide_banner", "-loglevel", "warning",
"-copyts", "-copyts",
@ -100,7 +107,7 @@ func (ts *Stream) run(start int32) error {
"-to", fmt.Sprintf("%.6f", ts.file.Keyframes[end]), "-to", fmt.Sprintf("%.6f", ts.file.Keyframes[end]),
"-i", ts.file.Path, "-i", ts.file.Path,
} }
args = append(args, ts.getTranscodeArgs(segments_str)...) args = append(args, ts.handle.getTranscodeArgs(segments_str)...)
args = append(args, []string{ args = append(args, []string{
"-f", "segment", "-f", "segment",
"-segment_time_delta", "0.2", "-segment_time_delta", "0.2",
@ -109,7 +116,7 @@ func (ts *Stream) run(start int32) error {
"-segment_start_number", fmt.Sprint(start), "-segment_start_number", fmt.Sprint(start),
"-segment_list_type", "flat", "-segment_list_type", "flat",
"-segment_list", "pipe:1", "-segment_list", "pipe:1",
ts.getOutPath(), outpath,
}...) }...)
cmd := exec.Command("ffmpeg", args...) cmd := exec.Command("ffmpeg", args...)

View File

@ -12,10 +12,10 @@ type VideoStream struct {
func NewVideoStream(file *FileStream, quality Quality) *VideoStream { func NewVideoStream(file *FileStream, quality Quality) *VideoStream {
log.Printf("Creating a new video stream for %s in quality %s", file.Path, quality) log.Printf("Creating a new video stream for %s in quality %s", file.Path, quality)
return &VideoStream{ ret := new(VideoStream)
Stream: NewStream(file), ret.quality = quality
quality: quality, ret.Stream = NewStream(file, ret)
} return ret
} }
func (vs *VideoStream) getOutPath() string { func (vs *VideoStream) getOutPath() string {