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

PowerShell中传递ffmpeg参数变量报错:输出文件无流

问题

在PowerShell中尝试将ffmpeg命令参数存入变量后执行,代码如下:

## DOSSIERS DE TRAVAIL
$Ingest = "C:\INGEST\" ## DOSSIER WATCHE
$outPath = "C:\TRANSCODE\" ## DOSSIER DE DESTINATION, A definir
$TEMP = "C:\temp\" ## dossier temporaire de récupération des essences (de-wrapp fichier ingesté) 

$MAPaudio = ""
$Filesaudio = ""

for ($i = 1; $i -le $nombreAudios; $i++) {
    $TEMP_AUDIO = $TEMP + "audio" + $i + ".wav"
    ##Extraction de l'audio et conversion en 48000hz (Obligatoire pour le MXF)
    $MAPaudio += " -map " + $i + ":a "
    $Filesaudio += " -i " + $TEMP_AUDIO
}

$TEMP_VIDEO = $TEMP + "video.mxf"

write-host $MAPaudio
write-host $Filesaudio
##Encapsulage de la vidéo
$result = " -i $TEMP_VIDEO $Filesaudio -c copy -map 0:v:0 $MAPaudio -y c:\transcode\test.mxf".tostring()
  
ffmpeg "$result"

执行后出现错误:

Output #0, mxf, to ' -i C:\temp\video.mxf  -i C:\temp\audio1.wav -i C:\temp\audio2.wav -i C:\temp\audio3.wav -i C:\temp\audio4.wav -c copy -map 0:v:0  -map 1:a  -map 2:a  -map 3:a  -map 4:a  -y c:\transcode\test.mxf':
Output file #0 does not contain any stream

直接在非PowerShell环境下用相同参数调用ffmpeg能正常运行,请问原因及解决方法?

原因

你把所有参数拼成单个字符串传给ffmpeg时,PowerShell会将这个完整字符串作为单个参数传递给程序。ffmpeg会误把这个字符串识别为输出文件路径,而非命令参数集合——这就是错误提示里把整个参数串当成输出目标的原因,自然找不到有效音视频流。

解决方法

方法1:用数组存储参数(推荐)

PowerShell调用外部程序时,最佳实践是用数组存储每个独立参数,这样PowerShell会正确拆分参数传递给ffmpeg:

## DOSSIERS DE TRAVAIL
$Ingest = "C:\INGEST\"
$outPath = "C:\TRANSCODE\"
$TEMP = "C:\temp\"

# 初始化参数数组
$ffmpegArgs = @()

# 添加视频输入参数
$ffmpegArgs += "-i", "$TEMP\video.mxf"

# 循环添加音频输入参数
for ($i = 1; $i -le $nombreAudios; $i++) {
    $TEMP_AUDIO = "$TEMP\audio$i.wav"
    $ffmpegArgs += "-i", $TEMP_AUDIO
}

# 添加编码、映射规则和输出参数
$ffmpegArgs += "-c", "copy"
$ffmpegArgs += "-map", "0:v:0"
for ($i = 1; $i -le $nombreAudios; $i++) {
    $ffmpegArgs += "-map", "$i:a"
}
$ffmpegArgs += "-y", "c:\transcode\test.mxf"

# 执行ffmpeg
ffmpeg $ffmpegArgs

方法2:使用Invoke-Expression(不推荐,有安全风险)

如果坚持用字符串拼接方式,可通过Invoke-Expression让PowerShell解析完整命令串:

$result = "ffmpeg -i $TEMP_VIDEO $Filesaudio -c copy -map 0:v:0 $MAPaudio -y c:\transcode\test.mxf"
Invoke-Expression $result

注意:这种方式如果参数包含用户可控内容,容易引发命令注入风险,仅在完全可信的环境下使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 04:50:27