pipe flv to ffmpeg hls segmentation

This commit is contained in:
Muaz Ahmad 2023-08-18 15:50:24 +05:00
parent f165efcdc2
commit f80e86fcf6
2 changed files with 22 additions and 7 deletions

View file

@ -2,10 +2,12 @@ package rtmp
import ( import (
"stream_server/rtmp/flv" "stream_server/rtmp/flv"
"os"
) )
func HandleDataLoop(chnk_wrp_ptr *ChunkWrapper) { 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() defer file_writer.Close()
if err != nil { if err != nil {
return return

View file

@ -2,22 +2,35 @@ package flv
import ( import (
"io" "io"
"os" "os/exec"
"encoding/binary" "encoding/binary"
) )
type FLVWriter struct { type FLVWriter struct {
w io.Writer w io.WriteCloser
} }
func NewFLVWriter(stream_dir string) (*FLVWriter, error) { func NewFLVWriter(stream_dir string) (*FLVWriter, error) {
writer := new(FLVWriter) writer := new(FLVWriter)
base_dir, _ := os.UserHomeDir() transcoder := exec.Command(
filepipe, err := os.Create(base_dir + "/live/" + stream_dir + "/stream.flv") "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 { if err != nil {
return nil, err return nil, err
} }
writer.w = filepipe writer.w = flvpipe
if err = writer.write_flv_header(); err != nil { if err = writer.write_flv_header(); err != nil {
return nil, err return nil, err
} }
@ -25,7 +38,7 @@ func NewFLVWriter(stream_dir string) (*FLVWriter, error) {
} }
func (writer *FLVWriter) Close() (error) { func (writer *FLVWriter) Close() (error) {
return writer.w.(*os.File).Close() return writer.w.Close()
} }
func (writer *FLVWriter) WriteMetadataTag(data *[]byte) (err error) { func (writer *FLVWriter) WriteMetadataTag(data *[]byte) (err error) {