簡體   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