用于ffmpeg编码器的.bat脚本中的字符串输入

时间:2016-01-27 05:21:06

标签: 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"

现在我希望能够在第一个括号(“* .mp4”)中输入一个包含文件位置的字符串,在“newfiles \ %% ~na.mp4”的末尾输入一个字符串。

我认为%%〜na用于生成数字订单或其他东西,但我不完全确定。

我认为需要创建两个变量/字符串然后通过键盘读取来使用。可悲的是,我完全没有这方面的经验。学校里有一些c ++,但就是这样。

提前致谢。

1 个答案:

答案 0 :(得分:0)

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

@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)将执行此操作:expands %I to a file name only(从文件名部分有效删除文件扩展名的路径)。在.BAT文件中使用时,您需要使用%%而不是%

如果您输入for /?,您将获得for-command的帮助。它将解释如何使用该命令,以及如何使用变量替换。此处包含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.

我想指出,通常命令行命令需要参数而不是查询用户输入。您可以将此与要求用户输入相结合。

@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=

执行为:script.bat "folder1" "folder2"
""文件夹名称周围只有在包含空格的情况下才需要。