From f80e86fcf663b41836b50096f71b918578b185cc Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Fri, 18 Aug 2023 15:50:24 +0500 Subject: [PATCH] pipe flv to ffmpeg hls segmentation --- rtmp/data_loop.go | 4 +++- rtmp/flv/writer.go | 25 +++++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/rtmp/data_loop.go b/rtmp/data_loop.go index 51df3d0..a5e23a3 100644 --- a/rtmp/data_loop.go +++ b/rtmp/data_loop.go @@ -2,10 +2,12 @@ package rtmp import ( "stream_server/rtmp/flv" + "os" ) func HandleDataLoop(chnk_wrp_ptr *ChunkWrapper) { - file_writer, err := flv.NewFLVWriter(chnk_wrp_ptr.params.stream_key) + base_dir, _ := os.UserHomeDir() + file_writer, err := flv.NewFLVWriter(base_dir + "/live/" + chnk_wrp_ptr.params.stream_key + "/") defer file_writer.Close() if err != nil { return diff --git a/rtmp/flv/writer.go b/rtmp/flv/writer.go index 8bf7d9f..26e6ad1 100644 --- a/rtmp/flv/writer.go +++ b/rtmp/flv/writer.go @@ -2,22 +2,35 @@ package flv import ( "io" - "os" + "os/exec" "encoding/binary" ) type FLVWriter struct { - w io.Writer + w io.WriteCloser } func NewFLVWriter(stream_dir string) (*FLVWriter, error) { writer := new(FLVWriter) - base_dir, _ := os.UserHomeDir() - filepipe, err := os.Create(base_dir + "/live/" + stream_dir + "/stream.flv") + transcoder := exec.Command( + "ffmpeg", + "-probesize", "5000", + "-i", "pipe:0", + "-c:a", "aac", + "-c:v", "h264", + "-b:v", "1M", + "-hls_time", "6", + "-hls_list_size", "10", + "-hls_flags", "delete_segments", + "stream.m3u8", + ) + transcoder.Dir = stream_dir + flvpipe, err := transcoder.StdinPipe() + transcoder.Start() if err != nil { return nil, err } - writer.w = filepipe + writer.w = flvpipe if err = writer.write_flv_header(); err != nil { return nil, err } @@ -25,7 +38,7 @@ func NewFLVWriter(stream_dir string) (*FLVWriter, error) { } func (writer *FLVWriter) Close() (error) { - return writer.w.(*os.File).Close() + return writer.w.Close() } func (writer *FLVWriter) WriteMetadataTag(data *[]byte) (err error) {