=== MP4 > GIF Converter ===

Programlama ve Script dilleri konusunda bilgi paylaşım alanıdır.
Cevapla
Kullanıcı avatarı
TRWE_2012
Zettabyte1
Zettabyte1
Mesajlar: 15169
Kayıt: 25 Eyl 2013, 13:38
cinsiyet: Erkek
Teşekkür etti: 2511 kez
Teşekkür edildi: 5317 kez

=== MP4 > GIF Converter ===

Mesaj gönderen TRWE_2012 »

Merhabalar

Önce ekran görüntüsü :
Resim
Kod İçeriği : (MP4_to_GIF_Converter.ps1)

Kod: Tümünü seç

<#
.SYNOPSIS
    MP4 to GIF Stable Converter
.DESCRIPTION
    Converts a selected MP4 video to a palette-based GIF using FFmpeg.
    Offers High / Medium / Low quality options. Window stays open after completion.
#>

$ErrorActionPreference = "Stop"

# --------------------------------------------------------------
# FUNCTIONS
# --------------------------------------------------------------

function Get-FFmpegPath {
    # Search in PATH
    $cmd = Get-Command ffmpeg.exe -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Source
    if ($cmd) { return $cmd }

    # Common install locations
    $locations = @(
        "C:\yt-dlp\bin\ffmpeg.exe",
        "C:\ffmpeg\bin\ffmpeg.exe",
        "$env:LOCALAPPDATA\Microsoft\WinGet\Links\ffmpeg.exe",
        "$env:ProgramFiles\FFmpeg\bin\ffmpeg.exe"
    )
    foreach ($loc in $locations) {
        if (Test-Path $loc) { return $loc }
    }
    return $null
}

function Get-VideoPath {
    while ($true) {
        $path = Read-Host "Full path of the MP4 file (drag-and-drop or paste)"
        $path = $path.Trim('"')
        if (Test-Path $path) {
            return $path
        }
        Write-Host "Invalid file path! Please try again." -ForegroundColor Red
    }
}

function Show-QualityMenu {
    Write-Host ""
    Write-Host "QUALITY LEVELS" -ForegroundColor Cyan
    Write-Host "1 - High    (15 fps, 1024px, 256 colors)"
    Write-Host "2 - Medium  (10 fps, 800px,  128 colors)"
    Write-Host "3 - Low     (8 fps,  640px,  64 colors)"
    $choice = Read-Host "Your choice"
    switch ($choice) {
        "1" { return @{ fps = 15; width = 1024; colors = 256; label = "high"   } }
        "2" { return @{ fps = 10; width = 800;  colors = 128; label = "medium" } }
        "3" { return @{ fps = 8;  width = 640;  colors = 64;  label = "low"    } }
        default { return $null }
    }
}

# --------------------------------------------------------------
# MAIN FLOW
# --------------------------------------------------------------
try {
    Write-Host "`n=== MP4 > GIF Converter ===`n" -ForegroundColor Green

    # FFmpeg check
    $ffmpeg = Get-FFmpegPath
    if (-not $ffmpeg) {
        Write-Host "FFmpeg not found! Please install FFmpeg and add it to PATH." -ForegroundColor Red
        Read-Host "`nPress ENTER to exit."
        exit 1
    }
    Write-Host "FFmpeg detected: $ffmpeg" -ForegroundColor DarkGray

    # File selection
    $videoPath = Get-VideoPath

    # Quality selection
    $settings = Show-QualityMenu
    while ($settings -eq $null) {
        Write-Host "Invalid choice! Please enter 1, 2, or 3." -ForegroundColor Red
        $settings = Show-QualityMenu
    }

    # Build output path
    $dir    = Split-Path $videoPath -Parent
    $name   = [IO.Path]::GetFileNameWithoutExtension($videoPath)
    $output = Join-Path $dir "$($name)_$($settings.label).gif"

    # FFmpeg filter string  (format operator keeps $fps/$width/$colors out of the string literal,
    # avoiding the PowerShell $var: drive-scoped-variable parse error)
    $filter = "fps={0},scale={1}:-1:flags=lanczos,split[s0][s1];[s0]palettegen=max_colors={2}[p];[s1][p]paletteuse" `
              -f $settings.fps, $settings.width, $settings.colors

    Write-Host "`nStarting conversion..." -ForegroundColor Yellow
    Write-Host "   Source : $videoPath"
    Write-Host "   Output : $output`n"

    # ----------------------------------------------------------------
    # FIX: FFmpeg always writes version info + progress to stderr.
    # With $ErrorActionPreference = "Stop" PowerShell turns every
    # stderr line into a terminating error and jumps to catch before
    # the process even finishes.
    # Solution: temporarily relax EAP around the native call,
    # then restore it and check $LASTEXITCODE manually.
    # ----------------------------------------------------------------
    $savedEAP = $ErrorActionPreference
    $ErrorActionPreference = "Continue"

    & $ffmpeg -y -i $videoPath -filter_complex $filter $output 2>&1 | Out-Null

    $ffmpegExit = $LASTEXITCODE
    $ErrorActionPreference = $savedEAP

    if ($ffmpegExit -ne 0) {
        throw "FFmpeg process ended with exit code $ffmpegExit."
    }

    Write-Host "GIF created successfully!" -ForegroundColor Green
    Write-Host "   $output" -ForegroundColor Green

} catch {
    Write-Host "`nERROR: $($_.Exception.Message)" -ForegroundColor Red
} finally {
    Read-Host "`nPress ENTER to exit."
}
Gereksinimler:

Betiğin çalışabilmesi için açık kaynak kodlu "ffmpeg.exe" dosyasına ihtiyacı var ve şuradan indirebilirsiniz tam paket

https://www.gyan.dev/ffmpeg/builds/

Nerede Kullanılabilir?

Diyelim ki bir .mp4 videosu çektiniz, ve bunu bir upload sitesine yüklemek istiyorsunuz.Betik burada devreye giriyor.MP4 dosyasını .GIF dosyasına çeviriyor.Böylece resim yükleme sitesine yükleyebilirsiniz.

Güle güle kullanın

NOT :

Örnek mi? şuna bakın..Yukarıdaki yöntemle üretildi.

viewtopic.php?p=575053#p575053
Cevapla

“Programlama ve Script dilleri” sayfasına dön