1

帮助。我尝试过的一切都失败了。我正在尝试修复我的视频收藏,其中很多在名称/等中有空格我想对它们进行转码,我已经编写了下面的脚本,但它失败了。我用于测试的 2 个文件名是“13 Eerie (2013).avi”和“Ace.Ventura.When.Nature.Calls.1995.720p.WEB-DL.x264-mSD.mkv”

我已经尝试了几件事,从在 find 上使用 print0 到破坏 IFS。任何援助将不胜感激。由于某些原因,当前版本将 e 字符上的所有内容分开


转换.sh

#!/bin/sh

OUTDIR="./done/"
LOGDIR="./logs/"
BACKUPDIR="./backup/"

# deal with spaces
SAVEIFS=$IFS
IFS=$(echo -en "\n")

# pull all files except MP4 or scripts in the CURRENT DIR only
for FULLFILENAME in `find . -maxdepth 1 -type f -not -iname "*.sh" -not -iname "*.mp4" -print`
do
    # extract the file extension
    filename=$(basename "$FULLFILENAME")
    ext="${filename##*.}"
    filename="${filename%.*}"

    # use handbrake to convert to x264
    HandBrakeCLI -i "$FULLFILENAME" -o "$OUTDIR$filename.mp4" -e x264 -q 22 -r 29.97 -B 64 -O 2>$LOGDIR$filename.log

    # move the original file to a backup
    mv "$FULLFILENAME" $BACKUPDIR
done

#restore the field separator
IFS=$SAVEIFS
4

1 回答 1

2
  • 不要使用命令替换find
  • 不要使用 for 循环
  • 使用进程替换来获取输出find
  • 使用选项并使用循环-print0读取while
  • 最好避免使用大写的变量名
  • 使用bashshebang

代码:

#!/bin/bash

outdir="./done/"
logdir="./logs/"
backupdir="./backup/"


# pull all files except MP4 or scripts in the CURRENT DIR only
while IFS= read -r -d '' fullfilename
do
    # extract the file extension
    filename="$(basename "$fullfilename")"
    ext="${filename##*.}"
    filename="${filename%.*}"

    # use handbrake to convert to x264
    HandBrakeCLI -i "$fullfilename" -o "$outdir$filename.mp4" -e x264 -q 22 -r 29.97 -B 64 -O 2>"$logdir$filename.log"

    # move the original file to a backup
    mv "$fullfilename" "$backupdir"
done < <(find . -maxdepth 1 -type f -not -iname "*.sh" -not -iname "*.mp4" -print0)
于 2016-03-17T14:59:40.193 回答