0

I would like to automate my video conversion with linux shell scripts. I use HandBrakeCLI on ubuntu 14.04 with my individual options. But I'm stuck at a certain point. This is what I would like to accomplish in pseudo code:

# watch a specific folder (A)
- if a (source) file is older then 1d process it
- as soon as the process is finished move the source file into folder (B)
- the target for the new file is folder (C)
# process the files
1. find all the new files (older then 1d) within (A)
2. get the full path of the file and store it
3. replace the source folder path (A) with the target folder (C)
4. start conversion with HandBrakeCLI like:
   HandBrake $options $sourcefile $targetfile

The first part with the specific folder scan I covered with this code:

find $A -name "*.mkv" -ctime +1 -print0

I'd like to pass the absolute path of the source file from folder (A) into my script 'convertMkv'. The tricky part is right here:

find $A -name "*.mkv" -ctime +1 -print0 | xargs -0 -I {} ./convertMkv "{}" "$C" ;

I would like to pass the source file from folder (A) and the target folder (C) to my conversion script, which will prepare the necessary paths and trigger HandBrakeCLI.

A samples of paths for source files could be:

"/tmp/Video of Interest.mkv"

"/tmp/File\ Folder/Lion_King.mkv"

"/tmp/Avatar.mkv"


This is my 'convertMkv' script:

#!/bin/bash
source="$HOME/handbrake/.raw"
target="$2"

OPT=""
OPT="$OPT --verbose"
OPT="$OPT --encode x264"
OPT="$OPT --quality 20.0"
OPT="$OPT --format mp4"
...

# -----------------------------------------------------------------------------
# mkv2mp4()
# -----------------------------------------------------------------------------
function mkv2mp4
{
    input=$1
    output=$2

    HandBrakeCLI $OPT -i "$input" -o "$output" 2>&1 | tee "/tmp/log/Test.log"
}

function main
{
    path="${1%/*}"
    file="${1##*/}"
    newPath="${path##/*/}"

    mkv2mp4 $1 $target/$newPath/$file
}

main $@
exit 0
MarcelHodan
  • 580
  • 1
  • 7
  • 27
  • http://shellcheck.net/ will catch the category of bugs responsible for this kind of issue (which is to say, inadequate quoting). – Charles Duffy Nov 18 '15 at 23:11
  • BTW, using the `function` keyword is bad form -- makes your code incompatible with the POSIX sh standard without adding any value. Simply define your functions as `main() {`, not `function main {`. – Charles Duffy Nov 18 '15 at 23:12

1 Answers1

2

Quote your variables:

mkv2mp4 "$1" "$target/$newPath/$file"

and:

main "$@"
Barmar
  • 741,623
  • 53
  • 500
  • 612