如何为ffmpeg创建Powershell脚本或Windows .bat文件 [英] How to create a powershell script / or windows .bat file for ffmpeg
问题描述
因此,我想创建一个Powershell或Batch文件(最适合该任务的文件),我可以将其粘贴到文件夹中,并在执行后将开始使用FFmpeg编码所有视频
So, I want to create a Powershell or Batch file (whichever works best for the task), that I can paste in a folder and after executing it, will start to use FFmpeg to encode all videos
有效的部分(在.bat中):
The part that works (in .bat):
for %%a in ("*.mkv") do(
ffmpeg -i "%%a" -c:v libx265 -c:a copy -x265-params crf=25 "%%a [encoded].mkv"
pause)
这可行,但是在某些文件夹中,我已经有[已编码]文件.我想让脚本跳过这些文件.我在reddit中询问并收到以下脚本:
This works, but in some folders, I already have [encoded] files. I'd like for the script to skip these files. I asked in reddit and received the following script:
for %%a in ("*.mkv") do (
if not exist "%%a [encoded].mkv" ffmpeg -i "%%a" -c:v libx265 -c:a copy -x265-params crf=25 "%%a [encoded].mkv"
pause)
我尝试了该脚本,但是每个文件都进行了编码,即使其中包含[encoded]也是如此.在我指出这一点之后,他们告诉我,我可能应该切换到PowerShell,并提供指向StackOverflow问题的链接:
I tried the script, but every file got encoded, even if it had the [encoded] in it. After I pointed that out, they told me that I should probably switch to PowerShell and gave a link to a StackOverflow question:
您可能需要切换到PowerShell查找不包含所选字符串的文件然后在返回的内容中执行一次foreach,以对其执行ffmpeg命令.
You probably need to switch to PowerShell Find files which does not contains selected string Then do a foreach in what is returned to execute the ffmpeg command on it.
但是我不知道如何在PowerShell中编码(以前从未使用过).bat文件甚至可以工作?如果可以,我该怎么写?而且,如果PowerShell脚本是更好的选择,我该如何编写代码?
but I don't know how to code in PowerShell (never used it before) Would a .bat file even work? If yes, how could I write it? And if a PowerShell script would be the better option, how would I code it?
感谢您的帮助和解答.
推荐答案
在 PowerShell 中,您可以使用以下内容(可以将此代码按原样粘贴到PowerShell控制台窗口中):>
In PowerShell, you can use the following (you can paste this code as-is into a PowerShell console window):
Get-ChildItem *.mkv | where BaseName -notlike '*`[encoded]' | foreach {
ffmpeg -i $_ -c:v libx265 -c:a copy -x265-params crf=25 "$($_.BaseName) [encoded].mkv"
pause
}
-
Get-ChildItem * .mkv
使用Get-ChildItem
以获得当前目录中所有扩展名为(.mkv
)的文件的信息(您可以添加-File
来明确排除目录,但是带有扩展名的目录名称是).Get-ChildItem *.mkv
uses theGet-ChildItem
to get information about all files with extension (.mkv
) in the current directory (you could add-File
to explicitly rule out directories, but directory names with extensions are are).其中BaseName-类似于'*`[encoded]'
使用通配符,-like
/-notlike
运算符进行操作.where BaseName -notlike '*`[encoded]'
uses theWhere-Object
cmdlet (whose built-in alias iswhere
) to weed out files whose base name (the name without the filename extensions) already ends in[encoded]
. Note the`
before the opening[
in order to use it verbatim, because[
has special meaning in PowerShell's wildcard expressions, which the-like
/-notlike
operator operates on.foreach {...}
使用ForEach-Object
cmdlet(其内置别名为foreach
)对每个调用ffmpeg
感兴趣的文件:foreach { ... }
uses theForEach-Object
cmdlet (whose built-in alias isforeach
) to invokeffmpeg
for each file of interest:-
在脚本块(
{...}
)中,System.IO代表输入文件的.FileInfo
实例.
Inside the script block (
{ ... }
), the automatic$_
variable refers to the pipeline (|
) input object at hand, which in this case is aSystem.IO.FileInfo
instance representing an input file.
"$($ _.BaseName)[encoded] .mkv"
使用可扩展字符串 (字符串插值),可通过输入文件的基本名称($ _.BaseName
)导出输出文件名."$($_.BaseName) [encoded].mkv"
uses an expandable string (string interpolation) to derive the output file name via the input file's base name ($_.BaseName
).这篇关于如何为ffmpeg创建Powershell脚本或Windows .bat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-