Skip to content

Instantly share code, notes, and snippets.

@pan-canon
Created May 26, 2025 19:19
Show Gist options
  • Save pan-canon/0b797ce1a5d68a5a28186620ccd9db8e to your computer and use it in GitHub Desktop.
Save pan-canon/0b797ce1a5d68a5a28186620ccd9db8e to your computer and use it in GitHub Desktop.
cut_video.ps1 (PowerShell | ffmpeg)
# cut_video.ps1
# 1) Locate ffmpeg
try {
& ffmpeg -version -ErrorAction Stop | Out-Null
$ffmpegPath = "ffmpeg"
}
catch {
while ($true) {
$candidate = Read-Host "ffmpeg not found in PATH. Enter full path to ffmpeg.exe"
if ($candidate -and (Test-Path $candidate -PathType Leaf)) {
$ffmpegPath = $candidate
break
}
Write-Host "Invalid path. Please try again."
}
}
# 2) Prompt for video file path
while ($true) {
$inputFile = Read-Host "Enter the full path to your video file (including extension)"
if ($inputFile -and (Test-Path $inputFile -PathType Leaf)) { break }
Write-Host "Video file not found. Please try again."
}
# 3) Display video duration
Write-Host ""
Write-Host "=== Video Duration ==="
& $ffmpegPath -i $inputFile 2>&1 | Select-String "Duration"
Write-Host ""
# 4) Prompt for start time components
Write-Host "Enter start time for your clip:"
do { $sh = Read-Host " Hours" } until ($sh -match '^\d+$')
do { $sm = Read-Host " Minutes" } until ($sm -match '^\d+$' -and [int]$sm -lt 60)
do { $ss = Read-Host " Seconds" } until ($ss -match '^\d+$' -and [int]$ss -lt 60)
$startTime = "{0:D2}:{1:D2}:{2:D2}" -f [int]$sh, [int]$sm, [int]$ss
# 5) Prompt for end time components
Write-Host "Enter end time for your clip:"
do { $eh = Read-Host " Hours" } until ($eh -match '^\d+$')
do { $em = Read-Host " Minutes" } until ($em -match '^\d+$' -and [int]$em -lt 60)
do { $es = Read-Host " Seconds" } until ($es -match '^\d+$' -and [int]$es -lt 60)
$endTime = "{0:D2}:{1:D2}:{2:D2}" -f [int]$eh, [int]$em, [int]$es
Write-Host ""
# 6) Construct output file name
$directory = Split-Path $inputFile -Parent
$fileName = [System.IO.Path]::GetFileNameWithoutExtension($inputFile)
$extension = [System.IO.Path]::GetExtension($inputFile)
$startLabel = $startTime -replace ":", "-"
$endLabel = $endTime -replace ":", "-"
$outputFile = Join-Path $directory ("{0}_from_{1}_to_{2}{3}" -f $fileName, $startLabel, $endLabel, $extension)
# 7) Execute ffmpeg to cut both video and audio
Write-Host "Cutting from $startTime to $endTime (video+audio)..."
& $ffmpegPath -i $inputFile -ss $startTime -to $endTime -c copy $outputFile
# 8) Inform the user
Write-Host ""
Write-Host "New file saved as:"
Write-Host " $outputFile"
@echo off
:: Launch the PowerShell script and keep console open
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0cut_video.ps1"
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment