Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying to batch convert a bunch of files and i am doing an simple listing of the converted files to a text document so that i can stop the script and continue from approximately were i started.

#! /bin/sh
source_dir="/home/eldamar/video"

find $source_dir -type f -regex ".*/.*\.\(mov\|mpg\|mkv\|avi\|m2v\|wmv\|flv\|m2ts\|vob\)" | {
while read file
  do
  ext=${file##*.}
  filename=${file##*/}
  basename=${filename%.*}
  dirname=${file%/*}
  touch converted.txt

  if grep -Fxq "$filename" converted.txt
  then
    echo "$(tput setaf 2)File "$filename" is allready converted, ignoring it :D$(tput setaf 7)"
  else
    # Extract subtitles for mkv files
    echo $ext
    if [ "$ext"=="mkv" ]; then
      # input.srt <- default subtitle
      # input.lang.srt <- other languages
      $sublang=""
      mkvinfo=$(mkvinfo $file | grep subtitles -B3 -A3)

      # Replace existing .srt
      # rm $dirname/$basename.srt

      # Extract subtitle from mkv
      # mkvextract tracks input.mkv -c ISO8859-1 3:$dirname/$basename.$sublang.srt
    fi

    avconv -y -i $file -map 0 -map -0:s -vcodec libx264 -acodec libfaac $dirname/$basename.mp4

    # Store the filename in list
    echo $filename >> converted.txt
  fi
  done;
}

There you go, subtitle extraction is also in progress but i am having trouble with knowing if avconv fails or not, is there a way?

Edit Somewhat done with this script now, here it is http://pastebin.com/trMDRaq5 thanks for the help!

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Most commands return a non-zero return value if an error occurred. You can either capture that return code or act on it with via || and &&.

Examples:

avconv -y -i file1 ; echo command finished

avconv -y -i file1 && echo command finished successfully.

avconv -y -i file1 || echo command finished but indicated failure!

[Edit]

You can also check $? in the shell. example:

#!/bin/sh
/usr/bin/true
echo $?
/usr/bin/false
echo $?

True always succeeds. If you run the test script it will return a 0.
False always fails. If you run the test script it will return a non-0. (in my case a 1, but that can vary).

share|improve this answer
    
Hey that worked! –  eldamar Feb 3 '13 at 15:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.