Add ffmpeg command building

This commit is contained in:
Zoe Roux 2024-01-14 03:45:04 +01:00
parent 88406c6ee5
commit 049965cdc9
2 changed files with 133 additions and 1 deletions

View File

@ -1,9 +1,108 @@
package src
import (
"context"
"fmt"
"log"
"os/exec"
"strings"
"sync"
)
func Min(a int32, b int32) int32 {
if a < b {
return a
}
return b
}
type Stream interface {
getTranscodeArgs(segments string) []string
getOutPath() string
}
type TranscodeStream struct {
File FileStream
Stream
file *FileStream
Clients []string
// true if the segment at given index is completed/transcoded, false otherwise
segments []bool
// the lock used for the segments array
lock sync.RWMutex
ctx context.Context
// TODO: add ffmpeg process
}
func NewStream(file *FileStream) (*TranscodeStream, error) {
ret := TranscodeStream{
file: file,
Clients: make([]string, 4),
segments: make([]bool, len(file.Keyframes)),
}
// Start the transcode up to the 100th segment (or less)
ret.run(0, Min(100, int32(len(file.Keyframes))))
return &ret, nil
}
func (ts *TranscodeStream) run(start int32, end int32) error {
log.Printf(
"Starting transcode for %s (from %d to %d out of %d segments)",
ts.file.Path,
start,
end,
len(ts.file.Keyframes),
)
// We do not need the first value (start of the transcode)
segments := make([]string, end-start-1)
for i := range segments {
segments[i] = fmt.Sprintf("%.6f", ts.file.Keyframes[int(start)+i+1])
}
segments_str := strings.Join(segments, ",")
args := []string{
"-nostats", "-hide_banner", "-loglevel", "warning",
"-copyts",
"-ss", fmt.Sprintf("%.6f", ts.file.Keyframes[start]),
"-to", fmt.Sprintf("%.6f", ts.file.Keyframes[end]),
"-i", ts.file.Path,
}
args = append(args, ts.getTranscodeArgs(segments_str)...)
args = append(args, []string{
"-f", "segment",
"-segment_time_delta", "0.2",
"-segment_format", "mpegts",
"-segment_times", segments_str,
"-segment_start_number", fmt.Sprint(start),
"-segment_list_type", "flat",
"-segment_list", "pipe:1",
ts.getOutPath(),
}...)
cmd := exec.CommandContext(
ts.ctx,
"ffmpeg",
args...,
)
log.Printf("Running %s", strings.Join(cmd.Args, " "))
return nil
}
func (ts *TranscodeStream) GetIndex(client string) (string, error) {
index := `#EXTM3U
#EXT-X-VERSION:3
#EXT-X-PLAYLIST-TYPE:VOD
#EXT-X-ALLOW-CACHE:YES
#EXT-X-TARGETDURATION:4
#EXT-X-MEDIA-SEQUENCE:0
`
for segment := 1; segment < len(ts.file.Keyframes); segment++ {
index += fmt.Sprintf("#EXTINF:%.6f\n", ts.file.Keyframes[segment]-ts.file.Keyframes[segment-1])
index += fmt.Sprintf("segment-%d.ts\n", segment)
}
index += `#EXT-X-ENDLIST`
return index, nil
}

View File

@ -0,0 +1,33 @@
package src
import "fmt"
type VideoStream struct {
TranscodeStream
quality Quality
}
func (vs *VideoStream) getOutPath() string {
return fmt.Sprintf("%s/segment-%s-%%03d.ts", vs.file.Out, vs.quality)
}
func (vs *VideoStream) getTranscodeArgs(segments string) []string {
if vs.quality == Original {
return []string{"-map", "0:V:0", "-c:v", "copy"}
}
return []string{
// superfast or ultrafast would produce a file extremly big so we prever veryfast or faster.
"-map", "0:V:0", "-c:v", "libx264", "-crf", "21", "-preset", "faster",
// resize but keep aspect ratio (also force a width that is a multiple of two else some apps behave badly.
"-vf", fmt.Sprintf("scale=-2:'min(%d,ih)'", vs.quality.Height()),
// Even less sure but bufsize are 5x the avergae bitrate since the average bitrate is only
// useful for hls segments.
"-bufsize", fmt.Sprint(vs.quality.MaxBitrate() * 5),
"-b:v", fmt.Sprint(vs.quality.AverageBitrate()),
"-maxrate", fmt.Sprint(vs.quality.MaxBitrate()),
// Force segments to be split exactly on keyframes (only works when transcoding)
"-force_key_frames", segments,
"-strict", "-2",
}
}