2016-07-22 80 views
0

我尝试使用ffmpeg为VHS备份编写简单的转码脚本。但是我无法处理文件名中的空格。bash中ffmpeg的文件名空间

我在脚本中一起构建了我的ffmpeg命令并对其进行了回显,并且在复制时粘贴了它的回显命令,但并非直接来自脚本。

有没有一个想法我的脚本怎么了?

脚本:

#!/bin/bash 
# VHStoMP4Backup Script 

INPUT=$1 
OUTPUT="/Volumes/Data/oliver/Video/Encodiert/${2}" 

command="ffmpeg \ 
    -i \"$INPUT\" \ 
    -vcodec copy \ 
    -acodec copy \ 
    \"$OUTPUT\"" 


if [ ! -z "$1" ] && [ ! -z "$2" ] ; 
then 
    echo ${command}$'\n' 
    ${command} 
else 
    echo "missing parameters" 
    echo "Usage: script INPUT_FILENAME OUTPUT_FILENAME" 
fi 

exit 

脚本中调用:

./VHStoMP4Backup.sh /Volumes/Data/oliver/Video/RAW\ Aufnahmen/Ewelina\ -\ Kasette\ 1.dv ewe.mp4 

命令行输出

olivers-mac-pro:Desktop oliver$ ./VHStoMP4Backup.sh /Volumes/Data/oliver/Video/RAW\ Aufnahmen/Ewelina\ -\ Kasette\ 1.dv ewe.mp4 
    ffmpeg -i "/Volumes/Data/oliver/Video/RAW Aufnahmen/Ewelina - Kasette 1.dv" -vcodec copy -acodec copy "/Volumes/Data/oliver/Video/Encodiert/ewe.mp4" 

    ffmpeg version git-2016-04-16-60517c3 Copyright (c) 2000-2016 the FFmpeg developers 
     built with Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) 
     configuration: --prefix=/usr/local/Cellar/ffmpeg/HEAD --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-opencl --enable-libx264 --enable-libmp3lame --enable-libxvid --enable-libfreetype --enable-libvorbis --enable-libvpx --enable-librtmp --enable-libfaac --enable-libass --enable-libssh --enable-libspeex --enable-libfdk-aac --enable-openssl --enable-libopus --enable-libvidstab --enable-libx265 --enable-nonfree --enable-vda 
     libavutil  55. 22.100/55. 22.100 
     libavcodec  57. 34.102/57. 34.102 
     libavformat 57. 34.101/57. 34.101 
     libavdevice 57. 0.101/57. 0.101 
     libavfilter  6. 42.100/6. 42.100 
     libavresample 3. 0. 0/3. 0. 0 
     libswscale  4. 1.100/4. 1.100 
     libswresample 2. 0.101/2. 0.101 
     libpostproc 54. 0.100/54. 0.100 
    "/Volumes/Data/oliver/Video/RAW: No such file or directory 
+0

另请参阅http://stackoverflow.com/questions/12136948/in-bash-why-do-shell-commands-ignore-quotes-in-arguments-when-the-论据,是 – tripleee

回答

1

Never store a command and its arguments in a regular variable,期望通过简单地扩展变量来执行命令。使用数组存储参数,然后在调用实际命令时展开数组。

if [ $# -lt 3 ]; then 
    echo "missing parameters" 
    echo "Usage: script INPUT_FILENAME OUTPUT_FILENAME" 
else 
    INPUT=$1 
    OUTPUT="/Volumes/Data/oliver/Video/Encodiert/${2}" 

    args=(-i "$INPUT" -vcodec -acodec "$OUTPUT") 
    ffmpeg "${args[@]}" 
fi 

您需要做更多的工作才能正确记录命令,但这是付出安全,正确代码的小代价。

printf 'ffmpeg' 
printf ' %q' "${args[@]}" 
printf '\n' 

(登录命令将不完全一样,你期望的,但它可以作为一个有效的命令行运行相同的命令。尤其是%q符倾向于用反斜杠单独转义字符而不是将较长的字符串放在引号中)。