0

Support video-only streams if there is no audio in the inbound stream. (#401)

Closes #400
This commit is contained in:
Gabe Kangas 2020-12-02 00:19:55 -08:00 committed by GitHub
parent cdbfd89a0f
commit 0b66f3b79f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 28 additions and 5 deletions

View File

@ -27,6 +27,7 @@ type Transcoder struct {
ffmpegPath string ffmpegPath string
segmentIdentifier string segmentIdentifier string
internalListenerPort int internalListenerPort int
videoOnly bool // If true ignore any audio, if any
TranscoderCompleted func(error) TranscoderCompleted func(error)
} }
@ -80,6 +81,9 @@ func (t *Transcoder) Start() {
command := t.getString() command := t.getString()
log.Tracef("Video transcoder started with %d stream variants.", len(t.variants)) log.Tracef("Video transcoder started with %d stream variants.", len(t.variants))
if t.videoOnly {
log.Tracef("Transcoder requested to operate on video only, ignoring audio.")
}
if config.Config.EnableDebugFeatures { if config.Config.EnableDebugFeatures {
log.Println(command) log.Println(command)
@ -252,7 +256,14 @@ func (t *Transcoder) getVariantsString() string {
for _, variant := range t.variants { for _, variant := range t.variants {
variantsCommandFlags = variantsCommandFlags + " " + variant.getVariantString(t) variantsCommandFlags = variantsCommandFlags + " " + variant.getVariantString(t)
variantsStreamMaps = variantsStreamMaps + fmt.Sprintf("v:%d,a:%d ", variant.index, variant.index) singleVariantMap := ""
if t.videoOnly {
singleVariantMap = fmt.Sprintf("v:%d ", variant.index)
} else {
singleVariantMap = fmt.Sprintf("v:%d,a:%d ", variant.index, variant.index)
}
variantsStreamMaps = variantsStreamMaps + singleVariantMap
} }
variantsCommandFlags = variantsCommandFlags + " " + variantsStreamMaps + "\"" variantsCommandFlags = variantsCommandFlags + " " + variantsStreamMaps + "\""
@ -339,12 +350,12 @@ func (v *HLSVariant) SetAudioBitrate(bitrate string) {
func (v *HLSVariant) getAudioQualityString() string { func (v *HLSVariant) getAudioQualityString() string {
if v.isAudioPassthrough { if v.isAudioPassthrough {
return fmt.Sprintf("-map a:0 -c:a:%d copy", v.index) return fmt.Sprintf("-map a:0? -c:a:%d copy", v.index)
} }
// libfdk_aac is not a part of every ffmpeg install, so use "aac" instead // libfdk_aac is not a part of every ffmpeg install, so use "aac" instead
encoderCodec := "aac" encoderCodec := "aac"
return fmt.Sprintf("-map a:0 -c:a:%d %s -b:a:%d %s", v.index, encoderCodec, v.index, v.audioBitrate) return fmt.Sprintf("-map a:0? -c:a:%d %s -b:a:%d %s", v.index, encoderCodec, v.index, v.audioBitrate)
} }
// AddVariant adds a new HLS variant to include in the output. // AddVariant adds a new HLS variant to include in the output.
@ -386,3 +397,8 @@ func (t *Transcoder) SetIdentifier(output string) {
func (t *Transcoder) SetInternalHTTPPort(port int) { func (t *Transcoder) SetInternalHTTPPort(port int) {
t.internalListenerPort = port t.internalListenerPort = port
} }
// SetVideoOnly will ignore any audio streams, if any.
func (t *Transcoder) SetVideoOnly(videoOnly bool) {
t.videoOnly = videoOnly
}

View File

@ -35,7 +35,7 @@ func TestFFmpegCommand(t *testing.T) {
cmd := transcoder.getString() cmd := transcoder.getString()
expected := `/fake/path/ffmpeg -hide_banner -loglevel warning -i fakecontent.flv -map v:0 -c:v:0 libx264 -b:v:0 1200k -maxrate:v:0 1272k -bufsize:v:0 1440k -g:v:0 119 -profile:v:0 high -r:v:0 30 -x264-params:v:0 "scenecut=0:open_gop=0:min-keyint=119:keyint=119" -map a:0 -c:a:0 copy -preset veryfast -map v:0 -c:v:1 libx264 -b:v:1 3500k -maxrate:v:1 3710k -bufsize:v:1 4200k -g:v:1 95 -profile:v:1 high -r:v:1 24 -x264-params:v:1 "scenecut=0:open_gop=0:min-keyint=95:keyint=95" -map a:0 -c:a:1 copy -preset faster -map v:0 -c:v:2 copy -map a:0 -c:a:2 copy -var_stream_map "v:0,a:0 v:1,a:1 v:2,a:2 " -f hls -hls_time 4 -hls_list_size 10 -hls_delete_threshold 10 -tune zerolatency -pix_fmt yuv420p -sc_threshold 0 -master_pl_name stream.m3u8 -strftime 1 -hls_segment_filename http://127.0.0.1:8123/%v/stream-jdofFGg%s.ts -max_muxing_queue_size 400 -method PUT -http_persistent 0 -fflags +genpts http://127.0.0.1:8123/%v/stream.m3u8 2> transcoder.log` expected := `/fake/path/ffmpeg -hide_banner -loglevel warning -i fakecontent.flv -map v:0 -c:v:0 libx264 -b:v:0 1200k -maxrate:v:0 1272k -bufsize:v:0 1440k -g:v:0 119 -profile:v:0 high -r:v:0 30 -x264-params:v:0 "scenecut=0:open_gop=0:min-keyint=119:keyint=119" -map a:0? -c:a:0 copy -preset veryfast -map v:0 -c:v:1 libx264 -b:v:1 3500k -maxrate:v:1 3710k -bufsize:v:1 4200k -g:v:1 95 -profile:v:1 high -r:v:1 24 -x264-params:v:1 "scenecut=0:open_gop=0:min-keyint=95:keyint=95" -map a:0? -c:a:1 copy -preset faster -map v:0 -c:v:2 copy -map a:0? -c:a:2 copy -var_stream_map "v:0,a:0 v:1,a:1 v:2,a:2 " -f hls -hls_time 4 -hls_list_size 10 -hls_delete_threshold 10 -tune zerolatency -pix_fmt yuv420p -sc_threshold 0 -master_pl_name stream.m3u8 -strftime 1 -hls_segment_filename http://127.0.0.1:8123/%v/stream-jdofFGg%s.ts -max_muxing_queue_size 400 -method PUT -http_persistent 0 -fflags +genpts http://127.0.0.1:8123/%v/stream.m3u8 2> transcoder.log`
if cmd != expected { if cmd != expected {
t.Errorf("ffmpeg command does not match expected.\nGot %s\n, want: %s", cmd, expected) t.Errorf("ffmpeg command does not match expected.\nGot %s\n, want: %s", cmd, expected)

View File

@ -27,6 +27,7 @@ func setCurrentBroadcasterInfo(t flvio.Tag, remoteAddr string) {
AudioBitrate: int(data.AudioBitrate), AudioBitrate: int(data.AudioBitrate),
AudioCodec: getAudioCodec(data.AudioCodec), AudioCodec: getAudioCodec(data.AudioCodec),
Encoder: data.Encoder, Encoder: data.Encoder,
VideoOnly: data.AudioCodec == nil,
}, },
} }

View File

@ -70,6 +70,7 @@ func HandleConn(c *rtmp.Conn, nc net.Conn) {
if t.Type == flvio.TAG_AMF0 { if t.Type == flvio.TAG_AMF0 {
log.Tracef("%+v\n", t.DebugFields()) log.Tracef("%+v\n", t.DebugFields())
setCurrentBroadcasterInfo(t, nc.RemoteAddr().String()) setCurrentBroadcasterInfo(t, nc.RemoteAddr().String())
_setStreamAsConnected()
} }
} }
@ -98,7 +99,6 @@ func HandleConn(c *rtmp.Conn, nc net.Conn) {
} }
_hasInboundRTMPConnection = true _hasInboundRTMPConnection = true
_setStreamAsConnected()
_rtmpConnection = nc _rtmpConnection = nc
f, err := os.OpenFile(pipePath, os.O_RDWR, os.ModeNamedPipe) f, err := os.OpenFile(pipePath, os.O_RDWR, os.ModeNamedPipe)

View File

@ -30,6 +30,10 @@ func getInboundDetailsFromMetadata(metadata []interface{}) (models.RTMPStreamMet
} }
func getAudioCodec(codec interface{}) string { func getAudioCodec(codec interface{}) string {
if codec == nil {
return "No audio"
}
var codecID float64 var codecID float64
if assertedCodecID, ok := codec.(float64); ok { if assertedCodecID, ok := codec.(float64); ok {
codecID = assertedCodecID codecID = assertedCodecID

View File

@ -43,6 +43,7 @@ func setStreamAsConnected() {
go func() { go func() {
_transcoder = ffmpeg.NewTranscoder() _transcoder = ffmpeg.NewTranscoder()
_transcoder.SetVideoOnly(_broadcaster.StreamDetails.VideoOnly)
_transcoder.TranscoderCompleted = func(error) { _transcoder.TranscoderCompleted = func(error) {
SetStreamAsDisconnected() SetStreamAsDisconnected()
} }

View File

@ -18,6 +18,7 @@ type InboundStreamDetails struct {
AudioBitrate int `json:"audioBitrate"` AudioBitrate int `json:"audioBitrate"`
AudioCodec string `json:"audioCodec"` AudioCodec string `json:"audioCodec"`
Encoder string `json:"encoder"` Encoder string `json:"encoder"`
VideoOnly bool `json:"-"`
} }
// RTMPStreamMetadata is the raw metadata that comes in with a RTMP connection. // RTMPStreamMetadata is the raw metadata that comes in with a RTMP connection.