bash ffmpeg 不适用于具有空格的文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22766111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ffmpeg not working with filenames that have whitespace
提问by cmw
I'm using FFMPEG to measure the duration of videos stored in an Amazon S3 Bucket.
我正在使用 FFMPEG 来测量存储在 Amazon S3 存储桶中的视频的持续时间。
I've read the FFMPEG docs, and they explicitly state that all whitespace and special characters need to be escaped, in order for FFMPEG to handle them properly:
我已经阅读了 FFMPEG 文档,他们明确指出所有空格和特殊字符都需要转义,以便 FFMPEG 正确处理它们:
See docs 2.1 and 2.1.1: https://ffmpeg.org/ffmpeg-utils.html
请参阅文档 2.1 和 2.1.1:https: //ffmpeg.org/ffmpeg-utils.html
However, when dealing with files whose filenames contain whitespace, ffmpeg fails to render a result.
但是,在处理文件名包含空格的文件时,ffmpeg 无法呈现结果。
I've tried the following, with no success
我尝试了以下方法,但没有成功
ffmpeg -i "http://s3.mybucketname.com/videos/my\ video\ file.mov" 2>&1 | grep Duration | awk '{print }' | tr -d
ffmpeg -i "http://s3.mybucketname.com/videos/my video file.mov" 2>&1 | grep Duration | awk '{print }' | tr -d
ffmpeg -i "http://s3.mybucketname.com/videos/my'\' video'\' file.mov" 2>&1 | grep Duration | awk '{print }' | tr -d
ffmpeg -i "http://s3.mybucketname.com/videos/my\ video\ file.mov" 2>&1 | grep Duration | awk '{print }' | tr -d
However, if I strip out the whitespace in the filename – all is well, and the duration of the video is returned.
但是,如果我去掉文件名中的空格 - 一切都很好,并且会返回视频的持续时间。
Any help is appreciated!
任何帮助表示赞赏!
回答by fedorqui 'SO stop harming'
If you happen to have spaces in your file name, just quote them:
如果您的文件名中碰巧有空格,只需引用它们:
ffmpeg -i "my video file.mov"
In a URL, a space cannot be there. Most probably you have to replace every single space with a %20
, so that you get:
在 URL 中,不能有空格。很可能您必须用 a 替换每个空格%20
,以便您得到:
ffmpeg -i http://myurl.com/my%20video%20file.mov
^^^ ^^^
回答by Rodrigo Fava
ffmpeg uses % to define a pattern and handle multiple files. For instance if your filename is URI encoded you must use "-pattern_type none" to avoid misinterpretation from ffmpeg:
ffmpeg 使用 % 来定义模式并处理多个文件。例如,如果您的文件名是 URI 编码的,则必须使用“-pattern_type none”以避免来自 ffmpeg 的误解:
ffmpeg -pattern_type none -i file%20name.mp4
回答by MagTun
To make ffmeg works with filename/path that have whitespaces, you should:
1) set the working directory with Pushd
2) put your filename inside quote ""
要使 ffmeg 使用具有空格的文件名/路径,您应该:
1)设置工作目录Pushd
2)将您的文件名放在引号内""
here is a working example:
这是一个工作示例:
cls
REM ++++++++++++++++++++++++++
REM Cut an audio file by right-cliking it (works also on multiple selected audio)
REM 1) save this file
REM 2) open registry, browse to Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\audio\shell. On the left panel, right-click on "shell" and select "New", then "Key". Type "Cut audio 5min". Right click on the newly created folder "Cut audio 5min" and select again "New" and then "Key". Type “command”. On the right pane, double-click the "(default)" value name and type the following: "C:\Users\Me\Cut audio.cmd" "%1"
REM optional: if you want an Icon : in the left pane, right click "Cut audio 5min", and select String value, then in the right pane, rename the new value to Icon, then double click on it and past this "%SystemRoot%\System32\shell32.dll,186" (; list icon: https://diymediahome.org/windows-icons-reference-list-with-details-locations-images/ )
REM 3) right click an audio file and select "Cut audio 5min": the chunks will be created in the same folder.
REM because ffmpeg has trouble with path/filename with space, we set the working directory to the folder of the audio file and the we run the command not on a full path but just on the filename, with "" around it
REM get https://stackoverflow.com/a/15568171/3154274
REM fullpath of rightclicked file %1
REM full path (letter drive + path ithout letter drive) %~d1%~p1
REM filename (filename + extension) %~n1%~x1
REM https://windowsloop.com/split-mp3-files-with-ffmpeg/
REM ffmpeg -i "input_audio_file.mp3" -f segment -segment_time 300 -c copy output_audio_file_%%03d.mp3
REM 300= 5min chunk
REM ++++++++++++++++++++++++++
REM set working directory
Pushd %~d1%~p1
REM to let the windows open cmd /k ffmpeg -i "%~n1%~x1" -f segment -segment_time 300 -c copy "%~n1"_%%03d.mp3
cmd /k ffmpeg -i "%~n1%~x1" -f segment -segment_time 300 -c copy "%~n1"_%%03d.mp3
回答by christian
for fileOne in *.mp4
do
baseName=$(basename "$fileOne" .mp4) # "$fileOne" quotes are necessary because of special chars in it
echo "input: " $fileOne
echo "var: " $baseName
echo "target: " $baseName".mp3"
cp "$fileOne" "tmp.mp4"
# ffmpeg problem with specialchars like whitespaces and '-'
# ffmpeg -i \"$fileOne\" "$baseName..mp3"
ffmpeg -i "tmp.mp4" "tmp.mp3"
mv "tmp.mp3" "$baseName".mp3""
done
rm "tmp-mp4"
`just rename the file for the conversion :)
`只需为转换重命名文件:)
回答by rocksyne
As many have pointed out, the best option is to quote the string. It works for all other special characters. I am attaching a snapshot from Here are some examples I found of the ffmpeg documentation page. I am attaching a screen shot just in in case it is unavailable in the future.
正如许多人指出的那样,最好的选择是引用字符串。它适用于所有其他特殊字符。我附上了一个快照,这里是我在ffmpeg 文档页面中找到的一些示例。我附上屏幕截图,以防将来无法使用。
回答by J. Does
I solved it by "enquoting" the arg that contain the file path. In my case, the path was stored in the %1 argument (which was written in the registry under quote that are escaped: \"%1\" ). I retrieve it (with a PowerShell script), simply using $arg (inbuilt argument). I then used it get some file information such as:
我通过“引用”包含文件路径的 arg 来解决它。在我的例子中,路径存储在 %1 参数中(它写在注册表中被转义的引号下: \"%1\" )。我检索它(使用 PowerShell 脚本),只需使用 $arg (内置参数)。然后我用它来获取一些文件信息,例如:
# Get the File path:
$FilePath = $args
# Get the complete file name:
$file_name_complete = [System.IO.Path]::GetFileName("$FilePath")
# Get File Name Without Extension:
$fileNameOnly = [System.IO.Path]::GetFileNameWithoutExtension("$FilePath")
# Get the Extension:
$fileExtensionOnly = [System.IO.Path]::GetExtension("$FilePath")
Don't forget the quote around $FilePath
.
不要忘记周围的报价$FilePath
。
Then you can use it to divide audio filesin 5min parts simply like this:
然后您可以使用它来将音频文件分成 5 分钟,就像这样:
ffmpeg -i $file_name_complete -f segment -segment_time 300 -c copy $fileNameOnly"_"%03d$fileExtensionOnly #