From b9a9607e9eff551663f242e10e0da572db475c3f Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 11 Aug 2024 16:21:22 +0200 Subject: [PATCH] Cleanup segments output path --- transcoder/src/audiostream.go | 4 ++-- transcoder/src/stream.go | 32 +++++++++++++++++++++----------- transcoder/src/videostream.go | 4 ++-- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/transcoder/src/audiostream.go b/transcoder/src/audiostream.go index 24f561a4..e5e66417 100644 --- a/transcoder/src/audiostream.go +++ b/transcoder/src/audiostream.go @@ -24,8 +24,8 @@ func (t *Transcoder) NewAudioStream(file *FileStream, idx uint32) (*AudioStream, return ret, nil } -func (as *AudioStream) getOutPath(encoder_id int) string { - return fmt.Sprintf("%s/segment-a%d-%d-%%d.ts", as.file.Out, as.index, encoder_id) +func (as *AudioStream) getIdentifier() string { + return fmt.Sprintf("a%d", as.index) } func (as *AudioStream) getFlags() Flags { diff --git a/transcoder/src/stream.go b/transcoder/src/stream.go index 3692e3ec..6aa97105 100644 --- a/transcoder/src/stream.go +++ b/transcoder/src/stream.go @@ -8,7 +8,6 @@ import ( "math" "os" "os/exec" - "path/filepath" "slices" "strings" "sync" @@ -23,9 +22,12 @@ const ( Transmux Flags = 1 << 3 ) +// First %d is encoder_id, second %d is segment number (escaped for ffmpeg) +const SegmentNameFormat = "%d-segment-%%d.ts" + type StreamHandle interface { getTranscodeArgs(segments string) []string - getOutPath(encoder_id int) string + getIdentifier() string getFlags() Flags } @@ -200,8 +202,8 @@ func (ts *Stream) run(start int32) error { segments = []float64{9999999} } - outpath := ts.handle.getOutPath(encoder_id) - err := os.MkdirAll(filepath.Dir(outpath), 0o755) + outpath := fmt.Sprintf("%s/%s", ts.file.Out, ts.handle.getIdentifier()) + err := os.MkdirAll(outpath, 0o755) if err != nil { return err } @@ -292,7 +294,7 @@ func (ts *Stream) run(start int32) error { go func() { scanner := bufio.NewScanner(stdout) - format := filepath.Base(outpath) + format := fmt.Sprintf(SegmentNameFormat, encoder_id) should_stop := false for scanner.Scan() { @@ -310,7 +312,7 @@ func (ts *Stream) run(start int32) error { if ts.isSegmentReady(segment) { // the current segment is already marked at done so another process has already gone up to here. cmd.Process.Signal(os.Interrupt) - log.Printf("Killing ffmpeg because segment %d is already ready", segment) + log.Printf("Killing ffmpeg %s-%d because segment %d is already ready", ts.handle.getIdentifier(), encoder_id, segment) should_stop = true } else { ts.segments[segment].encoder = encoder_id @@ -320,7 +322,7 @@ func (ts *Stream) run(start int32) error { should_stop = true } else if ts.isSegmentReady(segment + 1) { cmd.Process.Signal(os.Interrupt) - log.Printf("Killing ffmpeg because next segment %d is ready", segment) + log.Printf("Killing ffmpeg %s-%d because next segment %d is ready", ts.handle.getIdentifier(), encoder_id, segment) should_stop = true } } @@ -340,11 +342,11 @@ func (ts *Stream) run(start int32) error { go func() { err := cmd.Wait() if exiterr, ok := err.(*exec.ExitError); ok && exiterr.ExitCode() == 255 { - log.Printf("ffmpeg %d was killed by us", encoder_id) + log.Printf("ffmpeg %s-%d was killed by us", ts.handle.getIdentifier(), encoder_id) } else if err != nil { - log.Printf("ffmpeg %d occured an error: %s: %s", encoder_id, err, stderr.String()) + log.Printf("ffmpeg %s-%d occured an error: %s: %s", ts.handle.getIdentifier(), encoder_id, err, stderr.String()) } else { - log.Printf("ffmpeg %d finished successfully", encoder_id) + log.Printf("ffmpeg %s-%d finished successfully", ts.handle.getIdentifier(), encoder_id) } ts.lock.Lock() @@ -420,7 +422,15 @@ func (ts *Stream) GetSegment(segment int32) (string, error) { } } ts.prerareNextSegements(segment) - return fmt.Sprintf(ts.handle.getOutPath(ts.segments[segment].encoder), segment), nil + return fmt.Sprintf( + "%s/%s/%s", + ts.file.Out, + ts.handle.getIdentifier(), + fmt.Sprintf( + fmt.Sprintf(SegmentNameFormat, ts.segments[segment].encoder), + segment, + ), + ), nil } func (ts *Stream) prerareNextSegements(segment int32) { diff --git a/transcoder/src/videostream.go b/transcoder/src/videostream.go index 95c69a56..0f65dea0 100644 --- a/transcoder/src/videostream.go +++ b/transcoder/src/videostream.go @@ -44,8 +44,8 @@ func (vs *VideoStream) getFlags() Flags { return VideoF } -func (vs *VideoStream) getOutPath(encoder_id int) string { - return fmt.Sprintf("%s/segment-%s-%d-%%d.ts", vs.file.Out, vs.quality, encoder_id) +func (vs *VideoStream) getIdentifier() string { + return fmt.Sprintf("v%d-%s", vs.video.Index, vs.quality) } func closestMultiple(n int32, x int32) int32 {