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.

4

2 回答 2

1

您可以改用循环和来自进程替换while read的重定向输入。我对代码进行了评论以获取详细信息。

#!/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.

更新:

#!/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.
于 2013-09-24T17:40:23.557 回答
1

find不支持参数替换,需要传递${1%.*}给shell。但幸运的是,这并不难做到:

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

所以只是为了澄清; “顶级”是find -exec bash -c '...' _ {} \;单引号内的 Bash 脚本包含三个命令,所有这些命令都对同一个输入文件进行操作。

于 2013-09-24T17:45:00.543 回答