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) {