Batch file ffmpeg编码器的.bat脚本中的字符串输入

Batch file ffmpeg编码器的.bat脚本中的字符串输入,batch-file,ffmpeg,Batch File,Ffmpeg,好的,我有一个脚本,它用ffmpeg进行批量编码。它基本上为每个文件调用ffmpeg。我想做的是让脚本在开始执行之前请求输入和输出位置 以下是原始脚本: for %%a in ("*.mp4") do "G:\ffmpeg\bin\ffmpeg" -i "%%a" -c:v libx264 -preset slow -crf 20 -maxrate 15000k -bufsize 2000k -acodec copy "newfiles\%%~na.mp4" 现在,我希望能够输入一个字符串,该

好的,我有一个脚本,它用ffmpeg进行批量编码。它基本上为每个文件调用ffmpeg。我想做的是让脚本在开始执行之前请求输入和输出位置

以下是原始脚本:

for %%a in ("*.mp4") do "G:\ffmpeg\bin\ffmpeg" -i "%%a" -c:v libx264 -preset slow -crf 20 -maxrate 15000k -bufsize 2000k -acodec copy "newfiles\%%~na.mp4"
现在,我希望能够输入一个字符串,该字符串的第一个括号(“*.mp4”)中包含文件的位置,最后是“newfiles\%%~na.mp4”

我想%%~na是用来生成数字订单之类的,但我不能完全确定

我假设需要创建两个变量/字符串,然后通过从键盘读取来使用它们。遗憾的是,我完全没有这方面的经验。在学校做了一些C++,但那是。
提前感谢。

您可以向用户查询文件夹名称,并将其添加到前面,如下所示:

@echo off
set /p fromfolder="Enter search folder: "
set /p tofolder="Enter destination folder: "
for %%a in ("%fromfolder%\*.mp4") do "G:\ffmpeg\bin\ffmpeg" -i "%%a" -c:v libx264 -preset slow -crf 20 -maxrate 15000k -bufsize 2000k -acodec copy "%tofolder%\%%~na.mp4"
%~nI
(其中
I
是变量,在您的情况下是
a
)将允许执行以下操作:
仅将%I扩展为文件名
(有效地从路径的文件名部分删除文件扩展名)。在.BAT文件中使用时,需要使用
%%
而不是
%%

如果键入
for/?
,您将获得for命令的帮助。它将解释如何使用该命令,以及如何使用变量替换。此处包含/?的部分内容,以便于参考:

In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax: %~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 The modifiers can be combined to get compound results: %~dpI - expands %I to a drive letter and path only %~nxI - expands %I to a file name and extension only %~fsI - expands %I to a full path name with short names only %~dp$PATH:I - searches the directories listed in the PATH environment variable for %I and expands to the drive letter and path of the first one found. %~ftzaI - expands %I to a DIR like output line In the above examples %I and PATH can be replaced by other valid values. The %~ syntax is terminated by a valid FOR variable name. Picking upper case variable names like %I makes it more readable and avoids confusion with the modifiers, which are not case sensitive. 执行为:
script.bat“folder1”或“folder2”

文件夹名称周围的“”仅在包含空格的情况下才需要

@echo off
set fromfolder=%1
set tofolder=%2

REM Remove "" from folder names
set fromfolder=%fromfolder:"=%
set tofolder=%tofolder:"=%

REM Ask user if cmd line parameters not provided
IF [%fromfolder%] == [] set /p fromfolder="Enter source folder: "
IF [%tofolder%] == [] set /p tofolder="Enter destination folder: "

echo Source folder: %fromfolder%
echo Destination folder: %tofolder%

REM Execute command
for %%a in ("%fromfolder%\*.mp4") do "G:\ffmpeg\bin\ffmpeg" -i "%%a" -c:v libx264 -preset slow -crf 20 -maxrate 15000k -bufsize 2000k -acodec copy "%tofolder%\%%~na.mp4"

REM Unset
set fromfolder=
set tofolder=