Skip to content

Commit

Permalink
Use regexp to filter out context and loglevel
Browse files Browse the repository at this point in the history
  • Loading branch information
ioppermann committed Jan 14, 2025
1 parent 3bf461f commit 2a787c4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
39 changes: 19 additions & 20 deletions ffmpeg/parse/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Config struct {

type parser struct {
re struct {
logline *regexp.Regexp
frame *regexp.Regexp
quantizer *regexp.Regexp
size *regexp.Regexp
Expand Down Expand Up @@ -147,6 +148,7 @@ func New(config Config) Parser {
p.averager.window = 30 * time.Second
p.averager.granularity = time.Second

p.re.logline = regexp.MustCompile(`^(?:\[.*? @ .*?\] )?(?:\[[a-z]+\] )?(.*)`)
p.re.frame = regexp.MustCompile(`frame=\s*([0-9]+)`)
p.re.quantizer = regexp.MustCompile(`q=\s*([0-9\.]+)`)
p.re.size = regexp.MustCompile(`size=\s*([0-9]+)kB`)
Expand Down Expand Up @@ -201,13 +203,16 @@ func New(config Config) Parser {
}

func (p *parser) Parse(line []byte) uint64 {
isDefaultProgress := bytes.HasPrefix(line, []byte("frame="))
isFFmpegInputs := bytes.Contains(line, []byte("ffmpeg.inputs:"))
isFFmpegOutputs := bytes.Contains(line, []byte("ffmpeg.outputs:"))
isFFmpegMapping := bytes.Contains(line, []byte("ffmpeg.mapping:"))
isFFmpegProgress := bytes.Contains(line, []byte("ffmpeg.progress:"))
isHLSStreamMap := bytes.Contains(line, []byte("hls.streammap:"))
isAVstreamProgress := bytes.Contains(line, []byte("avstream.progress:"))
matches := p.re.logline.FindSubmatch(line)
msg := matches[1]

isDefaultProgress := bytes.HasPrefix(msg, []byte("frame="))
isFFmpegInputs := bytes.HasPrefix(msg, []byte("ffmpeg.inputs:"))
isFFmpegOutputs := bytes.HasPrefix(msg, []byte("ffmpeg.outputs:"))
isFFmpegMapping := bytes.HasPrefix(msg, []byte("ffmpeg.mapping:"))
isFFmpegProgress := bytes.HasPrefix(msg, []byte("ffmpeg.progress:"))
isHLSStreamMap := bytes.HasPrefix(msg, []byte("hls.streammap:"))
isAVstreamProgress := bytes.HasPrefix(msg, []byte("avstream.progress:"))

p.lock.log.Lock()
if p.logStart.IsZero() {
Expand Down Expand Up @@ -238,8 +243,7 @@ func (p *parser) Parse(line []byte) uint64 {
p.lock.progress.Lock()
defer p.lock.progress.Unlock()

_, line, _ = bytes.Cut(line, []byte("ffmpeg.inputs:"))
if err := p.parseFFmpegIO("input", line); err != nil {
if err := p.parseFFmpegIO("input", bytes.TrimPrefix(msg, []byte("ffmpeg.inputs:"))); err != nil {
p.logger.WithFields(log.Fields{
"line": line,
"error": err,
Expand All @@ -253,8 +257,7 @@ func (p *parser) Parse(line []byte) uint64 {
p.lock.progress.Lock()
defer p.lock.progress.Unlock()

_, line, _ = bytes.Cut(line, []byte("hls.streammap:"))
if err := p.parseHLSStreamMap(line); err != nil {
if err := p.parseHLSStreamMap(bytes.TrimPrefix(msg, []byte("hls.streammap:"))); err != nil {
p.logger.WithFields(log.Fields{
"line": line,
"error": err,
Expand All @@ -268,8 +271,7 @@ func (p *parser) Parse(line []byte) uint64 {
p.lock.progress.Lock()
defer p.lock.progress.Unlock()

_, line, _ = bytes.Cut(line, []byte("ffmpeg.outputs:"))
if err := p.parseFFmpegIO("output", line); err != nil {
if err := p.parseFFmpegIO("output", bytes.TrimPrefix(msg, []byte("ffmpeg.outputs:"))); err != nil {
p.logger.WithFields(log.Fields{
"line": line,
"error": err,
Expand Down Expand Up @@ -302,8 +304,7 @@ func (p *parser) Parse(line []byte) uint64 {
p.lock.progress.Lock()
defer p.lock.progress.Unlock()

_, line, _ = bytes.Cut(line, []byte("ffmpeg.mapping:"))
if err := p.parseFFmpegMapping(line); err != nil {
if err := p.parseFFmpegMapping(bytes.TrimPrefix(msg, []byte("ffmpeg.mapping:"))); err != nil {
p.logger.WithFields(log.Fields{
"line": line,
"error": err,
Expand Down Expand Up @@ -380,8 +381,7 @@ func (p *parser) Parse(line []byte) uint64 {
// Update the progress

if isAVstreamProgress {
_, line, _ = bytes.Cut(line, []byte("avstream.progress:"))
if err := p.parseAVstreamProgress(line); err != nil {
if err := p.parseAVstreamProgress(bytes.TrimPrefix(msg, []byte("avstream.progress:"))); err != nil {
p.logger.WithFields(log.Fields{
"line": line,
"error": err,
Expand All @@ -392,16 +392,15 @@ func (p *parser) Parse(line []byte) uint64 {
}

if isDefaultProgress {
if err := p.parseDefaultProgress(line); err != nil {
if err := p.parseDefaultProgress(msg); err != nil {
p.logger.WithFields(log.Fields{
"line": line,
"error": err,
}).Error().Log("Failed parsing default progress")
return 0
}
} else if isFFmpegProgress {
_, line, _ = bytes.Cut(line, []byte("ffmpeg.progress:"))
if err := p.parseFFmpegProgress(line); err != nil {
if err := p.parseFFmpegProgress(bytes.TrimPrefix(msg, []byte("ffmpeg.progress:"))); err != nil {
p.logger.WithFields(log.Fields{
"line": line,
"error": err,
Expand Down
10 changes: 7 additions & 3 deletions ffmpeg/prelude/prelude.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ type IO struct {
// 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) {
reLogline := regexp.MustCompile(`^(?:\[.*? @ .*?\] )?(?:\[[a-z]+\] )?(.*)`)
// 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(`^(?:\[[a-z]+\] )?(Input|Output) #([0-9]+), (.*?), (from|to) '([^']+)`)
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
Expand All @@ -56,8 +57,8 @@ func Parse(lines []string) (inputs, outputs []IO, noutputs int) {
reStreamAudio := regexp.MustCompile(`, ([0-9]+) Hz, ([^,]+)`)
reStreamBitrate := regexp.MustCompile(`, ([0-9]+) kb/s`)

reStreamMapping := regexp.MustCompile(`^(?:\[[a-z]+\] )?Stream mapping:`)
reStreamMap := regexp.MustCompile(`^(?:\[[a-z]+\])?[\s]+Stream #[0-9]+:[0-9]+`)
reStreamMapping := regexp.MustCompile(`^Stream mapping:`)
reStreamMap := regexp.MustCompile(`^[\s]+Stream #[0-9]+:[0-9]+`)

iotype := ""
format := ""
Expand All @@ -67,6 +68,9 @@ func Parse(lines []string) (inputs, outputs []IO, noutputs int) {
streamMapping := false

for _, line := range lines {
matches := reLogline.FindStringSubmatch(line)
line := matches[1]

if reStreamMapping.MatchString(line) {
streamMapping = true
continue
Expand Down

0 comments on commit 2a787c4

Please sign in to comment.