首页 > 解决方案 > 读取文件名中的空格时

问题描述

在这个.ogg文件上

$ tree
.
├── Disc 1 - 01 - Procrastination.ogg
├── Disc 1 - 02 - À carreaux !.ogg
├── Disc 1 - 03 - Météo marine.ogg
└── mp3

我尝试使用while循环将 ffmpeg 转换为 mp3,在文件名中保留空格::

$ ls *.ogg | while read line; do ffmpeg -i "$line" mp3/"$line".mp3 ; done

但我得到这个错误::

$ ls *.ogg | while read line; do ffmpeg -i "$line" mp3/"$line".mp3 ; done
...
Parse error, at least 3 arguments were expected, only 0 given
in string ' 1 - 02 - À carreaux !.ogg' ...
...

此报告bash ffmpeg find and space in filenames即使看起来相似,也适用于更复杂的脚本并且没有答案。

ffmpeg 不适用于具有空格的文件名,仅当输出为 http:// URL 时才修复它

标签: bashwhile-loopffmpeg

解决方案


用于find -print0获取 NUL 分隔的文件列表,而不是解析ls输出,这绝不是一个好主意:

#!/bin/bash

while read -d '' -r file; do
  ffmpeg -i "$file" mp3/"$file".mp3 </dev/null
done < <(find . -type f -name '*.ogg' -print0)

您也可以使用简单的 glob 来执行此操作:

shopt -s nullglob # make glob expand to nothing in case there are no matching files
for file in *.ogg; do
  ffmpeg -i "$file" mp3/"$file".mp3
done

看:


推荐阅读