FFMPEG - 如何获取准确的文件名并在渲染队列的每个输出视频中添加绘制文本



我需要一些关于在Windows中运行的FFMPEG代码的帮助:

@ECHO OFF
Setlocal EnableDelayedExpansion
Set INPUT=D:In
Set OUTPUT=D:Out
for %%a in ("%INPUT%*.*") DO ffmpeg -i "%%a" -vf "drawtext=text=${%%a}:x=105:y=120:fontfile=font/impact.ttf:fontsize=25:fontcolor=white" -vcodec libx264 -pix_fmt yuv420p -r 30 -g 60 -b:v 2000k -acodec libmp3lame -b:a 128k -ar 44100 -preset ultrafast "%OUTPUT%/%%~na.mp4"

INPUT文件夹中有一些视频文件,例如The.Input.Video.mp4,我想创建添加了文件名文本的输出视频,所以我使用drawtext=text=${%%a}.问题是,每个视频的接收文本显示为"D:FFMPEGBINThe.Input.Video.MP4"(它包含文件路径,"."和mp4后缀(。如何删除它们并仅将文件名设置为"输入视频"。多谢。

以下是这些类型变量的可能替换。(注意:一个%在cmd中,在批处理文件中需要两个%%(

%~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 the file.
%~tI    Expands %I to date/time of the file.
%~zI    Expands %I to size of the 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 uppercase variable names like %I makes it more readable and avoids confusion with the modifiers, which are not case sensitive.

因此,您只需要获取文件名即可%%~na

如果你还想用空格替换所有.,你需要将名称存储到一个正则变量(set "fileName=%%~na"(,然后像这样使用它:%fileName:.= %.因为你想在for循环中做到这一点,所以你必须使用call或delayedExpand。

<编辑:添加了完整的示例>

@ECHO OFF&Setlocal EnableDelayedExpansion
Set INPUT=D:In
Set OUTPUT=D:Out
for %%a in ("%INPUT%*.*") DO ( 
set "filename=%%~na"
ffmpeg -i "%%a" -vf "drawtext=text=!fileName:.= !:x=105:y=120:fontfile=font/impact.ttf:fontsize=25:fontcolor=white" -vcodec libx264 -pix_fmt yuv420p -r 30 -g 60 -b:v 2000k -acodec libmp3lame -b:a 128k -ar 44100 -preset ultrafast "%OUTPUT%/%%~na.mp4" 
)

set "fileName=!fileName:.= !"
set "fileName=!fileName:-= !"
set "fileName=!fileName:[= !"

只需在将文件名设置为%%~na后添加此内容即可。

最新更新