Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Add-Type -AssemblyName System.Windows.Forms
- $form = New-Object System.Windows.Forms.Form
- $form.Text = "Drop a Video File Here"
- $form.Width = 400
- $form.Height = 400
- $form.AllowDrop = $true
- # Add a label with instructions
- $label = New-Object System.Windows.Forms.Label
- $label.Text = "Drag and drop a video file here to extract its last frame"
- $label.AutoSize = $true
- $label.Location = New-Object System.Drawing.Point(20, 80)
- $form.Controls.Add($label)
- # Get the script directory more reliably
- $scriptDir = if ($PSScriptRoot) {
- $PSScriptRoot
- } elseif ($psISE) {
- Split-Path -Parent $psISE.CurrentFile.FullPath
- } else {
- $PWD.Path
- }
- # DragEnter event handler
- $form.Add_DragEnter({
- $_.Effect = if ($_.Data.GetDataPresent([Windows.Forms.DataFormats]::FileDrop)) {
- [Windows.Forms.DragDropEffects]::Copy
- } else {
- [Windows.Forms.DragDropEffects]::None
- }
- })
- # DragDrop event handler
- $form.Add_DragDrop({
- $files = $_.Data.GetData([Windows.Forms.DataFormats]::FileDrop)
- foreach ($file in $files) {
- $folder = Split-Path $file
- $baseName = [System.IO.Path]::GetFileNameWithoutExtension($file)
- $outputFile = Join-Path $folder "$($baseName)-lastframe.jpg"
- # Use the previously defined scriptDir
- $ffmpegPath = Join-Path $scriptDir "ffmpeg.exe"
- if (-not (Test-Path $ffmpegPath)) {
- [System.Windows.Forms.MessageBox]::Show("ffmpeg.exe not found at:`n$ffmpegPath`n`nPlease ensure it's in the same folder as the script.", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
- return
- }
- try {
- $process = Start-Process -FilePath $ffmpegPath -ArgumentList "-y", "-sseof", "-1", "-i", "`"$file`"", "-vframes", "1", "-q:v", "2", "`"$outputFile`"" -NoNewWindow -Wait -PassThru
- if ($process.ExitCode -eq 0) {
- [System.Windows.Forms.MessageBox]::Show("Last frame extracted successfully to:`n$outputFile", "Success", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
- } else {
- [System.Windows.Forms.MessageBox]::Show("Failed to extract frame. FFmpeg error code: $($process.ExitCode)", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
- }
- }
- catch {
- [System.Windows.Forms.MessageBox]::Show("Error processing file: $_", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
- }
- }
- })
- [System.Windows.Forms.Application]::EnableVisualStyles()
- $form.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment