2

(GNOME2, Ubuntu 10.04 LTS) I've made a nautilus script, so that if I have a directory full of various codecs, then I just need to right click in that folder -> Scripts -> THISSCRIPT.txt, then presto, it recursively converts all the video files (identified by video mimetype) to x.264 codec with 128 Kbit mp3 to avi. So that they will have small size+good quality. This works, great!

QUESTION: If I press "Cancel" on the Zenity progress bar, then the mencoder doesn't terminates.. How can I do this? I mean I need that If I press "Cancel" on the Zenity progress bar, it would terminate mencoder. How to do this?

#!/bin/bash

which mencoder > /dev/null 2>&1; if [[ $? -ne 0 ]]; then echo -e '\nerror, no mencoder package detected'; exit 1; fi
which zenity > /dev/null 2>&1; if [[ $? -ne 0 ]]; then echo -e '\nerror, no zenity package detected'; exit 1; fi

HOWMANYLEFT=0
find . -type f | xargs -I {} file --mime-type {} | fgrep "video/" | rev | awk 'BEGIN {FS="/oediv :"} { print $NF}' | rev | while read ONELINE
    do
    if file "$ONELINE" | egrep -qvi "x.264|h.264"
        then echo $ONELINE
    fi
done | sed 's/^.\///' | tee /tmp/vid-conv-tmp.txt | while read ONELINE
    do
    HOWMANY=`wc -l /tmp/vid-conv-tmp.txt | cut -d " " -f1`
    mencoder "$ONELINE" -o "OK-$ONELINE.avi" -ovc x264 -x264encopts bitrate=750 nr=2000 -oac mp3lame -lameopts cbr:br=128 > /dev/null 2>&1
    HOWMANYLEFT=`expr $HOWMANYLEFT + 1`
    echo "scale=10;($HOWMANYLEFT / $HOWMANY) * 100" | bc | cut -d "." -f1
done | zenity --progress --text="Processing files ..." --auto-close --percentage=0
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
gasko peter
  • 5,434
  • 22
  • 83
  • 145

1 Answers1

2

You need to use the --auto-kill option.. I've re-vamped the script a bit (I like your thinking-out-of-the-square use of rev, but there are other ways :) ...Here is one.

I've used yad instead of zenity. It is a fork of zenity, and the commands are basically the same. From what I've read, yad is being more actively developed and has more features (and this was a good chance for me to play with it). The --auto-kill option works with both zenity and yad.

As well as showing the percentage, the script shows a count of so-many (eg. 3 of 8) plus the current file's name. The percentage calculation uses awk(only because I'm comfortable with its syntax) ..

Re your specific question, just --auto-kill should be enough.

for p in mencoder yad ;do
    which $p >/dev/null 2>&1 || { echo -e '\nerror, no $p package detected'; exit 1; }
done

list="$(mktemp)"
find . -type f -print0 |   # -print0 caters for any filename 
  xargs --null file --print0 --mime-type | 
    sed -n 's|\x00 *video/.*|\x00|p' | tr -d $'\n' |
      xargs --null file --print0 |
        sed -nr '/\x00.*(x.264|h.264)/!{s/^\.\///; s/\x00.*//; p}' >"$list"
        # At this point, to count how many files there are to process, break out of the pipe.
        # You can't know how many there are until they have all passed through the pipe.

fct=0; wcfct=($(wc "$list"));
while IFS= read -r file ;do
    ((fct+=1)); pcnt=$(awk -v"OFMT=%.2f" "BEGIN{ print (($fct-1)/$wcfct)*100 }")
    echo "# $pcnt%: $fct of $wcfct: $file"; echo $pcnt
    mencoder "$file" -o "OK-$file.avi" -ovc x264 -x264encopts bitrate=750 nr=2000 -oac mp3lame -lameopts cbr:br=128 >/dev/null 2>&1
done <"$list" | yad --title="Encoding Progress" --progress --geometry +100+100 --auto-close --auto-kill 
rm "$list"
Peter.O
  • 32,426
  • 28
  • 115
  • 163
  • wow, ty for the script update. :) I have only 1 problem left: the --auto-kill doesn't work with zenity. can you help on that? :\ – gasko peter Jul 03 '12 at 00:34
  • putting a: nohup $(while true; do sleep 3 && if ! ps -ef | fgrep -iw "zenity --progress --text=Processing files ... --auto-close --percentage=0 --auto-kill" | grep -v fgrep > /dev/null 2>&1; then pkill -9 mencoder; pkill -9 archive; exit 1; fi; done) & did the trick – gasko peter Jul 03 '12 at 02:13
  • ...oops. I tested using `sleep 5` rather than a full encode, `sleep` exits immediately, whereas the encoding continues until that particular file copletes. I must have a good look at what you've done. I'm glad you got it working. – Peter.O Jul 03 '12 at 04:42