From 53af3d325fd8f623dc6625539182fcaf7ac9e0f8 Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Fri, 8 Sep 2023 14:56:21 +0500 Subject: [PATCH] Moved transcoder command creation to separate package entirely --- rtmp/flv/writer.go | 26 ++------------------------ transcoder/transcoder.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 24 deletions(-) create mode 100644 transcoder/transcoder.go diff --git a/rtmp/flv/writer.go b/rtmp/flv/writer.go index cfe5beb..341bf3f 100644 --- a/rtmp/flv/writer.go +++ b/rtmp/flv/writer.go @@ -2,9 +2,8 @@ package flv import ( "io" - "os/exec" "encoding/binary" - "os" + "stream_server/transcoder" ) type FLVWriter struct { @@ -12,29 +11,8 @@ type FLVWriter struct { } func NewFLVWriter(stream_key string) (*FLVWriter, error) { - base_dir, _ := os.UserHomeDir() writer := new(FLVWriter) - // spawn ffmpeg as a transcoder + hls segmenter - // most are generic commands, added a prefix to each segment url to make serving it over http easier - transcoder := exec.Command( - "ffmpeg", - "-probesize", "500", - "-i", "pipe:0", - "-c:a", "aac", - "-c:v", "h264", - "-b:v", "1M", - "-g", "30", - "-hls_time", "6", - "-hls_list_size", "4", - "-hls_base_url", "/vid/" + stream_key + "/", - "-hls_segment_type", "fmp4", - "-hls_flags", "delete_segments", - "-hls_flags", "+program_date_time", - "stream.m3u8", - ) - transcoder.Dir = base_dir + "/live/" + stream_key + "/" // shift to the appropriate dir for the given stream key - flvpipe, err := transcoder.StdinPipe() // give control over the ffmpeg input as a stdin pipe (will push in flv packets as they come) - transcoder.Start() // spawn ffmpeg + flvpipe, err := transcoder.NewTranscoder(stream_key) // give control over the ffmpeg input as a stdin pipe (will push in flv packets as they come) if err != nil { return nil, err } diff --git a/transcoder/transcoder.go b/transcoder/transcoder.go new file mode 100644 index 0000000..1ec1051 --- /dev/null +++ b/transcoder/transcoder.go @@ -0,0 +1,40 @@ +package transcoder + +import ( + "os/exec" + "os" + "io" +) + +func NewTranscoder(stream_key string) (io.WriteCloser, error) { + base_dir, _ := os.UserHomeDir() + command := "ffmpeg" + command_args := []string{ + "-probesize", "500", + "-i", "pipe:0", + "-c:a", "libopus", + "-c:v", "vp9", + "-quality", "realtime", + "-speed", "7", + "-tile-columns", "4", + "-frame-parallel", "1", + "-threads", "8", + "-b:v", "1M", + "-g", "30", + "-hls_time", "6", + "-hls_list_size", "4", + "-hls_base_url", "/vid/" + stream_key + "/", + "-hls_segment_type", "fmp4", + "-hls_flags", "delete_segments", + "-hls_flags", "+program_date_time", + "stream.m3u8", + } + transcoder := exec.Command(command, command_args...) + transcoder.Dir = base_dir + "/live/" + stream_key + "/" + data_in, err := transcoder.StdinPipe() + if err != nil { + return nil, err + } + transcoder.Start() + return data_in, nil +}