0

I got to this solution yesterday with help from here (it's finding a list of directories to process with HandBrakeCLI, and putting the results in a hard-coded directory):

#!/bin/bash
source_dir=/Volumes/VolumeName
dest_dir=/Volumes/OtherName

export dest_dir # export allows use by subprocesses!

find "$source_dir" -type d -name "VIDEO_TS" -exec bash -c '
  for dir; do
    name=${dir%/VIDEO_TS}
    name=${name##*/}
    ./HandBrakeCLI -i "$dir" -o "$dest_dir/$name.m4v"
  done
' _ {} +

The shell script works fine, but if I try to stop it by killing the main process:

kill -9 <pid>

it seems to work but then comes back to life and calls HandBrakeCLI with the next file in the list generated by find.

Is there a way of reliably stopping the whole thing? I'm assuming that somehow the future calls to the CLI are being queued/cached, but could someone explain what's happening please?

Lorccan
  • 793
  • 5
  • 20
  • I suggest to use `trap` command. See `help trap`. – Cyrus Jan 28 '16 at 19:46
  • Thanks. I can see the principle, but can't quite figure out the application here. If I put something like 'trap \'some code\' 2' at the top of the file, I don't think that's adding anything as I can already send ctrl-C to the main script. So, do I put it somewhere in the do...done loop and what should I do in the code part? – Lorccan Jan 28 '16 at 20:25

2 Answers2

0

Your script is made unnecessarily complex by invoking bash from find. I suggest iterating over the output of find.

#!/usr/bin/env bash

source_dir=/Volumes/VolumeName
dest_dir=/Volumes/OtherName

find "${source_dir}" -type d -name "VIDEO_TS" | while read dir; do
  name=${dir%/VIDEO_TS}
  name=${name##*/}
  ./HandBrakeCLI -i "${dir}" -o "${dest_dir}/${name}.m4v"
done
Jon Nalley
  • 511
  • 2
  • 8
  • Thanks for your suggestion. If I try this, I have two issues: i. with the curly brackets around the vars in the HandBrakeCLI call I get an error of 'bad substitution' and ii. if i remove these brackets the loop only runs once (even though there is more than one directory returned from find). – Lorccan Jan 30 '16 at 00:25
  • You could try debugging the script further by adding an 'echo' in front of the './HandBrakeCLI'. This will allow you to see exactly how the command will be called without actually running it. – Jon Nalley Jan 31 '16 at 00:55
  • As I said in my other post, there's an issue with ffmpeg consuming all the input. – Lorccan Jan 31 '16 at 01:16
0

I discovered that there's a problem with using find piped to while loop as suggested by @JonNailey with HandBrakeCLI (and other processes that call ffmpeg): it 'sucks up' all the input from the find command and starves the loop. See http://mywiki.wooledge.org/BashFAQ/089 for details.

The solution I chose was to put this redirection on the line calling HandBrakeCLI:

</dev/null 
Lorccan
  • 793
  • 5
  • 20