haeffnkr haeffnkr - 29 days ago 8
Linux Question

my linux bash script will not loop though all files

I am a noob and trying to build a post processing type script for Plex.
With help from some co workers and google I have put together the following script to convert mkv files to mp4 files.

The script is to "find" any and all files in the media directory and convert them to mp4 files.

It finds the first one, processes correctly and quits - why??
I need help getting this scrip to loop.
I have have the quirky find command written this way so the "find" command will will capture the file name with spaces and the dirname command will work.

Script ---

find -L "/media/4tbdisk/test/" -type f -name '*.mkv' -print0 | while IFS= read -r -d $'\0' FILE; do
echo "filename is ---" "$FILE";
DIR=$(dirname "${FILE}");
echo "directory is --" $DIR;
transcode-video --mp4 --quick "$FILE" --output "$DIR";
done


What happens is that the script will find an mkv file and process it but will not continue looping through to process the rest.
If the first file it finds already has been processed the "transcode" script will say "out put file exists..." and will continue on to the next, but after it creates the first mp4 file it stops

thanks in advance
haeffnkr

Answer

You appear to be using this transcode-video script. It's based on HandbrakeCLI and mplayer, both of which have a tendency to overconsume stdin and thereby breaking while read loops after the first iteration (ex1, ex2, ex3, ex4).

You can avoid this by redirecting input from /dev/null so it doesn't consume any of the loop's input:

find -L "/media/4tbdisk/test/" -type f -name '*.mkv' -print0 | while IFS=      read -r -d $'\0' FILE; do
echo "filename is ---" "$FILE";
DIR=$(dirname "${FILE}");
echo "directory is --" $DIR;
transcode-video < /dev/null --mp4 --quick "$FILE" --output "$DIR";
done
Comments