1

我被我的 bash 脚本与命令选项的数组卡住了。

我制作了 bash 脚本以从 mkv 文件中提取附件,最后在视频/音频编码后再次将附件合并到 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[*]}

问题,仍然无法用于带有空格的文件名。当我尝试 echo the${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

Ignacio 的回答将为您指明正确的方向,但我想评论您脚本中的一些内容。

这条线有很多毫无意义的回旋:

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]}"

Also, $"" is used for creating strings for translation (i18n and l10n). You probably don't need it there. However, this is one of the lines that you should change because it's part of your problem. You will find that the line prior to this one with the sed command will be unnecessary.

Edit:

If you really need to use an array:

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[@]}"

The resulting attachment array will have twice as many elements as the font1 array.

$ 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 回答