Social Media logo EST Login Sign Up Crypto News Not Logged In
Login

 

 

 

 

 

mitch

08:41:30 pm 07/24/2023

Viewed: 4807

a simple script to convert .webp image files to jpg, gif or png.
It is in powershell 1.0, it will open a browse for source image box, then it will open a box to select jpg,gif or png as the output format and it uses ffmpeg to it.

Add-Type -AssemblyName System.Windows.Forms

# Function to display a folder browser dialog and return the selected folder path
function Get-FolderDialog {
    $folderDialog = New-Object System.Windows.Forms.FolderBrowserDialog
    $folderDialog.ShowNewFolderButton = $false

    $result = $folderDialog.ShowDialog()

    if ($result -eq 'OK') {
        return $folderDialog.SelectedPath
    }

    return $null
}

# Function to display a file browser dialog and return the selected file path
function Get-FileDialog {
    $fileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $fileDialog.Filter = 'WebP files (*.webp)|*.webp|All files (*.*)|*.*'

    $result = $fileDialog.ShowDialog()

    if ($result -eq 'OK') {
        return $fileDialog.FileName
    }

    return $null
}

# Function to display a dropdown list and return the selected item
function Show-DropDownList {
    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'Select Output Format'
    $form.Size = New-Object System.Drawing.Size(500, 300)
    $form.StartPosition = 'CenterScreen'
    $form.Topmost = "true"

    $comboBox = New-Object System.Windows.Forms.ComboBox
    $comboBox.Location = New-Object System.Drawing.Point(30, 30)
    $comboBox.Size = New-Object System.Drawing.Size(240, 40)
    $comboBox.Font = New-Object System.Drawing.Font('Arial', 14) # Adjust the font size here
    $comboBox.Items.AddRange(@('jpg', 'gif', 'png'))
    $comboBox.DropDownStyle = 'DropDownList'

    $okButton = New-Object System.Windows.Forms.Button
    $okButton.Location = New-Object System.Drawing.Point(100, 80)
    $okButton.Size = New-Object System.Drawing.Size(100, 40)
    $okButton.Font = New-Object System.Drawing.Font('Arial', 12) # Adjust the font size here
    $okButton.Text = 'OK'
    $okButton.DialogResult = 'OK'

    # Set the default selection to "jpg"
    $comboBox.SelectedItem = "jpg"

    $form.Controls.Add($comboBox)
    $form.Controls.Add($okButton)

    $form.TopMost = $true # Set the form to always open on top

    $result = $form.ShowDialog()

    if ($result -eq 'OK') {
        return $comboBox.SelectedItem
    }

    return $null
}

# Main script logic
$ffmpegPath = Get-Command ffmpeg -ErrorAction SilentlyContinue
if (-not $ffmpegPath) {
    Write-Host 'FFmpeg not found. Please make sure FFmpeg is installed and added to the PATH.'
    Exit
}

$sourceFilePath = Get-FileDialog
if (-not $sourceFilePath) {
    Write-Host 'No source file selected. Exiting.'
    Exit
}

$outputFormat = Show-DropDownList
if (-not $outputFormat) {
    Write-Host 'No output format selected. Exiting.'
    Exit
}

$sourceDirectory = Split-Path -Parent $sourceFilePath
$outputDirectory = Join-Path -Path $sourceDirectory -ChildPath 'converted'
if (-not (Test-Path -Path $outputDirectory -PathType Container)) {
    New-Item -Path $outputDirectory -ItemType Directory | Out-Null
}

$outputFilePath = Join-Path -Path $outputDirectory -ChildPath ('{0}.{1}' -f [System.IO.Path]::GetFileNameWithoutExtension($sourceFilePath), $outputFormat)

# Convert the file using FFmpeg
$ffmpegCommand = "ffmpeg -i `"$sourceFilePath`" `"$outputFilePath`""
Invoke-Expression $ffmpegCommand

Write-Host "Conversion complete. The converted file is located at: $outputFilePath"

No video exists.

0Enjoy
 

Comments


Today: 508

Total: 735031

Last Hour: 0