繁体   English   中英

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

[英]FFMPEG - How to get exactly file name and add a drawtext in each output video of a rendering queue

我需要一些在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:\\FFMPEG\\BIN\\The.Input.Video.MP4 ”(它包含文件路径,“。”和mp4后缀)。 如何删除它们并将文件名仅作为“输入视频”。 非常感谢。

这是这些类型的变量的可能替代品。 (注意:1 %以cmd为单位,在批处理文件中,您需要2 %%

%~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或delayedExpansion。

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

@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:[= !"

只需在将fileName设置为%%~na 〜na之后添加即可。

</编辑>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM