Skip to content

Instantly share code, notes, and snippets.

@thibaultmol
Created January 18, 2023 10:36
Show Gist options
  • Save thibaultmol/d03ede050ab7fc60cc2042a331713da2 to your computer and use it in GitHub Desktop.
Save thibaultmol/d03ede050ab7fc60cc2042a331713da2 to your computer and use it in GitHub Desktop.
Trim video without re-encode
function Download-FFmpeg {
# Download FFmpeg from the official website
$url = "https://ffmpeg.org/releases/ffmpeg-latest-win64-static.zip"
$output = "$env:TEMP\ffmpeg.zip"
Invoke-WebRequest -Uri $url -OutFile $output
# Extract the files
Expand-Archive -Path $output -DestinationPath $env:TEMP
# Add FFmpeg to the system PATH
$env:PATH = "$env:TEMP\ffmpeg-*-static\bin;$env:PATH"
}
# Check if FFmpeg is installed
if (-not (Test-Path -Path "$env:TEMP\ffmpeg-*-static\bin\ffmpeg.exe")) {
if(-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Host "FFmpeg is not installed. Please re-run the script as an administrator to download and install FFmpeg."
break
}
else
{
Download-FFmpeg
}
}
# Get the input file
$file = Read-Host "Enter the original file name or drag and drop the file here:"
# Get the start and end times
$start = Read-Host "Enter the start time of the video in hh:mm:ss or mm:ss format"
$end = Read-Host "Enter the end time of the video in hh:mm:ss or mm:ss format"
# Create the output file name
$output = "$($file.BaseName)-trimmed$($file.Extension)"
# Trim the video
Start-Process -FilePath ffmpeg -ArgumentList "-i `"$file`" -ss $start -to $end -c copy -copyts -avoid_negative_ts 1 -progress - -y `"$output`"" -NoNewWindow -Wait
# Print the output file name
Write-Host "Video trim complete. Output file: $output"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment