在PowerShell中,如何对这个FFmpeg命令的文件路径进行转义?
在PowerShell中使用FFmpeg将 .ASS字幕文件添加到MP4文件时遇到问题:
# Function to add ASS subtitles to video
function Add-Subtitles-To-Video {
param ($videoPath, $assPath, $outputPath)
try {
Write-Host "`nAdding subtitles to video..." -ForegroundColor Cyan
Write-Host "videopath: $videoPath"
Write-Host "assPath: $assPath"
Write-Host "outputPath: $outputPath"
# Debug: Check if ASS file exists and has content
if (Test-Path $assPath) {
Write-Host "ASS file size: $([math]::Round((Get-Item $assPath).Length / 1KB, 2)) KB" -ForegroundColor Gray
Write-Host "ASS file first few lines:" -ForegroundColor Gray
Get-Content $assPath | Select-Object -First 5 | ForEach-Object { Write-Host " $_" -ForegroundColor Gray }
} else {
Write-Host "ASS file not found at temp location: $assPath" -ForegroundColor Red
return $false
}
$assPathFixed = $assPath -replace '\\', '/'
$ffmpegArgs = @(
"-i", $videoPath,
"-vf", "ass='$assPathFixed'",
"-c:a", "copy",
"-y", $outputPath
)
Write-Host "Running: ffmpeg $ffmpegArgs"
$errorLogPath = "ffmpeg_sub_error.log"
$outputLogPath = "ffmpeg_sub_output.log"
$process = Start-Process -FilePath "ffmpeg" -ArgumentList $ffmpegArgs -NoNewWindow -Wait -RedirectStandardError $errorLogPath -RedirectStandardOutput $outputLogPath -PassThru
if (($process.ExitCode -eq 0 -or (Test-Path $outputPath)) -and (Test-Path $outputPath)) {
Write-Host "Subtitles added successfully: $outputPath" -ForegroundColor Green
# Clean up temp ASS file
Remove-Item $assPath -ErrorAction SilentlyContinue
Remove-Item $errorLogPath -ErrorAction SilentlyContinue
Remove-Item $outputLogPath -ErrorAction SilentlyContinue
return $true
} else {
Write-Host "Failed to add subtitles. Exit code: $($process.ExitCode)" -ForegroundColor Red
# Show both error and output logs
if (Test-Path $errorLogPath) {
$errorContent = Get-Content $errorLogPath -Raw
if ($errorContent.Trim() -ne "") {
Write-Host "FFmpeg STDERR:" -ForegroundColor Red
Write-Host $errorContent -ForegroundColor Red
}
}
if (Test-Path $outputLogPath) {
$outputContent = Get-Content $outputLogPath -Raw
if ($outputContent.Trim() -ne "") {
Write-Host "FFmpeg STDOUT:" -ForegroundColor Yellow
Write-Host $outputContent -ForegroundColor Yellow
}
}
# Clean up log files
Remove-Item $errorLogPath -ErrorAction SilentlyContinue
Remove-Item $outputLogPath -ErrorAction SilentlyContinue
# Clean up temp ASS file
Remove-Item $assPath -ErrorAction SilentlyContinue
return $false
}
} catch {
Write-Host "Error adding subtitles: $_" -ForegroundColor Red
return $false
}
}
问题出现在这一部分:
$assPathFixed = $assPath -replace '\\', '/'
$ffmpegArgs = @(
"-i", $videoPath,
"-vf", "ass='$assPathFixed'",
"-c:a", "copy",
"-y", $outputPath
)
错误:
Adding subtitles to video...
videopath: C:\Users\X\AppData\Local\Temp\mixed_video.mp4
assPath: C:\Users\X\AppData\Local\Temp\subtitles.ass
outputPath: D:\Videos\Trivia\Script\content\categories\Art\testvideo_final.mp4
ASS file size: 0.66 KB
ASS file first few lines:
[Script Info]
Title: Subtitles
ScriptType: v4.00+
[V4+ Styles]
Running: ffmpeg -i C:\Users\X\AppData\Local\Temp\mixed_video.mp4 -vf ass='C:/Users/X/AppData/Local/Temp/subtitles.ass' -c:a copy -y D:\Videos\Trivia\Script\content\categories\Art\testvideo_final.mp4
Failed to add subtitles. Exit code: -22
FFmpeg STDERR:
ffmpeg version 8.0.1-essentials_build-www.gyan.dev Copyright (c) 2000-2025 the FFmpeg developers
built with gcc 15.2.0 (Rev8, 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-dxva2 --enable-d3d11va --enable-d3d12va --enable-ffnvcodec --enable-libvpl --enable-nvdec --enable-nvenc --enable-vaapi --enable-openal --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 60. 8.100 / 60. 8.100
libavcodec 62. 11.100 / 62. 11.100
libavformat 62. 3.100 / 62. 3.100
libavdevice 62. 1.100 / 62. 1.100
libavfilter 11. 4.100 / 11. 4.100
libswscale 9. 1.100 / 9. 1.100
libswresample 6. 1.100 / 6. 1.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'C:\Users\X\AppData\Local\Temp\mixed_video.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf62.3.100
Duration: 00:00:08.00, start: 0.000000, bitrate: 2251 kb/s
Stream #0:0[0x1](und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 129 kb/s (default)
Metadata:
handler_name : SoundHandler
vendor_id : [0][0][0][0]
Stream #0:1[0x2](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 720x1088, 2128 kb/s, 24 fps, 24 tbr, 12288 tbn (default)
Metadata:
handler_name : VideoHandler
vendor_id : [0][0][0][0]
encoder : Lavc62.11.100 libx264
[Parsed_ass_0 @ 0000018dbe6300c0] Unable to parse option value "/Users/X/AppData/Local/Temp/subtitles.ass" as image size
[fc#-1 @ 0000018dbe430700] Error applying option 'original_size' to filter 'ass': Invalid argument
Error opening output file D:\Videos\Trivia\Script\content\categories\Art\testvideo_final.mp4.
Error opening output files: Invalid argument
Error adding subtitles: You cannot call a method on a null-valued expression.
为了让.ass文件被加载到脚本中,我该如何对其进行转义?
解决方案
已解决:
# Normalize ASS path separators for Windows
$assPath = $assPath -replace '/', '\\'
# Escape characters for ffmpeg filter syntax: backslashes and colons
$ffmpegAssPath = $assPath -replace '\\', '\\\\' -replace ':', '\:'
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。