Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Script to repeatly convert avi into a different format (according the setting
- # saved) I used MP4 for my own use.
- # Next would be improved to add argument parsing on the CLI line.
- usage() {
- echo ' Program [-i dir] [-o dir] [-t ext] [-f file]
- [-i <directory>] directory to scan for files to be processed
- If none will work on the current directory
- [-o <directory>] directory to save the processed files
- If none will work on the current directory
- [-t <list of extensions>]
- a list of file type to be search by their extension and loaded.
- Just mention the word after the dot (i.e. avi mkv) and strictly
- comma separated. If none, the default will go for avi.
- [-f <setting>] the full path of avidemux setting that will use to convert the
- input file (i.e. mp4.js). This might affect the file extension
- type which is defaulted to mp4.
- Other extension types and particular command line specification,
- must specify into the loading setting. Only the first valid
- statement will get in force.
- One line in the setting must contain "#! extension =" and the
- type of extension you want to have on the output file(s).
- (quotes excluded! case sensitive)
- One line in the setting would contain "#! specification ="
- and the type of command line parameter you want to add there.
- (quotes excluded! case sensitive)
- This is worth in a non GUI environment, but it might be valid
- to code with ECMAScript type.
- [-log] a file to save the on screen output. If none, all discarted
- All are optional. Defaults as explained, Free option positioning '
- exit 255
- }
- question() {
- while getopts ":i:o:t:f:l:h" Option; do
- case $Option in
- i ) DIR=$OPTARG ;;
- o ) OUTD=$OPTARG ;;
- t ) read_type $OPTARG;;
- f ) get_format $OPTARG;;
- l ) LOG=$OPTARG;;
- h ) usage ;;
- * ) usage ;;
- esac
- done
- }
- read_type() {
- # Read a list of extensions and return al list of the files in the given
- # directory
- local list=''
- list=$(echo $1 |sed 's/\(\,\)/|/g')
- #set the list to what has been found
- LIST=$(search_by_ext $list 'extend')
- }
- get_format() {
- # Load avidemux setting file and extract the wanted file extension
- if ! [ -e $1 ]; then # Not found ? Quit
- echo 'No setting file found, exiting'
- echo ''
- usage
- fi
- local _line
- _line=$(grep -m1 "^#! extension" $1)
- [ ${#_line} -gt 0 ] && EXT=${_line/'#! extension'/} | sed s/\[\ =\]//g
- _line=$(grep -m1 "^#! specification" $1)
- [ ${#_line} -gt 0 ] && SPECS=${_line/'#! specification'/} | sed s/\[\ =\]//g
- }
- avail_space(){
- # calculate the available space for a given directory and check if there's
- # double the size of the give file. If enough space will return 1
- dirspace=$(df -B1 $1| awk 'END{print $4}')
- filesize=$(($(ls -l $2 | awk '{print $5}')*2))
- [ $dirspace -gt $filesize ] && return 1
- return 0
- }
- search_by_ext() {
- local _m='' pre='' post='' lang=$LANG
- # setting LANG to english for parsing the result
- LANG=en_US
- # if we use extended options we can add globbing
- [ $# == 2 ] &&
- if [ $2 == 'extend' ]; then
- shopt -s extglob
- pre='?('
- post=')'
- fi
- #Filtering some possible error
- m=$(ls $DIR/*.$pre$1$post 2>&1)
- # Restore locale language
- LANG=$lang
- # if avi files are found it returns a list
- [ $(echo $m | grep -c1 'ls: cannot access') == '0' ] && return $m
- }
- # Save the separator and use only newline termination
- ifs=$IFS
- DIR=$PWD
- #strip last / if it's there
- if [ ${DIR:((${#DIR}-1))} == '/' ]; then
- DIR=${DIR::-1}
- fi
- SPECS='--force-alt-h264'
- EXT='.mp4'
- FORMAT=~/.avidemux/jobs/avi2mp4-2.js
- LOG=/dev/null
- # Default to avi
- LIST=$(search_by_ext 'avi')
- #read all the arguments on the command line
- question $@
- # Verify a valid directory
- if ! [ -d $DIR ]; then
- echo $DIR' is not a valid directory'
- exit 1
- fi
- # it scans for all types of file inside the directory
- if [ ${#LIST} -gt 0 ]; then
- # chance separator to new line
- IFS='
- '
- for FIL in $LIST; do
- # It will quit in case no space to finish
- # we could remove the avi file once is done, but just leave it for inspecting
- # the result.
- avail_space $DIR $FIL # check for space
- if [ $? != '1' ]; then
- echo "No more space available on the destination directory "$DIR
- break
- fi
- # Saving the file into the origin directory
- DEST=$OUTD${FIL%.*}.$EXT
- # The script would be for mp4 conversion. Remember to tick auto unpacking VBR
- [ -f $DEST ] && echo $DEST' exists, skipping' && continue
- avidemux2_cli $SPECS --load "$FIL" --run $FORMAT --save $DEST >> $LOG
- done
- avidemux2_cli --quit
- else
- echo 'no files to process'
- fi
- IFS=$ifs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement