从批处理脚本插件调用 ffmeg

从批处理脚本插件调用 ffmeg

我正在开发一个可以快速缩小图像的批处理插件。这个想法是,您只需将图像拖放到 上.bat,它就会缩小图像。我正在调用ffmpeg它,但我对 的路径有问题ffmpeg。事实上,我不想ffmpeg在我的计算机上调用它,而是在本地,在插件的文件夹中调用它。

以下是我尝试过的:

set ffmpegpath=..\ffmpeg\bin\ffmpeg.exe

set inputPath=%1

set inputFolder=%1\..

set image_name=%~n1

set resolution=512

if exist "%1.png" goto:png
if exist "%1.jpg" goto:jpg
if exist "%1.jpeg" goto:jpeg

:png
set image_extension=png
goto:done

:jpg
set image_extension=jpg
goto:done

:jpeg
set image_extension=jpeg
goto:done

:done
echo image_extension is %image_extension%



%ffmpegpath% -i %1 -vf scale=%resolution%:%resolution% %image_name%_reduced.%image_extension%


pause

我搜索了互联网并尝试了多种组合,但仍然收到错误消息,提示系统未找到路径。我做错了什么?

答案1

为了引用批处理文件的文件路径,可以使用%~dp0变量。

这些是在可能不太明显的地方描述的;循环的文档forfor /?)。

在此文档中I可以用参数编号替换。参数0始终包含批处理文件的路径,包括文件名。

%~I         - expands %I removing any surrounding quotes (")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file
%~$PATH:I   - searches the directories listed in the PATH
               environment variable and expands %I to the
               fully qualified name of the first one found.
               If the environment variable name is not
               defined or the file is not found by the
               search, then this modifier expands to the
               empty string

因此,就您而言,您应该能够使用:

set ffmpegpath=%~dp0..\ffmpeg\bin\ffmpeg.exe

如果路径中有空格,调用它时用引号括起来也是很好的做法:

"%ffmpegpath%" -i %1 -vf scale=%resolution%:%resolution% %image_name%_reduced.%image_extension%

相关内容