Advertisement
Guest User

adrepeater.sh

a guest
Sep 23rd, 2012
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.01 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Script to repeatly convert avi into a different format (according the setting
  4. # saved) I used MP4 for my own use.
  5. # Next would be improved to add argument parsing on the CLI line.
  6.  
  7. usage() {
  8.    echo ' Program [-i dir] [-o dir] [-t ext] [-f file]
  9. [-i <directory>] directory to scan for files to be processed
  10.              If none will work on the current directory
  11. [-o <directory>] directory to save the processed files
  12.                 If none will work on the current directory
  13. [-t <list of extensions>]
  14.                 a list of file type to be search by their extension and loaded.
  15.                 Just mention the word after the dot (i.e. avi mkv) and strictly
  16.                 comma separated. If none, the default will go for avi.
  17. [-f <setting>]   the full path of avidemux setting that will use to convert the
  18.                 input file (i.e. mp4.js). This might affect the file extension
  19.                 type which is defaulted to mp4.
  20.                 Other extension types and particular command line specification,
  21.                 must specify into the loading setting. Only the first valid
  22.                 statement will get in force.
  23.                 One line in the setting must contain "#! extension =" and the
  24.                 type of extension you want to have on the output file(s).
  25.                 (quotes excluded! case sensitive)
  26.                 One line in the setting would contain "#! specification ="
  27.                 and the type of command line parameter you want to add there.
  28.                 (quotes excluded! case sensitive)
  29.                 This is worth in a non GUI environment, but it might be valid
  30.                 to code with ECMAScript type.
  31. [-log]           a file to save the on screen output. If none, all discarted
  32. All are optional. Defaults as explained, Free option positioning '
  33. exit 255
  34. }
  35. question() {
  36.    while getopts ":i:o:t:f:l:h" Option; do
  37.      case $Option in
  38.        i ) DIR=$OPTARG ;;
  39.        o ) OUTD=$OPTARG ;;
  40.        t ) read_type $OPTARG;;
  41.        f ) get_format $OPTARG;;
  42.        l ) LOG=$OPTARG;;
  43.        h ) usage ;;
  44.        * ) usage ;;
  45.      esac
  46.    done
  47. }
  48.  
  49. read_type() {
  50.    # Read a list of extensions and return al list of the files in the given
  51.    # directory
  52.    local list=''
  53.    list=$(echo $1 |sed 's/\(\,\)/|/g')
  54.    #set the list to what has been found
  55.    LIST=$(search_by_ext $list 'extend')
  56. }
  57.  
  58. get_format() {
  59.    # Load avidemux setting file and extract the wanted file extension
  60.    if ! [ -e $1 ]; then # Not found ? Quit
  61.       echo 'No setting file found, exiting'
  62.       echo ''
  63.       usage
  64.    fi
  65.    local _line
  66.    _line=$(grep -m1 "^#! extension" $1)
  67.    [ ${#_line} -gt 0 ] && EXT=${_line/'#! extension'/} | sed s/\[\ =\]//g
  68.    _line=$(grep -m1 "^#! specification" $1)
  69.    [ ${#_line} -gt 0 ] && SPECS=${_line/'#! specification'/} | sed s/\[\ =\]//g
  70. }
  71. avail_space(){
  72.    # calculate the available space for a given directory and check if there's
  73.    # double the size of the give file. If enough space will return 1
  74.    dirspace=$(df -B1 $1| awk 'END{print $4}')
  75.    filesize=$(($(ls -l $2 | awk '{print $5}')*2))
  76.    [ $dirspace -gt $filesize ] && return 1
  77.    return 0
  78. }
  79. search_by_ext() {
  80.    local _m='' pre='' post='' lang=$LANG
  81.    # setting LANG to english for  parsing the result
  82.    LANG=en_US
  83.    # if we use extended options we can add globbing
  84.    [ $# == 2 ] &&
  85.    if [ $2 == 'extend' ]; then
  86.       shopt -s extglob
  87.       pre='?('
  88.       post=')'
  89.    fi
  90.    #Filtering some possible error
  91.    m=$(ls $DIR/*.$pre$1$post 2>&1)
  92.    # Restore locale language
  93.    LANG=$lang
  94.    # if avi files are found  it returns a list
  95.    [ $(echo $m | grep -c1 'ls: cannot access') == '0' ] && return $m
  96. }
  97.  
  98. # Save the separator and use only newline termination
  99. ifs=$IFS
  100. DIR=$PWD
  101. #strip last / if it's there
  102. if [ ${DIR:((${#DIR}-1))} == '/' ]; then
  103.    DIR=${DIR::-1}
  104. fi
  105. SPECS='--force-alt-h264'
  106. EXT='.mp4'
  107. FORMAT=~/.avidemux/jobs/avi2mp4-2.js
  108. LOG=/dev/null
  109. # Default to avi
  110. LIST=$(search_by_ext 'avi')
  111. #read all the arguments on the command line
  112. question $@
  113. # Verify a valid directory
  114. if ! [ -d $DIR ]; then
  115.    echo $DIR' is not a valid directory'
  116.    exit 1
  117. fi
  118. # it scans for all types of file inside the directory
  119. if [ ${#LIST} -gt 0 ]; then
  120.    # chance separator to new line
  121.    IFS='
  122. '
  123.    for FIL in $LIST; do
  124.       # It will quit in case no space to finish
  125.       # we could remove the avi file once is done, but just leave it for inspecting
  126.       # the result.
  127.       avail_space $DIR $FIL # check for space
  128.       if [ $? != '1' ]; then
  129.          echo "No more space available on the destination directory "$DIR
  130.          break
  131.       fi
  132.       # Saving the file into the origin directory
  133.       DEST=$OUTD${FIL%.*}.$EXT
  134.       # The script would be for mp4 conversion. Remember to tick auto unpacking VBR
  135.       [ -f $DEST ] && echo  $DEST' exists, skipping' && continue
  136.       avidemux2_cli $SPECS --load "$FIL" --run $FORMAT --save $DEST >> $LOG
  137.    done
  138.    avidemux2_cli --quit
  139. else
  140.    echo 'no files to process'
  141. fi
  142. IFS=$ifs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement