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

使用FFmpeg拼接文件时遇“unknown keyword 'file'”错误求助

FFmpeg concat报错:Line 1: unknown keyword 'file' 问题排查与解决

报错信息

Line 1: unknown keyword 'file'
[in#0 @ 000001b09b25e840] Error opening input: Invalid data found when processing input
Error opening input file mylist.txt.
Error opening input files: Invalid data found when processing input

场景与代码

通过PowerShell生成文件列表后执行FFmpeg拼接命令,列表文件格式看似合规,但触发上述报错。相关代码如下:

$clips = Get-ChildItem -Path Y:\****\Test -File -Filter "*.mp4"
$regex = "(?:\w+)-(?:[i]+-)?(?<scene_id>\d+)-(?<clip_id>\d+)-(?:\w+)"
$groupedScenes = $clips | Group-Object {[regex]::Match($_.Name, $regex).Groups["scene_id"].value}
foreach($scene in $groupedScenes)
{
    $sortedScene = $scene.Group | Sort-Object {[regex]::Match($_.Name, $regex).Groups["clip_id"].value -as [int]} 
    foreach($i in $sortedScene) 
    {
        "file 'Y:\*****\Test\$i'" | Out-File mylist.txt -Encoding utf8 -Append
    }
    .\ffmpeg.exe -f concat -safe 0 -i mylist.txt -map 0 -c:v hevc_amf -quality quality -rc cqp -qp_p 26 -qp_i 26 -c:a aac -b:a 128K -y Y:\*****\Test\Procssed\$sortedScene.mp4
    Remove-Item mylist.txt
}

问题原因

  1. 编码问题:PowerShell的Out-File -Encoding utf8默认生成UTF-8 with BOM格式的文件,FFmpeg的concat demuxer无法正确解析带BOM的文件,开头的BOM字节会被识别为无效字符,导致第一行的file关键字被误判为未知内容。
  2. 文件名错误:输出文件名$sortedScene.mp4会生成无效名称($sortedScene是文件对象数组,会被转为System.IO.FileInfo[])。

解决方法

1. 生成UTF-8无BOM格式的列表文件

  • PowerShell 6+版本:直接修改编码参数为utf8NoBOM,并在循环前覆盖创建文件避免追加残留:
    foreach($scene in $groupedScenes)
    {
        $sortedScene = $scene.Group | Sort-Object {[regex]::Match($_.Name, $regex).Groups["clip_id"].value -as [int]} 
        # 先创建空文件覆盖旧内容
        Out-File mylist.txt -Encoding utf8NoBOM -Force
        foreach($i in $sortedScene) 
        {
            "file 'Y:\*****\Test\$i'" | Out-File mylist.txt -Encoding utf8NoBOM -Append
        }
        # 执行FFmpeg命令...
    }
    
  • PowerShell 5.1版本:使用.NET方法直接写入UTF-8无BOM内容:
    foreach($scene in $groupedScenes)
    {
        $sortedScene = $scene.Group | Sort-Object {[regex]::Match($_.Name, $regex).Groups["clip_id"].value -as [int]} 
        # 清空旧文件
        if (Test-Path mylist.txt) { Remove-Item mylist.txt }
        foreach($i in $sortedScene) 
        {
            [System.IO.File]::AppendAllText("mylist.txt", "file 'Y:\*****\Test\$i`n'", [System.Text.Encoding]::UTF8)
        }
        # 执行FFmpeg命令...
    }
    

2. 修正输出文件名

将输出文件名改为场景ID命名,避免无效文件名:

.\ffmpeg.exe -f concat -safe 0 -i mylist.txt -map 0 -c:v hevc_amf -quality quality -rc cqp -qp_p 26 -qp_i 26 -c:a aac -b:a 128K -y "Y:\*****\Test\Procssed\$($scene.Name).mp4"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 00:26:16