9

I am reading this article. There is a statement there that goes:

ffmpeg -f alsa -ac 2 -i hw:0,0 -f x11grab -s $(xwininfo -root | grep 'geometry' | awk '{print $2;}') -r 25 -i :0.0 -sameq -f mpeg -ar 48000 -s wvga -y sample.mp4 

When I run the command I get an error with the section that says:

xwininfo -root | grep 'geometry' | awk '{print $2;}' 

The reason is that when you use this command on my computer it outputs:

1360x768+0+0 

How do I remove the last part of the screen resolution output to be 1360x768 instead of 1360x768+0+0?

Braiam
  • 69,112

5 Answers5

10
  • You can use perl to get only the resolution:

    xwininfo -root | perl -lne 's/.*geometry (\w+).*/$1/ or next; print'
    1360x768
    
  • Or even shorter with just GNU grep:

    xwininfo -root | grep -oP '(?<=geometry )\w+'
    1360x768
    

    Explanation: The lookbehind (?<=geometry ) asserts that at the current position in the string, what precedes is the characters "geometry ". If the assertion succeeds, the engine matches the resolution pattern.

    A lookbehind does not "consume" any characters on the string. This means that after the closing parenthesis, the regex engine is left standing on the very same spot in the string from which it started looking: it hasn't moved. From that position, then engine can start matching characters again.

    Source: http://www.rexegg.com/regex-disambiguation.html#lookbehind

6

My pure awk approach, splitting the string into fields based on spaces and plus signs:

xwininfo -root | awk -F'[+| ]' '/geometry/ {print $4}'

A similar method to Sylvain's Perl expression but with sed:

xwininfo -root | sed -nr 's/.*geometry ([0-9x]+).*/\1/p'
Oli
  • 299,380
5

You could maybe use awk substr. In your particular case :

xwininfo -root | awk '/geometry/ {print substr($2,1,8);}'

Or if you want it to work in any case :

xwininfo -root | awk '/geometry/ {print $2;}' | cut -d'+' -f1

hope this help.

Oli
  • 299,380
Relic
  • 121
2

Others have already provided answers for the complete operation, but to answer only How do I get from 1360x768+0+0 to 1360x768? then I would recomment using cut as the simplest possible solution, e.g.

$ echo 1360x768+0+0 | cut -d+ -f1
1360x768
$
hlovdal
  • 171
1

I also tried command line screen capture examples a few weeks ago. As an alternative solution you can use

 xdpyinfo  | grep dimensions | awk -F ' ' ' { print $2 } ' 

for detecting screen resolution.

To capture screen with internal audio and microphone, you can use

 avconv -f   pulse -i default -f x11grab -r 15 -s $(xdpyinfo  | grep dimensions | awk -F ' ' ' { print $2 } ' ) -i :0.0+0,0 -acodec libmp3lame -vcodec libx264  $(date +"%m%d%Y_%H%M%S_$HOSTNAME")_screencast.mp4
kenn
  • 5,232