0

I have the following code and after the transcode finishes i wish to move the newly created file. But only after, i don't want to write to the other folder as it trancodes. That is why i presume using exec is better as this will only be processed if the previous exec read true. Also note that there maybe more than one file in the current folder.

#!/bin/bash
#
# Change this to specify a different handbrake preset. You can list them by running:     "HandBrakeCLI --preset-list"
#
PRESET="AppleTV 2"
if [ -z "$1" ] ; then
TRANSCODEDIR="/path/to/folder"
else
TRANSCODEDIR="$1"
fi
find "$TRANSCODEDIR"/* -type f -exec bash -c 'HandBrakeCLI -i "$1" -o "${1%.*}".mp4 --    preset="$PRESET"' __ {} \; -exec rm {} \;

My little knowledge of linux i thought maybe:

#!/bin/bash
#
# Change this to specify a different handbrake preset. You can list them by running:     "HandBrakeCLI --preset-list"
#
PRESET="AppleTV 2"
if [ -z "$1" ] ; then
TRANSCODEDIR="/path/to/folder"
else
TRANSCODEDIR="$1"
fi
find "$TRANSCODEDIR"/* -type f -exec bash -c 'HandBrakeCLI -i "$1" -o "${1%.*}".mp4 --    preset="$PRESET"' __ {} \; -exec rm {} \; -exec mv '"${1%.*}".mp4'     "/path to/converted/folder" \;

But this just puts out:

mv: cannot stat â"${1%.*}".mpâ4: No such file or directory

Now i thought maybe this was some characters from notepad++ hiding in there somewhere so i ran it through dos2uunix. But still i am getting the same error.

Now i thinking that "${1%.}".mp4 isn't actually getting the newly create file rather it is looking for a file called "${1%.}".mp4, which doesn't exist.

Any help would be appreciated.

Keelan
  • 325
  • 1
  • 4
  • 12

2 Answers2

1

You can use a while read loop and redirected input from process substitution instead. I made comments on the code for the details.

#!/bin/bash

#
# Change this to specify a different handbrake preset. You can list them by running:     "HandBrakeCLI --preset-list"
#
PRESET="AppleTV 2"

if [[ -z $1 ]] ; then
    TRANSCODEDIR="/path/to/file"
else
    TRANSCODEDIR="$1"
fi

while read -r FILE; do
    echo "Converting $FILE to ${FILE%.*}.mp4."  ## Optional message that could really be helpful.
    HandBrakeCLI -i "$FILE" -o "${FILE%.*}".mp4 && rm "$FILE"  ## Remove file only if process was successful (HandBrakeCLI returns zero return code).
done < <(exec find "$TRANSCODEDIR"/ -type f)  ## No need to specify * and do pathname expansions since we're searching it recursively anyway.

UPDATE:

#!/bin/bash

#
# Change this to specify a different handbrake preset. You can list them by running:     "HandBrakeCLI --preset-list"
#
PRESET="AppleTV 2"

SOURCEDIR=.

if [[ -z $1 ]] ; then
    TRANSCODEDIR="/path/to/dir"
else
    TRANSCODEDIR="$1"
fi

SOURCEDIR=${SOURCEDIR%/}/
OFFSET=${#SOURCEDIR}

while read -r FILE; do
    SUBPART=${FILE:OFFSET}

    # Prepare target directory. We create subdirectories to prevent conflict within files having the same name.

    TDIR=${SUBPART%/*}
    TDIR=${TRANSCODEDIR%/}/$TDIR

    mkdir -p "$TDIR" || {
        echo "Unable to create $TDIR."
        continue  ## Continue loop if we can't create the directory."
    }

    # Convert

    TFILE=${SUBPART%.*}.mp4
    TFILE=${TRANSCODEDIR%/}/$TFILE

    echo "Converting $FILE to $TFILE."  ## Optional message that could really be helpful.
    HandBrakeCLI -i "$FILE" -o "$TFILE" && rm "$FILE"  ## Remove file only if process was successful (HandBrakeCLI returns zero return code).
done < <(exec find "$SOURCEDIR" -type f)  ## No need to specify * and do pathname expansions since we're searching it recursively anyway.
konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • Little confused where would i begin to move the file to its new directory i presume in (exec find "$TRANSCODEDIR"/ -type f)? – Keelan Sep 24 '13 at 17:43
  • 1
    @Keelan Do you intend to have a different target directory? Is your source directory the current directory? – konsolebox Sep 24 '13 at 17:44
  • Yes, basically i will run this program to transcode the file sin the current directory. Then after each transcode it complete it deletes the original and moves the new mp4 into another directory /path/to/converted/folder. Now the remove exec and transcode works fine, i just have an issue finding the new mp4 to move it. – Keelan Sep 24 '13 at 17:47
  • 1
    @Keelan If your target file is already placed on the target directory you would no longer have to move it. Please check my update. I had made some pretests an it probably would work now as you wanted. – konsolebox Sep 24 '13 at 18:00
1

find does not support parameter substitutions, you need to pass ${1%.*} to the shell. But fortunately, that's not very hard to do:

find "$TRANSCODEDIR" -type f -exec bash -c 'HandBrakeCLI -i "$1" -o "${1%.*}".mp4 --    preset="$PRESET";
    rm "$1";
    mv "${1%.*}".mp4 "/path/to/converted/folder"' _ {} \;

So just to clarify; the "top level" is find -exec bash -c '...' _ {} \; and the Bash script inside the single quotes contains three commands, all of which operate on the same input file.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Very nice, this seems to work very well. Will this execute in order? What if the transcode fails will ti will try to move it? The files coming into the "current" directory come from somewhere else and have a a folder associated with them e.g. /path/to/current/directory/MKV FILE/file.mkv. When i move this file i am left with an empty directory. Anyway to safely delete the directory, rm -rf *? – Keelan Sep 24 '13 at 18:19
  • Nevermind i just run a cron script to delete empty directories. – Keelan Sep 24 '13 at 19:03
  • 1
    The commands are unconditional but you can run an arbitrarily complex script inside the single quotes. If Handbrake sets its exit code properly then `bash -e -c '...'` will break on error. – tripleee Sep 24 '13 at 19:07