1

コマンドのオプションに配列を使用したbashスクリプトを理解するのに行き詰まっています。

mkv ファイルから添付ファイルを抽出するための bash スクリプトを作成し、最後にビデオ/オーディオのエンコード後にその添付ファイルを mkv ファイルに再度マージします。

これは抽出アタッチメント用です

#find the total of attachment
A=$(mkvmerge -i input.mkv | grep -i attachment 
                          | awk '{printf $3 "\n"}' 
                          | sed 's;\:;;' 
                          | awk 'END { print NR }')

#extract it
for (( i=1; i<=$A; i++ ))
do
font[${i}]="$(mkvmerge -i input.mkv | grep -i attachment 
                                    | awk '{for (i=11; i <= NF; i++) printf($i"%c" , (i==NF)?ORS:OFS) }' 
                                    | sed "s/'//g" 
                                    | awk "NR==$i")"

mkvextract attachments input.mkv $i:"${font[${i}]}"
done

そして今、添付ファイルを再度マージします

for (( i=1; i<=$A; i++ ))
do
#seach for space between file name and and '\' before 
#the space because some attachment has space in filename
font1[${i}]=$(echo ${font[${i}]} | sed 's/ /\\ /g')
#make option for add attachment
attachment[${i}]=$"--attach-file ${font1[${i}]}"
done

mkvmerge -o output.mkv -d 1 -S test.mp4 sub.ass ${attachment[*]}

問題は、スペースを含むファイル名ではまだ機能しません。をエコーし​​てみたところ${attachment[*]}、大丈夫そうです

--attach-file Beach.ttf --attach-file Candara.ttf 
                        --attach-file CASUCM.TTF 
                        --attach-file Complete\ in\ Him.ttf 
                        --attach-file CURLZ_.TTF 
                        --attach-file Frostys\ Winterland.TTF 
                        --attach-file stilltim.ttf

ただし、出力では、最初の単語のみにスペースがあるファイル名が引き続き認識されます。

mkvmerge v3.0.0 ('Hang up your Hang-Ups') built on Dec  6 2010 19:19:04
Automatic MIME type recognition for 'Beach.ttf': application/x-truetype-font
Automatic MIME type recognition for 'Candara.ttf': application/x-truetype-font
Automatic MIME type recognition for 'CASUCM.TTF': application/x-truetype-font
Error: The file 'Complete\' cannot be attached because it does not exist or cannot be read.
4

1 に答える 1

0

イグナシオの答えは正しい方向を示しますが、スクリプトのいくつかについてコメントしたかったのです。

この行には無意味な回転がたくさんあります。

A=$(mkvmerge -i input.mkv | grep -i attachment | awk '{printf $3 "\n"}' | sed 's;\:;;' | awk 'END { print NR }')

ここでは簡略化されています:

A=$(mkvmerge -i input.mkv | grep -i attachment | wc -l)

配列インデックスにドル記号と中括弧を使用する必要はありません。

attachment[i]="--attach-file ${font1[i]}"

また、$""翻訳用の文字列の作成にも使用されます (i18n および l10n)。あなたはおそらくそこにそれを必要としません。ただし、これは問題の一部であるため、変更する必要がある行の 1 つです。sedこの行より前のコマンドの行は不要であることがわかります。

編集:

本当に配列を使用する必要がある場合:

for (( i=1; i<=A; i++ ))
do
    #make option for add attachment
    attachment+=("--attach-file" "${font1[i]}")
done

mkvmerge -o output.mkv -d 1 -S test.mp4 sub.ass "${attachment[@]}"

結果のattachment配列には、配列の 2 倍の要素が含まれfont1ます。

$ declare -p font1 attachment    # show the contents
declare -a font1='([0]="Beach.ttf" [1]="Candara.ttf" [2]="CASUCM.TTF" [3]="Complete" [4]="in" [5]="Him.ttf" [6]="CURLZ_.TTF" [7]="Frostys" [8]="Winterland.TTF" [9]="stilltim.ttf")'
declare -a attachment='([0]="--attach-file" [1]="Beach.ttf" [2]="--attach-file" [3]="Candara.ttf" [4]="--attach-file" [5]="CASUCM.TTF" [6]="--attach-file" [7]="Complete in Him.ttf" [8]="--attach-file" [9]="CURLZ_.TTF" [10]="--attach-file" [11]="Frostys Winterland.TTF" [12]="--attach-file" [13]="stilltim.ttf")'
于 2011-03-03T07:03:26.240 に答える