Advertisement
Xyberviri

iso2mkv.sh

Jun 26th, 2017
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2.  
  3. # License: Beerware 2017
  4. # Author: Xyberviri
  5. # Purpose: to make it easier to rip .iso files into .mkv files.
  6. # If you find this script useful you can buy me a beer here: https://www.paypal.me/xyberviri
  7. # wget https://pastebin.com/raw/SRHihAED -O- | tr -d '\r' >iso2mkv.sh;chmod +x iso2mkv.sh
  8.  
  9. # Usage info
  10. show_help() {
  11. cat << EOF
  12. Usage: ${0##*/} [-hv] [-s STARTING TITLE] [-e ENDING TITLE] [-o OUTFILE OFFSET] [-f OUTFILE] [FILE]...
  13.     Export titles from FILE to OUTFILE, FILE should be a .iso format.
  14.  
  15.     -h          display this help and exit
  16.     -f OUTFILE  name out output file, DEFAULTS to the input FILE.mkv
  17.     -s #        starting title, DEFAULT 1
  18.     -e #        ending title, DEFAULTs to last title
  19.     -o #        offset the exported title to this, useful when dealing
  20.                 with multi dvd seasons. This option also forces OUTFILE##.mkv behaviour.
  21.     -i          inspect the .iso and lists title index, lengths and total title counts.
  22.  
  23. ./iso2mkv.sh -e 4 -f SomeShowSeason1E  dvd.iso
  24. ..exports titles 1 - 4 to files SomeShowSeason1E{01-04}.mkv
  25.  
  26. ./iso2mkv.sh -s 5 -e 10 dvd.iso
  27. ..exports titles 5 - 10 to files  dvd{05-10}.mkv
  28.  
  29. ./iso2mkv.sh -e 10 -o 11 dvd2.iso
  30. ..exports titles 1 - 10 to files  dvd2{11-20}.mkv
  31.  
  32. ./iso2mkv.sh -e 1 dvd.iso
  33. ..exports titles 1 as dvd.mkv
  34.  
  35.  
  36. This script requires handbrake, install with the following:
  37.     add-apt-repository ppa:stebbins/handbrake-releases
  38.     apt-get update
  39.     apt-get install handbrake-cli
  40.  
  41. Customize the export on the very last line of the script
  42. Handbrake CLI here:
  43. https://handbrake.fr/docs/en/1.0.0/cli/cli-guide.html
  44.  
  45. EOF
  46. }
  47.  
  48. # Initialize our own variables:
  49. output_file=""
  50. verbose=0
  51. offset=0
  52. title_start=1
  53. title_end=0
  54. inspect=0
  55. autonumber=0
  56.  
  57. OPTIND=1
  58. # Resetting OPTIND is necessary if getopts was used previously in the script.
  59. # It is a good idea to make OPTIND local if you process options in a function.
  60.  
  61. while getopts hvif:o:s:e: opt; do
  62.     case $opt in
  63.         h)
  64.             show_help
  65.             exit 0
  66.             ;;
  67.         v)  verbose=$((verbose+1))
  68.             ;;
  69.         i)  inspect=$((inspect+1))
  70.             ;;
  71.         f)  output_file=$OPTARG
  72.             ;;
  73.         o)  offset=$OPTARG
  74.             ((autonumber++))
  75.             ;;
  76.         s)  title_start=$OPTARG
  77.             ;;
  78.         e)  title_end=$OPTARG
  79.             ;;
  80.         *)
  81.             show_help >&2
  82.             exit 1
  83.             ;;
  84.     esac
  85. done
  86. shift "$((OPTIND-1))"   # Discard the options and sentinel --
  87.  
  88. #input file
  89. FILE="$@"
  90. if [ -f "$FILE" ]
  91. then
  92.    printf 'iso2mkv: <%s>\n' "$FILE"
  93. else
  94.    echo "File $FILE does not exist" >&2
  95.    exit 0
  96. fi
  97.  
  98. #output filename
  99. if [ -z $output_file ]; then
  100.     output_file="$(echo $FILE | sed 's=.*/==;s/.iso//I')"
  101. fi
  102.  
  103. #count number of titles in iso
  104. rawout=$(HandBrakeCLI --min-duration 0 -i $FILE -t 0 2>&1 >/dev/null)
  105. count=$(echo $rawout | grep -Eao "\\+ title [0-9]+:" | wc -l)
  106. title_list=$(echo $rawout | grep -Eao "\\+ duration: [0-9]+:[0-9]+:[0-9]+"| sed 's/+\sduration://g')
  107.  
  108. #inspect to spit out the title lengths so we know what titles we want to export.
  109. if [ $inspect -gt 0 ]
  110. then
  111.  tIndex=1
  112.  for title in $title_list; do
  113.    printf 'Title: %2s %s\n' "$tIndex" "$title"
  114.    ((tIndex++))
  115.  done
  116.  exit 0
  117. fi
  118.  
  119. #bound title end to max count
  120. if [ $title_end -lt 1 ] || [ $title_end -gt $count ]
  121. then
  122.    title_end=$count
  123. fi
  124.  
  125. #bound title_start to title_end
  126. if [ $title_start -gt $title_end ]
  127. then
  128.    title_start=$title_end
  129. fi
  130.  
  131. #set offset to 1 if we haven't overridden it.
  132. if [ $offset -lt 1 ]
  133. then
  134.    offset=$title_start
  135. fi
  136.  
  137. #enable auto numbering if exporting more then 1 title
  138. if [ $title_end -ne $title_start ]
  139. then
  140.   ((autonumber++))
  141. fi
  142.  
  143. #set the file name
  144. filename="${output_file}.mkv"
  145.  
  146. #loop from $title_start to $title_end
  147. for i in $(seq $title_start $title_end)
  148. do
  149.     #override filename because we have more then one title, or -o was used.
  150.     if [ $autonumber -gt 0 ]
  151.     then
  152.         filename="${output_file}$(printf "%02d" $offset).mkv"
  153.         ((offset++))
  154.     fi
  155.     echo -e "\tExtracting title $i as '${filename}'\n"
  156.     HandBrakeCLI -i "$FILE" -t $i --preset Normal --output "${filename}"
  157. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement