3

I am trying to pipe an output from avconv to grep but somehow the pipe is not working correctly. The following ways are not working:

avconv -i MOVIE.mkv | grep SOMETHING - Which should show only the lines from the avconv that have SOMETHING in them.

avconv -i MOVIE.mkv pipe: | grep SOMETHING - Which should show only the lines from the avconv that have SOMETHING in them.

avconv -i MOVIE.mkv pipe:1 | grep SOMETHING - Which should show only the lines from the avconv that have SOMETHING in them.

How can I output the information from avconv to pipe?

Why is it not working?

Piping something like this works, but not the -i information parameter: avconv -codecs | grep "264"

Tested on 12.04 and 12.10

2
  • Try using single or double quotes around SOMETHING.
    – nanofarad
    Sep 18, 2012 at 23:25
  • Actually the output with single quotes, double quotes or without quotes is the same. This are only used when what we are looking for has a space or a symbol not friendly with the naming conventions. Like the space and other symbols that need the quotes to tell grep to look for that specific value. Sep 18, 2012 at 23:28

1 Answer 1

6

I am one of the developers of Format Junkie, so I kind of know how avconv works, and I assure you that it sends all the important data to stderr. So, you need to grep from stderr. Actually, you redirect all the stderr to stdout and then grep:

For example:

avconv -i 111.avi 2>&1 | grep Duration

correctly outputs:

  Duration: 00:01:05.02, start: 0.000000, bitrate: 2910 kb/s

The reason why the grep works only in some of avconv's output is that this specific output is being given to stdout, not to stderr, and so it is successfully piped to grep.

Normally stderr is used so as to output error messages and stdout so as to output normal informative messages.

It is not bad that both stderr and stdout exist, because you can filter the output the way you want. For example, consider the following:

command > log.txt 2> error_log.txt

This will output all the normal output (stdout) to log.txt and all errors (stderr) to error_log.txt

I don't know why avconv specifically uses stderr to display its messages, though.

5
  • Awesome thanks hakermania. Just a suggestion but wouldn't it be better if all outputs were the same way, to stdout, like first parsed before outputting the info. Or is there a reason why in some cases it is stderr? Sep 19, 2012 at 0:21
  • 1
    please see my edited answer.
    – hytromo
    Sep 19, 2012 at 0:23
  • Got it thanks. Did not notice that both outputs were there. Sep 19, 2012 at 0:28
  • 1
    I think the reason avconv writes info to stderr is because it supports writing the output file to stdout. Thus, stdout needs to be reserved for file data.
    – Nate C-K
    Feb 6, 2019 at 11:38
  • You can use |& instead of 2>&1 |. Seems more easy to remember. Mar 4, 2019 at 20:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .