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
  • 66,947
  • 30
  • 177
  • 264
Guest123ABC
  • 171
  • 2
  • 3
  • 11

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

Sylvain Pineau
  • 61,564
  • 18
  • 149
  • 183
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
  • 289,791
  • 117
  • 680
  • 835
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
  • 289,791
  • 117
  • 680
  • 835
Relic
  • 121
  • 4
  • Oh yes! Thank you very much! The first one worked! :) The second one produces two results: a 1 and then the resolution which would not work in this case, but still a cool statement for general knowledge. – Guest123ABC Sep 03 '14 at 18:13
  • 2
    you should use the answer from sylvain. it's nicer. – Relic Sep 03 '14 at 18:19
  • 1
    I've moved the grep portion into the awk command (because I can't help myself). The problem with substr is it uses fixed placements. Resolutions like 1000x1000 will break it because they're too long. – Oli Sep 03 '14 at 21:30
  • @Oli wow nice move. i'll not forget about that grep replacement. Thanks for the tips. You totally right about the substr, this is why i added the second command. – Relic Sep 04 '14 at 06:08
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
  • 3
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,074
  • 12
  • 55
  • 94