在Bash脚本中调用ffmpeg.exe失败的问题求助
在Bash脚本中调用Windows下ffmpeg.exe失败的解决办法
问题场景
我写了track_cmd.sh脚本,想调用Windows路径下的ffmpeg.exe,通过命令提示符执行bash track_cmd.sh运行脚本,但所有尝试都失败了。
原脚本内容
echo "try1" C:\Users\marti\Documents\projects\jan2023-rendertune\RenderTune\node_modules\ffmpeg-ffprobe-static\ffmpeg.exe echo "try2" # start /d "path" file.exe start /d "C:\Users\marti\Documents\projects\jan2023-rendertune\RenderTune\node_modules\ffmpeg-ffprobe-static" ffmpeg.exe echo "try3" # start "c:\windows\system32" notepad.exe start "C:\Users\marti\Documents\projects\jan2023-rendertune\RenderTune\node_modules\ffmpeg-ffprobe-static" ffmpeg.exe echo "try4" #start "" fullPath/file.exe start "" C:\\Users\\marti\\Documents\\projects\\jan2023-rendertune\\RenderTune\\node_modules\\ffmpeg-ffprobe-static\\ffmpeg.exe echo "done"
执行后的错误输出
>bash track_cmd.sh try1 track_cmd.sh: line 2: C:UsersmartiDocumentsprojectsjan2023-rendertuneRenderTunenode_modulesffmpeg-ffprobe-staticffmpeg.exe: command not found try2 track_cmd.sh: line 5: start: command not found try3 track_cmd.sh: line 8: start: command not found try4 track_cmd.sh: line 11: start: command not found done
直接在Windows命令提示符里执行该ffmpeg的完整路径能正常运行,说明路径本身是有效的。
错误原因
- 路径格式不兼容:Bash是类Unix环境,会把反斜杠
\当成转义字符,原脚本里的C:\Users\...会被解析成C:Users...(反斜杠直接消失),导致路径完全错误,找不到程序。 start命令不被Bash识别:start是Windows CMD独有的命令,Bash环境里没有这个命令,自然会报错“command not found”。
解决方案
方案1:用Bash兼容的路径直接调用
把路径里的反斜杠\换成正斜杠/,或者用双反斜杠\\(避免被转义),如果路径包含空格,记得用引号把整个路径包起来。
修改后的脚本片段:
echo "try1-fixed" # 用正斜杠的写法(Bash中C盘可简写为/c/) "/c/Users/marti/Documents/projects/jan2023-rendertune/RenderTune/node_modules/ffmpeg-ffprobe-static/ffmpeg.exe" # 或者用双反斜杠的写法 # "C:\\Users\\marti\\Documents\\projects\\jan2023-rendertune\\RenderTune\\node_modules\\ffmpeg-ffprobe-static\\ffmpeg.exe" echo "done"
方案2:通过cmd /c调用Windows CMD命令
如果需要用start命令(比如让ffmpeg在新窗口运行、后台执行),可以在Bash里用cmd /c来执行Windows的CMD命令,这样就能兼容start的语法:
echo "try-start-fixed" # 注意:start命令的第一个引号是窗口标题,必须保留,空字符串就行 cmd /c start "" "C:\Users\marti\Documents\projects\jan2023-rendertune\RenderTune\node_modules\ffmpeg-ffprobe-static\ffmpeg.exe" echo "done"
完整修改后的脚本示例
echo "try1-fixed" "/c/Users/marti/Documents/projects/jan2023-rendertune/RenderTune/node_modules/ffmpeg-ffprobe-static/ffmpeg.exe" echo "try2-fixed" cmd /c start "" "/c/Users/marti/Documents/projects/jan2023-rendertune/RenderTune/node_modules/ffmpeg-ffprobe-static/ffmpeg.exe" echo "done"
内容的提问来源于stack exchange,提问作者Martin
相关产品推荐
相关产品推荐

