移动新创建的文件

时间:2013-09-24 17:24:15

标签: linux ubuntu mv transcode handbrake

我有以下代码,转码完成后我希望移动新创建的文件。但只有在之后,我不想在转码时写入其他文件夹。这就是为什么我认为使用exec更好,因为只有在前一个exec读为true时才会处理。另请注意,当前文件夹中可能有多个文件。

#!/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 {} \;

我认为我对linux的一点知识可能是:

#!/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" \;

但这只是说出来了:

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

现在我想也许这是一些来自notepad ++的人物隐藏在某处,所以我通过dos2uunix运行它。但我仍然得到同样的错误。

现在我认为“$ {1%。}”。mp4实际上并没有获得新创建的文件,而是在寻找一个名为“$ {1%。}”的文件。 mp4,不存在。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

您可以使用来自process substitutionwhile 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.

答案 1 :(得分: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脚本包含三个命令,所有命令都在同一个输入文件上运行。