2016-07-22 2 views
0

私はffmpegを使ってVHSバックアップ用の簡単なトランスコーディングスクリプトを書こうとしています。しかし、私はファイル名のスペースを扱わない。bashのffmpegのファイル名のスペース

私はffmpegコマンドをスクリプトに組み込んでエコーし、エコーされたコマンドをコピーして貼り付けると、それは動作しますが、スクリプトからはdiretclyではありません。

私のスクリプトには何の問題がありますか?

スクリプト:

#!/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指定子はバックスラッシュで個別に文字をエスケープする傾向があります長い文字列を引用符で囲む代わりに)

関連する問題