【发布时间】:2015-11-18 22:58:33
【问题描述】:
我想使用 linux shell 脚本自动进行视频转换。我在 ubuntu 14.04 上使用 HandBrakeCLI 和我的个人选项。但我被困在某个点上。这是我想用伪代码完成的:
# watch a specific folder (A)
- if a (source) file is older then 1d process it
- as soon as the process is finished move the source file into folder (B)
- the target for the new file is folder (C)
# process the files
1. find all the new files (older then 1d) within (A)
2. get the full path of the file and store it
3. replace the source folder path (A) with the target folder (C)
4. start conversion with HandBrakeCLI like:
HandBrake $options $sourcefile $targetfile
我用这段代码介绍的特定文件夹扫描的第一部分:
find $A -name "*.mkv" -ctime +1 -print0
我想将源文件的绝对路径从文件夹 (A) 传递到我的脚本“convertMkv”中。棘手的部分就在这里:
find $A -name "*.mkv" -ctime +1 -print0 | xargs -0 -I {} ./convertMkv "{}" "$C" ;
我想将文件夹 (A) 和目标文件夹 (C) 中的源文件传递给我的转换脚本,这将准备必要的路径并触发 HandBrakeCLI。
源文件的路径示例可以是:
"/tmp/Video of Interest.mkv"
"/tmp/File\文件夹/Lion_King.mkv"
"/tmp/Avatar.mkv"
这是我的“convertMkv”脚本:
#!/bin/bash
source="$HOME/handbrake/.raw"
target="$2"
OPT=""
OPT="$OPT --verbose"
OPT="$OPT --encode x264"
OPT="$OPT --quality 20.0"
OPT="$OPT --format mp4"
...
# -----------------------------------------------------------------------------
# mkv2mp4()
# -----------------------------------------------------------------------------
function mkv2mp4
{
input=$1
output=$2
HandBrakeCLI $OPT -i "$input" -o "$output" 2>&1 | tee "/tmp/log/Test.log"
}
function main
{
path="${1%/*}"
file="${1##*/}"
newPath="${path##/*/}"
mkv2mp4 $1 $target/$newPath/$file
}
main $@
exit 0
【问题讨论】:
-
shellcheck.net 将捕获导致此类问题(即引用不足)的错误类别。
-
顺便说一句,使用
function关键字是错误的形式——使您的代码与 POSIX sh 标准不兼容,而不会增加任何价值。只需将您的函数定义为main() {,而不是function main {。
标签: linux shell command-line-interface whitespace handbrake