package flv import ( "io" "os/exec" "encoding/binary" "os" ) type FLVWriter struct { w io.WriteCloser } func NewFLVWriter(stream_key string) (*FLVWriter, error) { base_dir, _ := os.UserHomeDir() writer := new(FLVWriter) transcoder := exec.Command( "ffmpeg", "-probesize", "500", "-i", "pipe:0", "-c:a", "aac", "-c:v", "h264", "-b:v", "1M", "-hls_time", "6", "-hls_list_size", "4", "-hls_base_url", "/vid/" + stream_key + "/", "-hls_flags", "delete_segments", "-hls_flags", "+program_date_time", "stream.m3u8", ) transcoder.Dir = base_dir + "/live/" + stream_key + "/" flvpipe, err := transcoder.StdinPipe() transcoder.Start() if err != nil { return nil, err } writer.w = flvpipe if err = writer.write_flv_header(); err != nil { return nil, err } return writer, nil } func (writer *FLVWriter) Close() (error) { return writer.w.Close() } func (writer *FLVWriter) WriteMetadataTag(data *[]byte) (err error) { uint24_buf := make([]byte, 4) tag_header := make([]byte, 11) tag_header[0] = 18 binary.BigEndian.PutUint32(uint24_buf, uint32(len((*data)[16:]))) copy(tag_header[1:4], uint24_buf[1:]) if _, err = writer.w.Write(tag_header); err != nil { return } if _, err = writer.w.Write((*data)[16:]); err != nil { return } tag_len_buf := make([]byte, 4) binary.BigEndian.PutUint32(tag_len_buf, uint32(len((*data)[16:]) + 11)) _, err = writer.w.Write(tag_len_buf) return } func (writer *FLVWriter) WriteMediaTag(data *[]byte, timestamp uint32, media_type uint8) (err error) { uint24_buf := make([]byte, 4) tag_header := make([]byte, 11) tag_header[0] = media_type data_len := uint32(len(*data)) binary.BigEndian.PutUint32(uint24_buf, data_len) copy(tag_header[1:4], uint24_buf[1:]) binary.BigEndian.PutUint32(uint24_buf, timestamp) copy(tag_header[4:7], uint24_buf[1:]) tag_header[7] = uint24_buf[0] if _, err = writer.w.Write(tag_header); err != nil { return } if _, err = writer.w.Write(*data); err != nil { return } tag_len_buf := make([]byte, 4) binary.BigEndian.PutUint32(tag_len_buf, data_len + 11) _, err = writer.w.Write(tag_len_buf) return } func (writer *FLVWriter) write_flv_header() (err error) { header := make([]byte, 13) copy(header[:3], "FLV") header[3] = 1 header[4] = 5 header[8] = 9 _, err = writer.w.Write(header) return }