1

I am trying to make a script what looks at a folder and will automatically encode files that go into that folder using hand brake. I want to do this doing monitoring the folder using inotify putting the new additions to the folder into a list then using a cron job to encode them overnight. However when using a while loop to loop over the list handbrake only encodes the first file exists then the scripts carrys on to after the loop without doing every file in the list. Here is the script that is calling handbrake:

#!/bin/bash

while IFS= read -r line
do
    echo "$(basename "$line")"
    HandBrakeCLI -Z "Very Fast 1080p30" -i "$line" -o "$line.m4v"
    rm "$line"
done < list.txt

> list.txt

When testing the loop with a simple echo instead of the HandBrakeCLI it works fine and prints out every file so I have no idea what is wrong.

Here is the scripts that is monitoring the folder incase that is the problem:

#!/bin/bash

if ! [ -f list.txt ]
then
    touch list.txt
fi

inotifywait -m -e create --format "%w%f" tv-shows | while read FILE
do
    echo "$FILE" >> list.txt
done

Any help would be great, thanks

EDIT: Just to be more specific, the script works fine for the first file in the list.txt, it encodes it no problem and removes the old version, but then it doesn't do any of the others in the list

Thomas Briggs
  • 119
  • 1
  • 11
  • You can run the first script using `bash -xv YourHandbrakeScriptName` to see all variables as each line is executed. – Mark Setchell Apr 11 '20 at 14:53
  • apart from the output from handbrake which I can post if it is needed I just output this: + rm 'tv-shows/Marvels The Avengers- Trailer (OFFICIAL).mp4' + IFS= + read -r line # > list.txt I also commented out the >list.txt for testing so I can keep running the encoder on the list.txt without having to retype it Not too sure what I am looking for. Just currently testing it with trailers – Thomas Briggs Apr 11 '20 at 15:00
  • 1
    Mmm, you could try adding a space followed by `< /dev/null` to the end of the line starting `HandbrakeCLI` in case it's grabbing the `stdin`. – Mark Setchell Apr 11 '20 at 15:09
  • Sadly that didn't work, I tried HandBrakeCLI .... < /dev/null and HandBrakeCLI .... > /dev/null but still nope, it still only does the first file in the list, not all of them – Thomas Briggs Apr 11 '20 at 15:17
  • Does it list all files if you comment out the `HandbrakeCLI` line? – Mark Setchell Apr 11 '20 at 15:19
  • Yeah so if I change the script and replace the HandBrakeCLI line with a echo $line then it prints out every line in the list.txt file fine – Thomas Briggs Apr 11 '20 at 15:21
  • It looks like Handbrake does grab `stdin`... https://github.com/HandBrake/HandBrake/issues/1316 which means it could `eat` your other input files. – Mark Setchell Apr 11 '20 at 15:27
  • 1
    Echoing nothing into HB may help... https://stackoverflow.com/a/17414265/2836621 – Mark Setchell Apr 11 '20 at 15:31
  • Yeah it now works perfect, thank you very much – Thomas Briggs Apr 11 '20 at 15:36
  • Cool! Maybe upvote linked answer from @gabkdlly Good luck with your project. – Mark Setchell Apr 11 '20 at 15:39
  • 1
    It shouldn't be possible for `echo "" | ...` to work when `... < /dev/null` fails. Can you please double check? – that other guy Apr 11 '20 at 16:16
  • Can do, the exact command I did was HandBrakeCLI -Z "Very Fast 1080p30" -i "$line" -o "$line.m4v" < /dev/null is that the correct way? – Thomas Briggs Apr 11 '20 at 16:23
  • Yes, if that's all a single line, it should have the same effect – that other guy Apr 11 '20 at 16:31
  • Okay sorry to @mark-setchell for wasting your time, I must have made a mistake when typing in < /dev/null or not saved the file because just re checked and doing that did solve the problem, not too sure where I went wrong. The < /dev/null is nw working so I'll use that instead thanks – Thomas Briggs Apr 11 '20 at 16:32
  • For your convenience, [ShellCheck](https://www.shellcheck.net) also automatically suggests this – that other guy Apr 11 '20 at 16:38
  • Thanks for suggesting that, I will keep that tool in mind for future problems. I am new bash and sh scripting in general at the moment – Thomas Briggs Apr 11 '20 at 16:42

1 Answers1

0

Taken from here

To solve the problem simply

echo "" | HandBrakeCLI ......

or

HandBrakeCLI ...... < /dev/null
Thomas Briggs
  • 119
  • 1
  • 11