You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何PowerShell向ffmpeg传递参数时首个连字符被移除?

问题描述

我写了一段合并MP4的简化PowerShell脚本:

[string]$inputs = ""
$filenames = "input1.mp4", "input2.mp4", "input3.mp4"
foreach ($f in $filenames) {
    $inputs += "-i $f "
}
Write-Host $inputs
.\bin\ffmpeg.exe $inputs -filter_complex...

Write-Host输出的字符串符合预期,但ffmpeg报错:

Unrecognized option 'i input1.mp4 -i input2.mp4 -i input3.mp4 '.
Error splitting the argument list: Option not found

看起来参数传递给ffmpeg时首个“-”丢失了,后续的“-”能正常传递。另外用Start-Process的-ArgumentList参数就能正常运行,想知道原因。

问题原因

问题出在PowerShell传递外部程序参数的逻辑上:

  • 直接把拼接好的字符串$inputs传给ffmpeg时,PowerShell会把整个字符串当成单个完整参数发给ffmpeg。ffmpeg拿到这个参数后,会把它当作一个选项名来解析,自然找不到开头缺了-的i input1.mp4 -i...这个选项,所以报错。
  • 而Start-Process的-ArgumentList参数会自动按规则拆分字符串(比如识别带空格的路径引号),把拆分后的每一项作为独立参数传给ffmpeg,相当于你手动挨个传-i input1.mp4、-i input2.mp4这些参数,所以能正常运行。
正确写法

不要用字符串拼接参数,改用数组存储每个独立参数:

$inputs = @()
$filenames = "input1.mp4", "input2.mp4", "input3.mp4"
foreach ($f in $filenames) {
    $inputs += "-i", $f
}
.\bin\ffmpeg.exe $inputs -filter_complex...

这样PowerShell会把数组里的每个元素作为独立参数传给ffmpeg,和Start-Process -ArgumentList的效果一致,能正确被ffmpeg解析。

内容的提问来源于stack exchange,提问作者robynChill

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 18:10:36