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.

Revisions

  1. serg-yalosovetsky created this gist Jun 26, 2025.
    63 changes: 63 additions & 0 deletions compress_all.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    <#
    Стискає усі *.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 ==="