Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top