2

I'm running a command from within terminal that goes through a directory checking for media files and then extracts closed captioning data from those files. Unfortunately, even if there is no closed captioning data present, the program still processes the entire file and this can take a long time. What I would like to be able to do is check the output after 60 seconds and look for data, and if the file is empty, terminate the process and move on to the next file.

My old command is as follows

for i in */*.vob
do
     /home/me/ccextractor/linux/ccextractor -out=srt -utf8 -trim "$i"
done

I've been experimenting with sleep but I can't seem to get it working. Any suggestions?


SOLUTION

With help from the answers below (take note of the comments as well), my final working code is:

for i in */*.vob
do
    /home/me/ccextractor/linux/ccextractor -out=srt -utf8 -trim "$i" &
    pid=$!
    sleep 15
    srtfile=$(expr "${i}" | sed -r 's/.{4}$//')
    fgrep -q "1" "${srtfile}.srt" || kill $pid
    wait $pid
done
jeffers.R
  • 23
  • 3
  • Maybe looking for `timeout` instead of `sleep`, see [this](http://stackoverflow.com/q/5161193/3076724). Also, consider re-writing your one-liner as a regular bash script to improve readability of the question. – Reinstate Monica Please Mar 23 '14 at 02:45
  • Thanks BroSlow for the suggestions. I also updated the formatting to help others read the question and solution in the future as well. – jeffers.R Mar 23 '14 at 04:50

2 Answers2

3

You can have CCExtractor exit after processing one minute of video, there's not need to kill the process or control it separately.

ccextractor -endat 1:00 [etc]

will do what you want.

devnull
  • 118,548
  • 33
  • 236
  • 227
cfsmp3
  • 31
  • 2
  • Thanks for the input! The catch is, I don't want ccextractor to quit if there is caption data being extracted. I only want it to quit when it's processing a file even though there isn't any captioning data present. That's why I need to check and see if the srt file is empty or not after a certain amount of time. If it's not empty, I let it continue until the file is done. – jeffers.R Mar 23 '14 at 20:31
  • jeffers.R you can process the first minute, check the size of the generated file and if it's non-zero process the file completely. – cfsmp3 Mar 24 '14 at 06:34
1

Assuming that ccextractor works fine in the background (doesn't require input or a tty), then try:

for i in */*.vob
do
    /home/me/ccextractor/linux/ccextractor -out=srt -utf8 -trim "$i" &
    pid=$!
    sleep 60
    [ -s "${i}_1" ] || kill $pid
    wait $pid
done

where I have also assumed that the output file that ccextractor creates has _1 appended to the name of the input file.

[ -s "${i}_1" ] tests to see if the output file exists and has size greater than zero. If that is false, then the "or" condition is run and the process is killed.

wait $pid causes the shell to wait for one ccextractor to exit before starting another. If you want to run then all in parallel, remove this line.

John1024
  • 109,961
  • 14
  • 137
  • 171
  • This helped out tremendously! Thank you so much. I couldn't get `-s` to work for some reason, so I switch that up and used `fgrep` to search for a known character that would be present if the file was being populated. I also had to do a little work to provide the correct file path to `fgrep` (or `-s`) since the ccextractor program replaces the existing extension with a new one. – jeffers.R Mar 23 '14 at 04:35