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

PowerShell拼接AVI文件脚本报错,同类脚本正常求排查

问题:PowerShell调用FFmpeg拼接AVI文件失败

报错的PowerShell脚本

# Set the folder path where the AVI files are located
$folderPath = "C:\Users\HomePC\hswlab\Desktop\videos\eyeblink\022223\2023_04_24\16_15_23\My_WebCam"

# Get all AVI files in the folder
$aviFiles = Get-ChildItem -Path $folderPath -Filter "*.avi" | Sort-Object Name

# Check if there are at least 2 AVI files for concatenation
if ($aviFiles.Count -ge 2) {
    $videoFiles = $aviFiles.FullName

    Write-Host "Input Files for Concatenation:"
    foreach ($file in $aviFiles) {
        Write-Host $file.Name
    }

    $outputFile = Join-Path -Path $folderPath -ChildPath "concatenated.avi"

    # Create the FFmpeg command for concatenation
    $concatArguments = "-f", "concat", "-i", "`"concat:$videoFiles`"", "-c", "copy", "`"$outputFile`""

    $command = "ffmpeg $concatArguments"
    Write-Host "FFmpeg Command: $command"

    try {
        $process = Start-Process -FilePath "ffmpeg" -ArgumentList $concatArguments -NoNewWindow -PassThru -Wait -ErrorAction Stop
        Write-Host "Concatenation complete. Output file: $outputFile"
    } catch {
        Write-Host "Error occurred while executing FFmpeg command:"
        Write-Host "Error message: $($_.Exception.Message)"
    }
} else {
    Write-Host "Not enough AVI files found in the folder for concatenation."
}

报错输出

Input Files for Concatenation:
webcam0.avi
webcam1.avi
FFmpeg Command: ffmpeg -f concat -i "concat:C:\Users\HomePC\hswlab\Desktop\videos\eyeblink\022223\2023_04_24\16_15_23\My_WebCam\webcam0.avi C:\Users\HomePC\hswlab\Desktop\videos\eyeblink\022223\2023_04_24\16_15_23\My_WebCam\webcam1.avi" -c copy "C:\Users\HomePC\hswlab\Desktop\videos\eyeblink\022223\2023_04_24\16_15_23\My_WebCam\concatenated.avi"
ffmpeg version 2023-06-27-git-9b6d191a66-essentials_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 12.2.0 (Rev10, Built by MSYS2 project)
  configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-sdl2 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libvpl --enable-libgme --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvo-amrwbenc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-librubberband
  libavutil      58. 13.101 / 58. 13.101
  libavcodec     60. 21.100 / 60. 21.100
  libavformat    60.  9.100 / 60.  9.100
  libavdevice    60.  2.100 / 60.  2.100
  libavfilter     9.  8.102 /  9.  8.102
  libswscale      7.  3.100 /  7.  3.100
  libswresample   4. 11.100 /  4. 11.100
  libpostproc    57.  2.100 / 57.  2.100
[in#0 @ 000001d2f05f1b00] Error opening input: Invalid argument
Concatenation complete. Output file: C:\Users\HomePC\hswlab\Desktop\videos\eyeblink\022223\2023_04_24\16_15_23\My_WebCam\concatenated.avi

可正常运行的对比脚本

$file1 = "C:\Users\HomePC\hswlab\Desktop\videos\eyeblink\022223\2023_04_24\16_15_23\My_WebCam\webcam0.avi"       # Path to the first video file
$file2 = "C:\Users\HomePC\hswlab\Desktop\videos\eyeblink\022223\2023_04_24\16_15_23\My_WebCam\webcam1.avi"       # Path to the second video file
$outputFile = "C:\Users\HomePC\hswlab\Desktop\videos\eyeblink\022223\2023_04_24\16_15_23\My_WebCam\concat.avi"  # Path to the output concatenated video file

# Create a temporary batch script
$batchScriptPath = [System.IO.Path]::GetTempFileName() + ".bat"
@'
@echo off
ffmpeg -i "%~1" -i "%~2" -filter_complex "[0:v][1:v]concat=n=2:v=1[outv]" -map "[outv]" "%~3"
'@ | Set-Content -Path $batchScriptPath

# Execute the temporary batch script
try {
    & $batchScriptPath $file1 $file2 $outputFile
    Write-Host "Concatenation complete. Output file: $outputFile"
} catch {
    Write-Host "Error occurred while executing FFmpeg command:"
    Write-Host $_.Exception.Message
}

# Remove the temporary batch script
Remove-Item -Path $batchScriptPath -Force

问题原因与修复方案

问题1:concat:协议使用错误

第一个脚本采用的concat:伪协议拼接方式,要求多文件路径用|分隔而非空格,且Windows环境下该方式对AVI容器支持不稳定,易触发输入错误。

问题2:参数引号处理错误

构建参数时的""concat:$videoFiles""会导致FFmpeg收到的路径被错误包裹,加上$videoFiles`作为数组直接拼接成空格分隔的字符串,完全不符合FFmpeg语法要求。

修复后的脚本(推荐使用FFmpeg官方concat demuxer)

该方式无需重新编码,速度更快且兼容性更好:

# Set the folder path where the AVI files are located
$folderPath = "C:\Users\HomePC\hswlab\Desktop\videos\eyeblink\022223\2023_04_24\16_15_23\My_WebCam"

# Get all AVI files in the folder
$aviFiles = Get-ChildItem -Path $folderPath -Filter "*.avi" | Sort-Object Name

# Check if there are at least 2 AVI files for concatenation
if ($aviFiles.Count -ge 2) {
    Write-Host "Input Files for Concatenation:"
    foreach ($file in $aviFiles) {
        Write-Host $file.Name
    }

    $outputFile = Join-Path -Path $folderPath -ChildPath "concatenated.avi"
    # 创建临时文件列表,用于FFmpeg concat demuxer
    $listFile = Join-Path -Path $env:TEMP -ChildPath "avi_list.txt"
    # 写入文件路径(用单引号包裹避免空格问题)
    $aviFiles.FullName | ForEach-Object { "file '$_'" } | Set-Content -Path $listFile -Encoding ASCII

    # 构建FFmpeg参数数组,避免引号解析问题
    $concatArguments = "-f", "concat", "-safe", "0", "-i", $listFile, "-c", "copy", $outputFile

    Write-Host "FFmpeg Command: ffmpeg $($concatArguments -join ' ')"

    try {
        $process = Start-Process -FilePath "ffmpeg" -ArgumentList $concatArguments -NoNewWindow -PassThru -Wait -ErrorAction Stop
        # 检查FFmpeg退出码判断是否成功
        if ($process.ExitCode -eq 0) {
            Write-Host "Concatenation complete. Output file: $outputFile"
        } else {
            Write-Host "FFmpeg执行失败,退出码: $($process.ExitCode)"
        }
    } catch {
        Write-Host "执行FFmpeg命令时出错:"
        Write-Host "错误信息: $($_.Exception.Message)"
    } finally {
        # 清理临时文件
        Remove-Item -Path $listFile -Force -ErrorAction SilentlyContinue
    }
} else {
    Write-Host "文件夹中AVI文件数量不足,无法拼接。"
}

关键修改点

  1. 使用concat demuxer:通过临时文本文件列出所有要拼接的视频路径,是FFmpeg官方推荐的可靠拼接方式
  2. 移除错误的引号包裹:直接传递参数数组给Start-Process,避免PowerShell与FFmpeg之间的引号解析冲突
  3. 添加-safe 0参数:允许FFmpeg使用绝对路径
  4. 检查FFmpeg退出码:更准确判断拼接是否成功
  5. 自动清理临时文件:避免残留垃圾文件

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 15:28:07