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

问题描述 投票:0回答:1

我需要一些在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"

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

windows batch-file
1个回答
0
投票

以下是这些类型变量的可能替换。 (注意:一个%在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或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" 
)

</ EDIT>

<编辑:替换/删除多个字符的示例>

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

只需在将fileName设置为%%~na后添加此项。

</ EDIT>

© www.soinside.com 2019 - 2024. All rights reserved.