[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$InputPath, [string]$OutputDir = 'hls', [ValidateRange(2, 30)] [int]$SegmentSeconds = 6, [ValidateRange(0, 51)] [int]$Crf = 20, [switch]$Force ) $ErrorActionPreference = 'Stop' if (-not (Get-Command ffmpeg -ErrorAction SilentlyContinue)) { throw 'ffmpeg не найден в PATH. Установите FFmpeg.' } if (-not (Test-Path -LiteralPath $InputPath -PathType Leaf)) { throw "Входной файл не найден: $InputPath" } $source = (Resolve-Path -LiteralPath $InputPath).Path $out = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($OutputDir) if (-not (Test-Path -LiteralPath $out)) { New-Item -ItemType Directory -Path $out -Force | Out-Null } $playlist = Join-Path $out 'index.m3u8' $oldSegments = @(Get-ChildItem -LiteralPath $out -Filter 'segment_*.ts' -File -ErrorAction SilentlyContinue) if ((-not $Force) -and ((Test-Path -LiteralPath $playlist) -or $oldSegments.Count -gt 0)) { throw 'В папке уже есть HLS-файлы. Добавьте -Force для перезаписи.' } $arguments = @( '-hide_banner', '-nostdin', '-y', '-i', $source, '-c:v', 'libx264', '-preset', 'medium', '-crf', "$Crf", '-pix_fmt', 'yuv420p', '-c:a', 'aac', '-b:a', '160k', '-f', 'hls', '-hls_time', "$SegmentSeconds", '-hls_playlist_type', 'vod', '-hls_segment_filename', (Join-Path $out 'segment_%03d.ts'), $playlist ) Write-Host "HLS: $source -> $playlist" & ffmpeg @arguments if ($LASTEXITCODE -ne 0) { throw "ffmpeg завершился с кодом $LASTEXITCODE" } $count = @(Get-ChildItem -LiteralPath $out -Filter 'segment_*.ts' -File).Count Write-Host "Готово: $playlist, сегментов: $count"