2

I have a rmvb file path list, and want to convert this files to mp4 files. So I hope to use bash pipeline to handle it. The code is

Convert() {
    ffmpeg -i "$1" -vcodec mpeg4 -sameq -acodec aac -strict experimental "$1.mp4"
}

Convert_loop(){
    while read line; do
       Convert $line
    done
}

cat list.txt | Convert_loop

However, it only handle the first file and the pipe exits.

So, does ffmpeg affect the bash pipe?

4

3 回答 3

3

[...]

for i in `cat list.txt`

永远不要使用这种语法:

for i in $(command); do ...; done # or
for i in `command`; do ...; done

这种语法逐字读取命令的输出,而不是逐行读取,这通常会产生意想不到的问题(例如,当行包含一些空格以及当您想要读取一行时,例如某个项目)。

总有一个更聪明的解决方案:

command|while read -r; do ...; done # better general case to read command output in a loop
while read -r; do ...; done <<< "$(command)" # alternative to the previous solution
while read -r; do ...; done < <(command) # another alternative to the previous solution
for i in $DIR/*; do ...; done # instead of "for i in $(ls $DIR); do ...; done
for i in {1..10}; do ...; done # instead of "for i in $(seq 1 10); do ...; done
for (( i=1 ; i<=10 ; i++ )); do ...; done # such that the previous command
while read -r; do ...; done < file # instead of "cat file|while read -r; do ...; done"
while read -r || [[ -n $REPLY ]]; do ...; done < file # Same as before but also deal with files that doesn't have EOF.

# dealing with xargs or find -exec sometimes...
# ...

我写了一门课程,其中包含有关此主题和反复出现的错误的更多详细信息,但不幸的是用法语:)

要回答原始问题,您可以使用以下内容:

Convert() {
    ffmpeg -i “$1” -vcodec mpe4 -sameq -acodec aac -strict experimental “$1.mp4”
}

Convert_loop(){
   while read -r; do
       Convert $REPLY
   done < $1
}

Convert_loop list.txt
于 2013-10-26T13:36:09.277 回答
3

警告:我从未使用过,但在处理有关程序的其他问题时,ffmpeg似乎从标准输入读取而不实际使用它,因此第一次调用是在获取第一个之后消耗文件列表的其余部分线。尝试这个sshffmpegConvertread

Convert() {
    ffmpeg -i "$1" -vcodec mpe4 -sameq -acodec aac \
           -strict experimental "$1.mp4" < /dev/null
}

这样,ffmpeg就不会从用于read命令的标准输入中“劫持”数据。

于 2013-10-26T14:24:24.500 回答
1

!=)

convert() {
    ffmpeg -i "$1" \
           -vcodec mpe4 \
           -sameq -acodec aac \
           -strict experimental "${1%.*}.mp4"
}

while read line; do
    convert "$line"
done < list.txt
于 2013-10-26T15:28:21.797 回答