Skip to content

Instantly share code, notes, and snippets.

@serg-yalosovetsky
Created June 26, 2025 10:56
Show Gist options
  • Select an option

  • Save serg-yalosovetsky/b81f69baf5e323bf59f1acb0b0b0f4a5 to your computer and use it in GitHub Desktop.

Select an option

Save serg-yalosovetsky/b81f69baf5e323bf59f1acb0b0b0f4a5 to your computer and use it in GitHub Desktop.
compress gopro videos win
<#
Стискає усі *.mp4 у папці через NVIDIA HEVC (NVENC) і показує прогрес-бар.
Вихідні файли кладуться у підпапку .\compressed з суфіксом _nv.mp4
#>
# -------- Налаштування --------
$srcDir = "F:\Downloads\GoPro-1750887512"
$dstDir = Join-Path $srcDir 'compressed'
$cq = 25 # 24-27: менше число = краща якість, більший розмір
$preset = "p5" # p1 швидко, p7 якісно
# ------------------------------
New-Item -ItemType Directory -Path $dstDir -Force | Out-Null
$files = Get-ChildItem -Path $srcDir -Filter *.mp4
if (-not $files) { Write-Error "Файлів .mp4 не знайдено"; exit }
$i = 0
foreach ($f in $files) {
$i++
$outFile = Join-Path $dstDir "$($f.BaseName)_nv.mp4"
# --- тривалість відео (сек) ---
$durationSec = (& ffprobe -v error `
-show_entries format=duration `
-of default=noprint_wrappers=1:nokey=1 `
-- "$($f.FullName)").Trim() -as [double]
if (-not $durationSec) {
Write-Warning "Не можу визначити тривалість $($f.Name), пропускаю."; continue
}
$durationMs = [int64]($durationSec * 1000)
Write-Host "► [$i/$($files.Count)] $($f.Name)"
# --- запускаємо ffmpeg з прогрес-каналом ---
$psi = [System.Diagnostics.ProcessStartInfo]::new()
$psi.FileName = "ffmpeg"
$psi.Arguments = "-hide_banner -y -hwaccel cuda " +
"-i `"$($f.FullName)`" " +
"-c:v hevc_nvenc -cq $cq -preset $preset -c:a copy " +
"-progress pipe:1 -nostats " +
"-f mp4 `"$outFile`""
$psi.RedirectStandardError = $true # -progress пише у stderr
$psi.UseShellExecute = $false
$proc = [System.Diagnostics.Process]::Start($psi)
while (-not $proc.HasExited) {
while (($line = $proc.StandardError.ReadLine()) -ne $null) {
if ($line -match '^out_time_ms=(\d+)$') {
$curMs = [int64]$matches[1]
$pct = [math]::Min(($curMs / $durationMs) * 100, 100)
Write-Progress -Activity $f.Name -Status ("{0:N1} %" -f $pct) -PercentComplete $pct
}
}
Start-Sleep -Milliseconds 100
}
Write-Progress -Activity $f.Name -Completed
if ($proc.ExitCode -ne 0) {
Write-Warning "FFmpeg повернув код $($proc.ExitCode) для $($f.Name)"
}
}
Write-Host "`n=== Готово! Стиснуті файли у: $dstDir ==="
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment