From 24f89aeb1bdece1cffd0e1fe66d18edb3e6a1bf9 Mon Sep 17 00:00:00 2001 From: Ingo Oppermann Date: Tue, 19 Jul 2022 12:25:00 +0200 Subject: [PATCH] Unify FFmpeg prelude parser, add pid awareness The progress and probe parser had both their own prelude parsing implementation in case the json variant is not available. Now there's a new package for this implementation that both use. If a program has an ID (in case of srt+mpegts) then this was no recognized by the parser and these inputs/outputs were not detected. --- ffmpeg/parse/parser.go | 162 +++++------------ ffmpeg/prelude/prelude.go | 191 +++++++++++++++++++++ ffmpeg/prelude/prelude_test.go | 213 +++++++++++++++++++++++ ffmpeg/probe/prober.go | 143 +++------------- ffmpeg/probe/prober_test.go | 305 +++++++++++---------------------- 5 files changed, 569 insertions(+), 445 deletions(-) create mode 100644 ffmpeg/prelude/prelude.go create mode 100644 ffmpeg/prelude/prelude_test.go diff --git a/ffmpeg/parse/parser.go b/ffmpeg/parse/parser.go index 35d08e7a..78bfe64f 100644 --- a/ffmpeg/parse/parser.go +++ b/ffmpeg/parse/parser.go @@ -10,6 +10,7 @@ import ( "sync" "time" + "github.com/datarhei/core/v16/ffmpeg/prelude" "github.com/datarhei/core/v16/log" "github.com/datarhei/core/v16/net/url" "github.com/datarhei/core/v16/process" @@ -552,131 +553,60 @@ func (p *parser) Prelude() []string { } func (p *parser) parsePrelude() bool { - process := ffmpegProcess{} - p.lock.progress.Lock() defer p.lock.progress.Unlock() - // Input #0, lavfi, from 'testsrc=size=1280x720:rate=25': - // Input #1, lavfi, from 'anullsrc=r=44100:cl=stereo': - // Output #0, hls, to './data/testsrc.m3u8': - reFormat := regexp.MustCompile(`^(Input|Output) #([0-9]+), (.*?), (from|to) '([^']+)`) - - // Stream #0:0: Video: rawvideo (RGB[24] / 0x18424752), rgb24, 1280x720 [SAR 1:1 DAR 16:9], 25 tbr, 25 tbn, 25 tbc - // Stream #1:0: Audio: pcm_u8, 44100 Hz, stereo, u8, 705 kb/s - // Stream #0:0: Video: h264 (libx264), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 25 fps, 90k tbn, 25 tbc - // Stream #0:1(eng): Audio: aac (LC), 44100 Hz, stereo, fltp, 64 kb/s - reStream := regexp.MustCompile(`Stream #([0-9]+):([0-9]+)(?:\(([a-z]+)\))?: (Video|Audio|Subtitle): (.*)`) - reStreamCodec := regexp.MustCompile(`^([^\s,]+)`) - reStreamVideoSize := regexp.MustCompile(`, ([0-9]+)x([0-9]+)`) - //reStreamVideoFPS := regexp.MustCompile(`, ([0-9]+) fps`) - reStreamAudio := regexp.MustCompile(`, ([0-9]+) Hz, ([^,]+)`) - //reStreamBitrate := regexp.MustCompile(`, ([0-9]+) kb/s`) - - reStreamMapping := regexp.MustCompile(`^Stream mapping:`) - reStreamMap := regexp.MustCompile(`^[\s]+Stream #[0-9]+:[0-9]+`) - - //format := InputOutput{} - - formatType := "" - formatURL := "" - - var noutputs int - streamMapping := false - data := p.Prelude() - for _, line := range data { - if reStreamMapping.MatchString(line) { - streamMapping = true - continue - } + inputs, outputs, noutputs := prelude.Parse(data) - if streamMapping { - if reStreamMap.MatchString(line) { - noutputs++ - } else { - streamMapping = false - } - - continue - } - - if matches := reFormat.FindStringSubmatch(line); matches != nil { - formatType = strings.ToLower(matches[1]) - formatURL = matches[5] - - continue - } - - if matches := reStream.FindStringSubmatch(line); matches != nil { - format := ffmpegProcessIO{} - - format.Address = formatURL - if ip, _ := url.Lookup(format.Address); len(ip) != 0 { - format.IP = ip - } - - if x, err := strconv.ParseUint(matches[1], 10, 64); err == nil { - format.Index = x - } - - if x, err := strconv.ParseUint(matches[2], 10, 64); err == nil { - format.Stream = x - } - format.Type = strings.ToLower(matches[4]) - - streamDetail := matches[5] - - if matches = reStreamCodec.FindStringSubmatch(streamDetail); matches != nil { - format.Codec = matches[1] - } - /* - if matches = reStreamBitrate.FindStringSubmatch(streamDetail); matches != nil { - if x, err := strconv.ParseFloat(matches[1], 64); err == nil { - format.Bitrate = x - } - } - */ - if format.Type == "video" { - if matches = reStreamVideoSize.FindStringSubmatch(streamDetail); matches != nil { - if x, err := strconv.ParseUint(matches[1], 10, 64); err == nil { - format.Width = x - } - if x, err := strconv.ParseUint(matches[2], 10, 64); err == nil { - format.Height = x - } - } - /* - if matches = reStreamVideoFPS.FindStringSubmatch(streamDetail); matches != nil { - if x, err := strconv.ParseFloat(matches[1], 64); err == nil { - format.FPS = x - } - } - */ - } else if format.Type == "audio" { - if matches = reStreamAudio.FindStringSubmatch(streamDetail); matches != nil { - if x, err := strconv.ParseUint(matches[1], 10, 64); err == nil { - format.Sampling = x - } - format.Layout = matches[2] - } - } - - if formatType == "input" { - process.input = append(process.input, format) - } else { - process.output = append(process.output, format) - } - } - } - - if len(process.output) != noutputs { + if len(outputs) != noutputs { return false } - p.process.input = process.input - p.process.output = process.output + for _, in := range inputs { + io := ffmpegProcessIO{ + Address: in.Address, + Format: in.Format, + Index: in.Index, + Stream: in.Stream, + Type: in.Type, + Codec: in.Codec, + Pixfmt: in.Pixfmt, + Width: in.Width, + Height: in.Height, + Sampling: in.Sampling, + Layout: in.Layout, + } + + if ip, _ := url.Lookup(io.Address); len(ip) != 0 { + io.IP = ip + } + + p.process.input = append(p.process.input, io) + } + + for _, out := range outputs { + io := ffmpegProcessIO{ + Address: out.Address, + Format: out.Format, + Index: out.Index, + Stream: out.Stream, + Type: out.Type, + Codec: out.Codec, + Pixfmt: out.Pixfmt, + Width: out.Width, + Height: out.Height, + Sampling: out.Sampling, + Layout: out.Layout, + } + + if ip, _ := url.Lookup(io.Address); len(ip) != 0 { + io.IP = ip + } + + p.process.output = append(p.process.output, io) + } return true } diff --git a/ffmpeg/prelude/prelude.go b/ffmpeg/prelude/prelude.go new file mode 100644 index 00000000..2e55567b --- /dev/null +++ b/ffmpeg/prelude/prelude.go @@ -0,0 +1,191 @@ +package prelude + +import ( + "regexp" + "strconv" + "strings" +) + +type IO struct { + // common + Address string + Format string + Index uint64 + Stream uint64 + Language string + Type string + Codec string + Coder string + Bitrate float64 // kbps + Duration float64 // sec + + // video + FPS float64 + Pixfmt string + Width uint64 + Height uint64 + + // audio + Sampling uint64 // Hz + Layout string + Channels uint64 +} + +// Parse parses the inputs and outputs from the default FFmpeg output. It returns a list of +// detected inputs and outputs as well as the number of outputs according to the stream mapping. +func Parse(lines []string) (inputs, outputs []IO, noutputs int) { + // Input #0, lavfi, from 'testsrc=size=1280x720:rate=25': + // Input #1, lavfi, from 'anullsrc=r=44100:cl=stereo': + // Output #0, hls, to './data/testsrc.m3u8': + reFormat := regexp.MustCompile(`^(Input|Output) #([0-9]+), (.*?), (from|to) '([^']+)`) + + // Duration: 00:01:02.28, start: 0.000000, bitrate: 5895 kb/s + // Duration: N/A, start: 0.000000, bitrate: 5895 kb/s + reDuration := regexp.MustCompile(`Duration: ([0-9]+):([0-9]+):([0-9]+)\.([0-9]+)`) + + // Stream #0:0: Video: rawvideo (RGB[24] / 0x18424752), rgb24, 1280x720 [SAR 1:1 DAR 16:9], 25 tbr, 25 tbn, 25 tbc + // Stream #1:0: Audio: pcm_u8, 44100 Hz, stereo, u8, 705 kb/s + // Stream #0:0: Video: h264 (libx264), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 25 fps, 90k tbn, 25 tbc + // Stream #0:1: Audio: aac (LC), 44100 Hz, stereo, fltp, 64 kb/s + // Stream #4:0[0x100]: Video: h264 (Main) ([27][0][0][0] / 0x001B), yuv420p(tv, smpte170m/bt709/bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], 25 tbr, 90k tbn + // Stream #4:1[0x101]: Audio: aac (LC) ([15][0][0][0] / 0x000F), 48000 Hz, stereo, fltp, 162 kb/s + reStream := regexp.MustCompile(`Stream #([0-9]+):([0-9]+)(?:\[[0-9a-fx]+\])?(?:\(([a-z]+)\))?: (Video|Audio|Subtitle): (.*)`) + reStreamCodec := regexp.MustCompile(`^([^\s,]+)`) + reStreamVideoPixfmtSize := regexp.MustCompile(`, ([0-9A-Za-z]+)(\([^\)]+\))?, ([0-9]+)x([0-9]+)`) + reStreamVideoFPS := regexp.MustCompile(`, ([0-9]+(\.[0-9]+)?) fps`) + reStreamAudio := regexp.MustCompile(`, ([0-9]+) Hz, ([^,]+)`) + reStreamBitrate := regexp.MustCompile(`, ([0-9]+) kb/s`) + + reStreamMapping := regexp.MustCompile(`^Stream mapping:`) + reStreamMap := regexp.MustCompile(`^[\s]+Stream #[0-9]+:[0-9]+`) + + iotype := "" + format := "" + address := "" + var duration float64 = 0.0 + + streamMapping := false + + for _, line := range lines { + if reStreamMapping.MatchString(line) { + streamMapping = true + continue + } + + if streamMapping { + if reStreamMap.MatchString(line) { + noutputs++ + } else { + streamMapping = false + } + + continue + } + + if matches := reFormat.FindStringSubmatch(line); matches != nil { + iotype = matches[1] + format = matches[3] + address = matches[5] + duration = 0 + + continue + } + + if matches := reDuration.FindStringSubmatch(line); matches != nil { + duration = 0.0 + + // hours + if x, err := strconv.ParseFloat(matches[1], 64); err == nil { + duration += x * 60 * 60 + } + + // minutes + if x, err := strconv.ParseFloat(matches[2], 64); err == nil { + duration += x * 60 + } + + // seconds + if x, err := strconv.ParseFloat(matches[3], 64); err == nil { + duration += x + } + + // fractions + if x, err := strconv.ParseFloat(matches[4], 64); err == nil { + duration += x / 100 + } + + continue + } + + if matches := reStream.FindStringSubmatch(line); matches != nil { + io := IO{} + + io.Address = address + io.Format = format + io.Duration = duration + + if x, err := strconv.ParseUint(matches[1], 10, 64); err == nil { + io.Index = x + } + + if x, err := strconv.ParseUint(matches[2], 10, 64); err == nil { + io.Stream = x + } + + io.Language = "und" + if len(matches[3]) == 3 { + io.Language = matches[3] + } + + io.Type = strings.ToLower(matches[4]) + + streamDetail := matches[5] + + if matches = reStreamCodec.FindStringSubmatch(streamDetail); matches != nil { + io.Codec = matches[1] + } + + if matches = reStreamBitrate.FindStringSubmatch(streamDetail); matches != nil { + if x, err := strconv.ParseFloat(matches[1], 64); err == nil { + io.Bitrate = x + } + } + + if io.Type == "video" { + if matches = reStreamVideoPixfmtSize.FindStringSubmatch(streamDetail); matches != nil { + io.Pixfmt = matches[1] + + if x, err := strconv.ParseUint(matches[3], 10, 64); err == nil { + io.Width = x + } + if x, err := strconv.ParseUint(matches[4], 10, 64); err == nil { + io.Height = x + } + } + + if matches = reStreamVideoFPS.FindStringSubmatch(streamDetail); matches != nil { + if x, err := strconv.ParseFloat(matches[1], 64); err == nil { + io.FPS = x + } + } + + } else if io.Type == "audio" { + if matches = reStreamAudio.FindStringSubmatch(streamDetail); matches != nil { + if x, err := strconv.ParseUint(matches[1], 10, 64); err == nil { + io.Sampling = x + } + + io.Layout = matches[2] + } + } + + if iotype == "Input" { + inputs = append(inputs, io) + } else if iotype == "Output" { + outputs = append(outputs, io) + } + } + } + + return +} diff --git a/ffmpeg/prelude/prelude_test.go b/ffmpeg/prelude/prelude_test.go new file mode 100644 index 00000000..9c7768e6 --- /dev/null +++ b/ffmpeg/prelude/prelude_test.go @@ -0,0 +1,213 @@ +package prelude + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestPrelude(t *testing.T) { + rawdata := `ffmpeg version 4.0.2 Copyright (c) 2000-2018 the FFmpeg developers + built with Apple LLVM version 9.1.0 (clang-902.0.39.2) + configuration: --prefix=/usr/local/Cellar/ffmpeg/4.0.2 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-gpl --enable-libmp3lame --enable-libx264 --enable-libx265 --enable-libxvid --enable-opencl --enable-videotoolbox --disable-lzma + libavutil 56. 14.100 / 56. 14.100 + libavcodec 58. 18.100 / 58. 18.100 + libavformat 58. 12.100 / 58. 12.100 + libavdevice 58. 3.100 / 58. 3.100 + libavfilter 7. 16.100 / 7. 16.100 + libavresample 4. 0. 0 / 4. 0. 0 + libswscale 5. 1.100 / 5. 1.100 + libswresample 3. 1.100 / 3. 1.100 + libpostproc 55. 1.100 / 55. 1.100 +Input #0, lavfi, from 'testsrc=size=1280x720:rate=25': + Duration: N/A, start: 0.000000, bitrate: N/A + Stream #0:0: Video: rawvideo (RGB[24] / 0x18424752), rgb24, 1280x720 [SAR 1:1 DAR 16:9], 25 tbr, 25 tbn, 25 tbc +Input #1, lavfi, from 'anullsrc=r=44100:cl=stereo': + Duration: N/A, start: 0.000000, bitrate: 705 kb/s + Stream #1:0: Audio: pcm_u8, 44100 Hz, stereo, u8, 705 kb/s +Input #2, playout, from 'playout:rtmp://l5gn74l5-vpu.livespotting.com/live/0chl6hu7_360?token=m5ZuiCQYRlIon8': + Duration: N/A, start: 0.000000, bitrate: 265 kb/s + Stream #2:0: Video: h264 (Constrained Baseline), yuvj420p(pc, progressive), 640x360 [SAR 1:1 DAR 16:9], 265 kb/s, 10 fps, 10 tbr, 1000k tbn, 20 tbc +Input #3, mov,mp4,m4a,3gp,3g2,mj2, from 'movie.mp4': + Metadata: + major_brand : isom + minor_version : 512 + compatible_brands: isomiso2avc1mp41 + encoder : Lavf58.20.100 + Duration: 00:01:02.28, start: 0.000000, bitrate: 5895 kb/s + Stream #3:0(eng): Video: h264 (Main) (avc1 / 0x31637661), yuvj420p(pc, bt709), 2560x1440 [SAR 1:1 DAR 16:9], 5894 kb/s, 23.98 fps, 25 tbr, 90k tbn, 50 tbc (default) + Stream #3:1(por): Subtitle: subrip +Input #4, mpegts, from 'srt://localhost:6000?mode=caller&transtype=live&streamid=#!:m=request,r=ingest/ad045490-8233-4f31-a296-ea5771a340ac&passphrase=foobarfoobar': + Duration: N/A, start: 71.786667, bitrate: N/A + Program 1 + Metadata: + service_name : Service01 + service_provider: FFmpeg + Stream #4:0[0x100]: Video: h264 (Main) ([27][0][0][0] / 0x001B), yuv420p(tv, smpte170m/bt709/bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], 25 tbr, 90k tbn + Stream #4:1[0x101]: Audio: aac (LC) ([15][0][0][0] / 0x000F), 48000 Hz, stereo, fltp, 162 kb/s +Stream mapping: + Stream #0:0 -> #0:0 (rawvideo (native) -> h264 (libx264)) + Stream #1:0 -> #0:1 (pcm_u8 (native) -> aac (native)) +Press [q] to stop, [?] for help +[libx264 @ 0x7fa96a800600] using SAR=1/1 +[libx264 @ 0x7fa96a800600] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2 +[libx264 @ 0x7fa96a800600] profile Constrained Baseline, level 3.1 +[libx264 @ 0x7fa96a800600] 264 - core 152 r2854 e9a5903 - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=50 keyint_min=5 scenecut=0 intra_refresh=0 rc=crf mbtree=0 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0 +[hls @ 0x7fa969803a00] Opening './data/testsrc5375.ts.tmp' for writing +Output #0, hls, to './data/testsrc.m3u8': + Metadata: + encoder : Lavf58.12.100 + Stream #0:0: Video: h264 (libx264), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 25 fps, 90k tbn, 25 tbc + Metadata: + encoder : Lavc58.18.100 libx264 + Side data: + cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1 + Stream #0:1: Audio: aac (LC), 44100 Hz, stereo, fltp, 64 kb/s + Metadata: + encoder : Lavc58.18.100 aac +[hls @ 0x7fa969803a00] Opening './data/testsrc5376.ts.tmp' for writing=0.872x +[hls @ 0x7fa969803a00] Opening './data/testsrc.m3u8.tmp' for writing +[hls @ 0x7fa969803a00] Opening './data/testsrc.m3u8.tmp' for writing +frame= 58 fps= 25 q=-1.0 Lsize=N/A time=00:00:02.32 bitrate=N/A speed=0.999x` + + data := strings.Split(rawdata, "\n") + + inputs, outputs, noutputs := Parse(data) + + require.Equal(t, 7, len(inputs)) + require.Equal(t, 2, len(outputs)) + require.Equal(t, 2, noutputs) + + i := inputs[0] + + require.Equal(t, "testsrc=size=1280x720:rate=25", i.Address) + require.Equal(t, "lavfi", i.Format) + require.Equal(t, uint64(0), i.Index) + require.Equal(t, uint64(0), i.Stream) + require.Equal(t, "und", i.Language) + require.Equal(t, "video", i.Type) + require.Equal(t, "rawvideo", i.Codec) + require.Equal(t, 0.0, i.Bitrate) + require.Equal(t, 0.0, i.Duration) + require.Equal(t, 0.0, i.FPS) + require.Equal(t, "rgb24", i.Pixfmt) + require.Equal(t, uint64(1280), i.Width) + require.Equal(t, uint64(720), i.Height) + + i = inputs[1] + + require.Equal(t, "anullsrc=r=44100:cl=stereo", i.Address) + require.Equal(t, "lavfi", i.Format) + require.Equal(t, uint64(1), i.Index) + require.Equal(t, uint64(0), i.Stream) + require.Equal(t, "und", i.Language) + require.Equal(t, "audio", i.Type) + require.Equal(t, "pcm_u8", i.Codec) + require.Equal(t, 705.0, i.Bitrate) + require.Equal(t, 0.0, i.Duration) + require.Equal(t, uint64(44100), i.Sampling) + require.Equal(t, "stereo", i.Layout) + + i = inputs[2] + + require.Equal(t, "playout:rtmp://l5gn74l5-vpu.livespotting.com/live/0chl6hu7_360?token=m5ZuiCQYRlIon8", i.Address) + require.Equal(t, "playout", i.Format) + require.Equal(t, uint64(2), i.Index) + require.Equal(t, uint64(0), i.Stream) + require.Equal(t, "und", i.Language) + require.Equal(t, "video", i.Type) + require.Equal(t, "h264", i.Codec) + require.Equal(t, 265.0, i.Bitrate) + require.Equal(t, 0.0, i.Duration) + require.Equal(t, 10.0, i.FPS) + require.Equal(t, "yuvj420p", i.Pixfmt) + require.Equal(t, uint64(640), i.Width) + require.Equal(t, uint64(360), i.Height) + + i = inputs[3] + + require.Equal(t, "movie.mp4", i.Address) + require.Equal(t, "mov,mp4,m4a,3gp,3g2,mj2", i.Format) + require.Equal(t, uint64(3), i.Index) + require.Equal(t, uint64(0), i.Stream) + require.Equal(t, "eng", i.Language) + require.Equal(t, "video", i.Type) + require.Equal(t, "h264", i.Codec) + require.Equal(t, 5894.0, i.Bitrate) + require.Equal(t, 62.28, i.Duration) + require.Equal(t, 23.98, i.FPS) + require.Equal(t, "yuvj420p", i.Pixfmt) + require.Equal(t, uint64(2560), i.Width) + require.Equal(t, uint64(1440), i.Height) + + i = inputs[4] + + require.Equal(t, "movie.mp4", i.Address) + require.Equal(t, "mov,mp4,m4a,3gp,3g2,mj2", i.Format) + require.Equal(t, uint64(3), i.Index) + require.Equal(t, uint64(1), i.Stream) + require.Equal(t, "por", i.Language) + require.Equal(t, "subtitle", i.Type) + require.Equal(t, "subrip", i.Codec) + + i = inputs[5] + + require.Equal(t, "srt://localhost:6000?mode=caller&transtype=live&streamid=#!:m=request,r=ingest/ad045490-8233-4f31-a296-ea5771a340ac&passphrase=foobarfoobar", i.Address) + require.Equal(t, "mpegts", i.Format) + require.Equal(t, uint64(4), i.Index) + require.Equal(t, uint64(0), i.Stream) + require.Equal(t, "und", i.Language) + require.Equal(t, "video", i.Type) + require.Equal(t, "h264", i.Codec) + require.Equal(t, 0.0, i.Bitrate) + require.Equal(t, 0.0, i.Duration) + require.Equal(t, 0.0, i.FPS) + require.Equal(t, "yuv420p", i.Pixfmt) + require.Equal(t, uint64(1920), i.Width) + require.Equal(t, uint64(1080), i.Height) + + i = inputs[6] + + require.Equal(t, "srt://localhost:6000?mode=caller&transtype=live&streamid=#!:m=request,r=ingest/ad045490-8233-4f31-a296-ea5771a340ac&passphrase=foobarfoobar", i.Address) + require.Equal(t, "mpegts", i.Format) + require.Equal(t, uint64(4), i.Index) + require.Equal(t, uint64(1), i.Stream) + require.Equal(t, "und", i.Language) + require.Equal(t, "audio", i.Type) + require.Equal(t, "aac", i.Codec) + require.Equal(t, 162.0, i.Bitrate) + require.Equal(t, 0.0, i.Duration) + require.Equal(t, uint64(48000), i.Sampling) + require.Equal(t, "stereo", i.Layout) + + i = outputs[0] + + require.Equal(t, "./data/testsrc.m3u8", i.Address) + require.Equal(t, "hls", i.Format) + require.Equal(t, uint64(0), i.Index) + require.Equal(t, uint64(0), i.Stream) + require.Equal(t, "und", i.Language) + require.Equal(t, "video", i.Type) + require.Equal(t, "h264", i.Codec) + require.Equal(t, 0.0, i.Bitrate) + require.Equal(t, 0.0, i.Duration) + require.Equal(t, 25.0, i.FPS) + require.Equal(t, "yuv420p", i.Pixfmt) + require.Equal(t, uint64(1280), i.Width) + require.Equal(t, uint64(720), i.Height) + + i = outputs[1] + + require.Equal(t, "./data/testsrc.m3u8", i.Address) + require.Equal(t, "hls", i.Format) + require.Equal(t, uint64(0), i.Index) + require.Equal(t, uint64(1), i.Stream) + require.Equal(t, "und", i.Language) + require.Equal(t, "audio", i.Type) + require.Equal(t, "aac", i.Codec) + require.Equal(t, 64.0, i.Bitrate) + require.Equal(t, 0.0, i.Duration) + require.Equal(t, uint64(44100), i.Sampling) + require.Equal(t, "stereo", i.Layout) +} diff --git a/ffmpeg/probe/prober.go b/ffmpeg/probe/prober.go index 0c804f6f..19cd4c3d 100644 --- a/ffmpeg/probe/prober.go +++ b/ffmpeg/probe/prober.go @@ -2,11 +2,10 @@ package probe import ( "encoding/json" - "regexp" - "strconv" "strings" "time" + "github.com/datarhei/core/v16/ffmpeg/prelude" "github.com/datarhei/core/v16/log" "github.com/datarhei/core/v16/process" "github.com/datarhei/core/v16/restream/app" @@ -84,128 +83,32 @@ func (p *prober) parseJSON(line string) { } func (p *prober) parseDefault() { - // Input #0, lavfi, from 'testsrc=size=1280x720:rate=25': - // Input #1, lavfi, from 'anullsrc=r=44100:cl=stereo': - // Output #0, hls, to './data/testsrc.m3u8': - reFormat := regexp.MustCompile(`^Input #([0-9]+), (.*?), (from|to) '([^']+)`) + lines := make([]string, len(p.data)) - // Duration: 00:01:02.28, start: 0.000000, bitrate: 5895 kb/s - // Duration: N/A, start: 0.000000, bitrate: 5895 kb/s - reDuration := regexp.MustCompile(`Duration: ([0-9]+):([0-9]+):([0-9]+)\.([0-9]+)`) + for i, line := range p.data { + lines[i] = line.Data + } - // Stream #0:0: Video: rawvideo (RGB[24] / 0x18424752), rgb24, 1280x720 [SAR 1:1 DAR 16:9], 25 tbr, 25 tbn, 25 tbc - // Stream #1:0: Audio: pcm_u8, 44100 Hz, stereo, u8, 705 kb/s - // Stream #0:0: Video: h264 (libx264), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 25 fps, 90k tbn, 25 tbc - // Stream #0:1: Audio: aac (LC), 44100 Hz, stereo, fltp, 64 kb/s - reStream := regexp.MustCompile(`Stream #([0-9]+):([0-9]+)(?:\(([a-z]+)\))?: (Video|Audio|Subtitle): (.*)`) - reStreamCodec := regexp.MustCompile(`^([^\s,]+)`) - reStreamVideoPixfmtSize := regexp.MustCompile(`, ([0-9A-Za-z]+)(\([^\)]+\))?, ([0-9]+)x([0-9]+)`) - reStreamVideoFPS := regexp.MustCompile(`, ([0-9]+(\.[0-9]+)?) fps`) - reStreamAudio := regexp.MustCompile(`, ([0-9]+) Hz, ([^,]+)`) - reStreamBitrate := regexp.MustCompile(`, ([0-9]+) kb/s`) + inputs, _, _ := prelude.Parse(lines) - format := "" - address := "" - var duration float64 = 0.0 + p.inputs = make([]probeIO, len(inputs)) - for _, line := range p.data { - if matches := reFormat.FindStringSubmatch(line.Data); matches != nil { - format = matches[2] - address = matches[4] - - continue - } - - if matches := reDuration.FindStringSubmatch(line.Data); matches != nil { - duration = 0.0 - - // hours - if x, err := strconv.ParseFloat(matches[1], 64); err == nil { - duration += x * 60 * 60 - } - - // minutes - if x, err := strconv.ParseFloat(matches[2], 64); err == nil { - duration += x * 60 - } - - // seconds - if x, err := strconv.ParseFloat(matches[3], 64); err == nil { - duration += x - } - - // fractions - if x, err := strconv.ParseFloat(matches[4], 64); err == nil { - duration += x / 100 - } - - continue - } - - if matches := reStream.FindStringSubmatch(line.Data); matches != nil { - io := probeIO{} - - io.Address = address - io.Format = format - io.Duration = duration - - if x, err := strconv.ParseUint(matches[1], 10, 64); err == nil { - io.Index = x - } - - if x, err := strconv.ParseUint(matches[2], 10, 64); err == nil { - io.Stream = x - } - - io.Language = "und" - if len(matches[3]) == 3 { - io.Language = matches[3] - } - - io.Type = strings.ToLower(matches[4]) - - streamDetail := matches[5] - - if matches = reStreamCodec.FindStringSubmatch(streamDetail); matches != nil { - io.Codec = matches[1] - } - - if matches = reStreamBitrate.FindStringSubmatch(streamDetail); matches != nil { - if x, err := strconv.ParseFloat(matches[1], 64); err == nil { - io.Bitrate = x - } - } - - if io.Type == "video" { - if matches = reStreamVideoPixfmtSize.FindStringSubmatch(streamDetail); matches != nil { - io.Pixfmt = matches[1] - - if x, err := strconv.ParseUint(matches[3], 10, 64); err == nil { - io.Width = x - } - if x, err := strconv.ParseUint(matches[4], 10, 64); err == nil { - io.Height = x - } - } - - if matches = reStreamVideoFPS.FindStringSubmatch(streamDetail); matches != nil { - if x, err := strconv.ParseFloat(matches[1], 64); err == nil { - io.FPS = x - } - } - - } else if io.Type == "audio" { - if matches = reStreamAudio.FindStringSubmatch(streamDetail); matches != nil { - if x, err := strconv.ParseUint(matches[1], 10, 64); err == nil { - io.Sampling = x - } - - io.Layout = matches[2] - } - } - - p.inputs = append(p.inputs, io) - } + for i, input := range inputs { + p.inputs[i].Address = input.Address + p.inputs[i].Format = input.Format + p.inputs[i].Duration = input.Duration + p.inputs[i].Index = input.Index + p.inputs[i].Stream = input.Stream + p.inputs[i].Language = input.Language + p.inputs[i].Type = input.Type + p.inputs[i].Codec = input.Codec + p.inputs[i].Bitrate = input.Bitrate + p.inputs[i].Pixfmt = input.Pixfmt + p.inputs[i].Width = input.Width + p.inputs[i].Height = input.Height + p.inputs[i].FPS = input.FPS + p.inputs[i].Sampling = input.Sampling + p.inputs[i].Layout = input.Layout } } diff --git a/ffmpeg/probe/prober_test.go b/ffmpeg/probe/prober_test.go index 60056be1..6ef72171 100644 --- a/ffmpeg/probe/prober_test.go +++ b/ffmpeg/probe/prober_test.go @@ -3,6 +3,8 @@ package probe import ( "strings" "testing" + + "github.com/stretchr/testify/require" ) func TestProber(t *testing.T) { @@ -38,6 +40,14 @@ Input #3, mov,mp4,m4a,3gp,3g2,mj2, from 'movie.mp4': Duration: 00:01:02.28, start: 0.000000, bitrate: 5895 kb/s Stream #3:0(eng): Video: h264 (Main) (avc1 / 0x31637661), yuvj420p(pc, bt709), 2560x1440 [SAR 1:1 DAR 16:9], 5894 kb/s, 23.98 fps, 25 tbr, 90k tbn, 50 tbc (default) Stream #3:1(por): Subtitle: subrip +Input #4, mpegts, from 'srt://localhost:6000?mode=caller&transtype=live&streamid=#!:m=request,r=ingest/ad045490-8233-4f31-a296-ea5771a340ac&passphrase=foobarfoobar': + Duration: N/A, start: 71.786667, bitrate: N/A + Program 1 + Metadata: + service_name : Service01 + service_provider: FFmpeg + Stream #4:0[0x100]: Video: h264 (Main) ([27][0][0][0] / 0x001B), yuv420p(tv, smpte170m/bt709/bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], 25 tbr, 90k tbn + Stream #4:1[0x101]: Audio: aac (LC) ([15][0][0][0] / 0x000F), 48000 Hz, stereo, fltp, 162 kb/s Stream mapping: Stream #0:0 -> #0:0 (rawvideo (native) -> h264 (libx264)) Stream #1:0 -> #0:1 (pcm_u8 (native) -> aac (native)) @@ -51,230 +61,107 @@ Press [q] to stop, [?] for help` prober.ResetStats() - if len(prober.inputs) != 5 { - t.Errorf("#inputs: want=5, have=%d\n", len(prober.inputs)) - return - } + require.Equal(t, 7, len(prober.inputs)) i := prober.inputs[0] - if i.Address != "testsrc=size=1280x720:rate=25" { - t.Errorf("#input0.address: want=testsrc=size=1280x720:rate=25, have=%s\n", i.Address) - } - - if i.Format != "lavfi" { - t.Errorf("#input0.format: want=lavfi, have=%s\n", i.Format) - } - - if i.Index != 0 { - t.Errorf("#input0.index: want=0, have=%d\n", i.Index) - } - - if i.Stream != 0 { - t.Errorf("#input0.stream: want=0, have=%d\n", i.Stream) - } - - if i.Language != "und" { - t.Errorf("#input0.language: want=und, have=%s\n", i.Language) - } - - if i.Type != "video" { - t.Errorf("#input0.type: want=video, have=%s\n", i.Type) - } - - if i.Codec != "rawvideo" { - t.Errorf("#input0.codec: want=rawvideo, have=%s\n", i.Codec) - } - - if i.Bitrate != 0 { - t.Errorf("#input0.bitrate: want=0, have=%f\n", i.Bitrate) - } - - if i.Duration != 0 { - t.Errorf("#input0.duration: want=0, have=%f\n", i.Duration) - } - - if i.FPS != 0 { - t.Errorf("#input0.fps: want=0, have=%f\n", i.FPS) - } - - if i.Pixfmt != "rgb24" { - t.Errorf("#input0.pixfmt: want=rgb24, have=%s\n", i.Pixfmt) - } - - if i.Width != 1280 { - t.Errorf("#input0.width: want=1280, have=%d\n", i.Width) - } - - if i.Height != 720 { - t.Errorf("#input0.height: want=720, have=%d\n", i.Height) - } + require.Equal(t, "testsrc=size=1280x720:rate=25", i.Address) + require.Equal(t, "lavfi", i.Format) + require.Equal(t, uint64(0), i.Index) + require.Equal(t, uint64(0), i.Stream) + require.Equal(t, "und", i.Language) + require.Equal(t, "video", i.Type) + require.Equal(t, "rawvideo", i.Codec) + require.Equal(t, 0.0, i.Bitrate) + require.Equal(t, 0.0, i.Duration) + require.Equal(t, 0.0, i.FPS) + require.Equal(t, "rgb24", i.Pixfmt) + require.Equal(t, uint64(1280), i.Width) + require.Equal(t, uint64(720), i.Height) i = prober.inputs[1] - if i.Address != "anullsrc=r=44100:cl=stereo" { - t.Errorf("#input1.address: want=anullsrc=r=44100:cl=stereo, have=%s\n", i.Address) - } - - if i.Format != "lavfi" { - t.Errorf("#input1.format: want=lavfi, have=%s\n", i.Format) - } - - if i.Index != 1 { - t.Errorf("#input1.index: want=1, have=%d\n", i.Index) - } - - if i.Stream != 0 { - t.Errorf("#input1.stream: want=0, have=%d\n", i.Stream) - } - - if i.Language != "und" { - t.Errorf("#input1.language: want=und, have=%s\n", i.Language) - } - - if i.Type != "audio" { - t.Errorf("#input1.type: want=audio, have=%s\n", i.Type) - } - - if i.Codec != "pcm_u8" { - t.Errorf("#input1.codec: want=pcm_u8, have=%s\n", i.Codec) - } - - if i.Bitrate != 705 { - t.Errorf("#input1.bitrate: want=705, have=%f\n", i.Bitrate) - } - - if i.Duration != 0 { - t.Errorf("#input1.duration: want=0, have=%f\n", i.Duration) - } - - if i.Sampling != 44100 { - t.Errorf("#input1.sampling: want=44100, have=%d\n", i.Sampling) - } - - if i.Layout != "stereo" { - t.Errorf("#input1.layout: want=stereo, have=%s\n", i.Layout) - } + require.Equal(t, "anullsrc=r=44100:cl=stereo", i.Address) + require.Equal(t, "lavfi", i.Format) + require.Equal(t, uint64(1), i.Index) + require.Equal(t, uint64(0), i.Stream) + require.Equal(t, "und", i.Language) + require.Equal(t, "audio", i.Type) + require.Equal(t, "pcm_u8", i.Codec) + require.Equal(t, 705.0, i.Bitrate) + require.Equal(t, 0.0, i.Duration) + require.Equal(t, uint64(44100), i.Sampling) + require.Equal(t, "stereo", i.Layout) i = prober.inputs[2] - if i.Address != "playout:rtmp://l5gn74l5-vpu.livespotting.com/live/0chl6hu7_360?token=m5ZuiCQYRlIon8" { - t.Errorf("#input2.address: want=playout:rtmp://l5gn74l5-vpu.livespotting.com/live/0chl6hu7_360?token=m5ZuiCQYRlIon8, have=%s\n", i.Address) - } - - if i.Format != "playout" { - t.Errorf("#input2.format: want=playout, have=%s\n", i.Format) - } - - if i.Index != 2 { - t.Errorf("#input2.index: want=2, have=%d\n", i.Index) - } - - if i.Stream != 0 { - t.Errorf("#input2.stream: want=0, have=%d\n", i.Stream) - } - - if i.Language != "und" { - t.Errorf("#input2.language: want=und, have=%s\n", i.Language) - } - - if i.Type != "video" { - t.Errorf("#input2.type: want=video, have=%s\n", i.Type) - } - - if i.Codec != "h264" { - t.Errorf("#input2.codec: want=h264, have=%s\n", i.Codec) - } - - if i.Bitrate != 265 { - t.Errorf("#input2.bitrate: want=265, have=%f\n", i.Bitrate) - } - - if i.Duration != 0 { - t.Errorf("#input2.duration: want=0, have=%f\n", i.Duration) - } - - if i.FPS != 10 { - t.Errorf("#input2.fps: want=10, have=%f\n", i.FPS) - } - - if i.Pixfmt != "yuvj420p" { - t.Errorf("#input2.pixfmt: want=yuvj420p, have=%s\n", i.Pixfmt) - } - - if i.Width != 640 { - t.Errorf("#input2.width: want=640, have=%d\n", i.Width) - } - - if i.Height != 360 { - t.Errorf("#input2.height: want=360, have=%d\n", i.Height) - } + require.Equal(t, "playout:rtmp://l5gn74l5-vpu.livespotting.com/live/0chl6hu7_360?token=m5ZuiCQYRlIon8", i.Address) + require.Equal(t, "playout", i.Format) + require.Equal(t, uint64(2), i.Index) + require.Equal(t, uint64(0), i.Stream) + require.Equal(t, "und", i.Language) + require.Equal(t, "video", i.Type) + require.Equal(t, "h264", i.Codec) + require.Equal(t, 265.0, i.Bitrate) + require.Equal(t, 0.0, i.Duration) + require.Equal(t, 10.0, i.FPS) + require.Equal(t, "yuvj420p", i.Pixfmt) + require.Equal(t, uint64(640), i.Width) + require.Equal(t, uint64(360), i.Height) i = prober.inputs[3] - if i.Address != "movie.mp4" { - t.Errorf("#input3.address: want=movie.mp4, have=%s\n", i.Address) - } - - if i.Format != "mov,mp4,m4a,3gp,3g2,mj2" { - t.Errorf("#input3.format: want=mov,mp4,m4a,3gp,3g2,mj2, have=%s\n", i.Format) - } - - if i.Index != 3 { - t.Errorf("#input3.index: want=3, have=%d\n", i.Index) - } - - if i.Stream != 0 { - t.Errorf("#input3.stream: want=0, have=%d\n", i.Stream) - } - - if i.Language != "eng" { - t.Errorf("#input3.language: want=eng, have=%s\n", i.Language) - } - - if i.Type != "video" { - t.Errorf("#input3.type: want=video, have=%s\n", i.Type) - } - - if i.Codec != "h264" { - t.Errorf("#input3.codec: want=h264, have=%s\n", i.Codec) - } - - if i.Bitrate != 5894 { - t.Errorf("#input3.bitrate: want=5894, have=%f\n", i.Bitrate) - } - - if i.Duration != 62.28 { - t.Errorf("#input3.duration: want=62.82, have=%f\n", i.Duration) - } - - if i.FPS != 23.98 { - t.Errorf("#input3.fps: want=23.98, have=%f\n", i.FPS) - } - - if i.Pixfmt != "yuvj420p" { - t.Errorf("#input3.pixfmt: want=yuvj420p, have=%s\n", i.Pixfmt) - } - - if i.Width != 2560 { - t.Errorf("#input3.width: want=2560, have=%d\n", i.Width) - } - - if i.Height != 1440 { - t.Errorf("#input3.height: want=1440, have=%d\n", i.Height) - } + require.Equal(t, "movie.mp4", i.Address) + require.Equal(t, "mov,mp4,m4a,3gp,3g2,mj2", i.Format) + require.Equal(t, uint64(3), i.Index) + require.Equal(t, uint64(0), i.Stream) + require.Equal(t, "eng", i.Language) + require.Equal(t, "video", i.Type) + require.Equal(t, "h264", i.Codec) + require.Equal(t, 5894.0, i.Bitrate) + require.Equal(t, 62.28, i.Duration) + require.Equal(t, 23.98, i.FPS) + require.Equal(t, "yuvj420p", i.Pixfmt) + require.Equal(t, uint64(2560), i.Width) + require.Equal(t, uint64(1440), i.Height) i = prober.inputs[4] - if i.Language != "por" { - t.Errorf("#input4.language: want=por, have=%s\n", i.Language) - } + require.Equal(t, "movie.mp4", i.Address) + require.Equal(t, "mov,mp4,m4a,3gp,3g2,mj2", i.Format) + require.Equal(t, uint64(3), i.Index) + require.Equal(t, uint64(1), i.Stream) + require.Equal(t, "por", i.Language) + require.Equal(t, "subtitle", i.Type) + require.Equal(t, "subrip", i.Codec) - if i.Type != "subtitle" { - t.Errorf("#input4.type: want=subtitle, have=%s\n", i.Type) - } + i = prober.inputs[5] - if i.Codec != "subrip" { - t.Errorf("#input4.codec: want=subtip, have=%s\n", i.Codec) - } + require.Equal(t, "srt://localhost:6000?mode=caller&transtype=live&streamid=#!:m=request,r=ingest/ad045490-8233-4f31-a296-ea5771a340ac&passphrase=foobarfoobar", i.Address) + require.Equal(t, "mpegts", i.Format) + require.Equal(t, uint64(4), i.Index) + require.Equal(t, uint64(0), i.Stream) + require.Equal(t, "und", i.Language) + require.Equal(t, "video", i.Type) + require.Equal(t, "h264", i.Codec) + require.Equal(t, 0.0, i.Bitrate) + require.Equal(t, 0.0, i.Duration) + require.Equal(t, 0.0, i.FPS) + require.Equal(t, "yuv420p", i.Pixfmt) + require.Equal(t, uint64(1920), i.Width) + require.Equal(t, uint64(1080), i.Height) + + i = prober.inputs[6] + + require.Equal(t, "srt://localhost:6000?mode=caller&transtype=live&streamid=#!:m=request,r=ingest/ad045490-8233-4f31-a296-ea5771a340ac&passphrase=foobarfoobar", i.Address) + require.Equal(t, "mpegts", i.Format) + require.Equal(t, uint64(4), i.Index) + require.Equal(t, uint64(1), i.Stream) + require.Equal(t, "und", i.Language) + require.Equal(t, "audio", i.Type) + require.Equal(t, "aac", i.Codec) + require.Equal(t, 162.0, i.Bitrate) + require.Equal(t, 0.0, i.Duration) + require.Equal(t, uint64(48000), i.Sampling) + require.Equal(t, "stereo", i.Layout) }