Search code examples
bashffmpegterminalxdotool

How to correctly detect window with xdotool in bash script


I'm trying to record screen and also input from my webcam. To show image from a webcam I use ffplay. However I want it to be placed in a specific location of my screen. To do so I use xdotool and following bash script:

 #!/bin/bash                                                                                                                                                                                                                                   
 ffplay -i /dev/video0 &                                                                                                                                                                                       
 res=$!                                                                                                                  
 echo $res                                                                                                               
 window_pid=$(xdotool search --pid $res)                                                                                       
 echo $window_pid
 xdotool windowmove $window_pid 1200 200                                                                                                       
 wait

For some reason I get correct process id res but nothing for the window_pid. If I run similar commands in terminal it works correctly (I run ffplay in one terminal instance and the rest of commands in another). What am I missing here?


Solution

  • You can try this :

    #!/bin/bash                                                                                                                                                                                                                                   
    ffplay -i /dev/video0 &                                                                                                                                                                                       
    res=$!                                                                                                                  
    echo $res                                                                                                               
    until window_pid=$(xdotool search --pid $res); test -n "$window_pid"; do
        sleep .1
    done
    echo $window_pid
    xdotool windowmove $window_pid 1200 200                                                                                                       
    wait