rainisto

Extract last frame from video

Dec 10th, 2024
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.69 KB | Software | 0 0
  1. Add-Type -AssemblyName System.Windows.Forms
  2.  
  3. $form = New-Object System.Windows.Forms.Form
  4. $form.Text = "Drop a Video File Here"
  5. $form.Width = 400
  6. $form.Height = 400
  7. $form.AllowDrop = $true
  8.  
  9. # Add a label with instructions
  10. $label = New-Object System.Windows.Forms.Label
  11. $label.Text = "Drag and drop a video file here to extract its last frame"
  12. $label.AutoSize = $true
  13. $label.Location = New-Object System.Drawing.Point(20, 80)
  14. $form.Controls.Add($label)
  15.  
  16. # Get the script directory more reliably
  17. $scriptDir = if ($PSScriptRoot) {
  18.     $PSScriptRoot
  19. } elseif ($psISE) {
  20.     Split-Path -Parent $psISE.CurrentFile.FullPath
  21. } else {
  22.     $PWD.Path
  23. }
  24.  
  25. # DragEnter event handler
  26. $form.Add_DragEnter({
  27.     $_.Effect = if ($_.Data.GetDataPresent([Windows.Forms.DataFormats]::FileDrop)) {
  28.         [Windows.Forms.DragDropEffects]::Copy
  29.     } else {
  30.         [Windows.Forms.DragDropEffects]::None
  31.     }
  32. })
  33.  
  34. # DragDrop event handler
  35. $form.Add_DragDrop({
  36.     $files = $_.Data.GetData([Windows.Forms.DataFormats]::FileDrop)
  37.     foreach ($file in $files) {
  38.         $folder = Split-Path $file
  39.         $baseName = [System.IO.Path]::GetFileNameWithoutExtension($file)
  40.         $outputFile = Join-Path $folder "$($baseName)-lastframe.jpg"
  41.  
  42.         # Use the previously defined scriptDir
  43.         $ffmpegPath = Join-Path $scriptDir "ffmpeg.exe"
  44.  
  45.         if (-not (Test-Path $ffmpegPath)) {
  46.             [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)
  47.             return
  48.         }
  49.  
  50.         try {
  51.             $process = Start-Process -FilePath $ffmpegPath -ArgumentList "-y", "-sseof", "-1", "-i", "`"$file`"", "-vframes", "1", "-q:v", "2", "`"$outputFile`"" -NoNewWindow -Wait -PassThru
  52.            
  53.             if ($process.ExitCode -eq 0) {
  54.                 [System.Windows.Forms.MessageBox]::Show("Last frame extracted successfully to:`n$outputFile", "Success", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
  55.             } else {
  56.                 [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)
  57.             }
  58.         }
  59.         catch {
  60.             [System.Windows.Forms.MessageBox]::Show("Error processing file: $_", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
  61.         }
  62.     }
  63. })
  64.  
  65. [System.Windows.Forms.Application]::EnableVisualStyles()
  66. $form.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment