When I run the below script mplayer
gives the below error and doesn't continue the while loop after pressing return. if I replace mplayer
with mpv
it works. I have to use mplayer, as mpv
doesn't support the video formats I need to play.
I suspect the problem is related to file descriptor redirection, but I had to do that to get user input to work in a while loop with piped data from find
.
Purpose
The goal of the script is I want to play each file in a directory, and after watching the video it should prompt me for what to do with the file. E.g. delete, rename, move.
Question
Can anyone see what the problem is, or perhaps how to work around it?
#!/usr/bin/bash
exec 3<&0 # redirect std input to a different file descriptor
find . -maxdepth 1 -type f -name "*" | while read f; do
mplayer -really-quiet -msglevel all=-1 "$f"
echo $f
printf "What to do: "
IFS= read -r -u3 opt
done
exec 3<&- # Close fd3
The error is
do_connect: could not connect to socket
connect: No such file or directory
Failed to open VDPAU backend libvdpau_va_gl.so: cannot open shared object file: No such file or directory
Assertion 'm' failed at pulse/thread-mainloop.c:175, function pa_threaded_mainloop_lock(). Aborting.
Changed to answer from comment, shell syntax to loop over files in current directory is just :
for f in *; do
[[ -f $f ]] || continue
... do something with "$f"
done
This will work with any character in filenames including spaces.
The -f
test is to ensure $f is a file (otherwise continue).
This handles also the case where there is no file : glob is not expanded and "$f"
is "*"
.
The issue with mplayer may be due to the pipe writing to standard input of while .. done
as to mplayer input is inherited from caller if mplayer is consuming whole input the caller will have an empty input. another fix could be to explicitly redirect input inside while loop exec 0</dev/null
or just for mplayer's command mplayer .. </dev/null
. or just exec 0</dev/tty
and in this last case exec 3<&0
/read -r -u3 opt
is no more needed just read -r opt