Last active
May 1, 2025 12:33
-
-
Save Kianda/4c32b0e56b9327e21a263f064b3bfb29 to your computer and use it in GitHub Desktop.
Windows Powershell FFMPEG ps1 script to split video into chunks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: .\split_video.ps1 -inputFile "input.mp4" -numSplits 4 | |
param ( | |
[string]$inputFile, # Input MP4 file | |
[int]$numSplits # Number of parts | |
) | |
# Check if ffprobe and ffmpeg are installed | |
$ffprobe = Get-Command ffprobe -ErrorAction SilentlyContinue | |
$ffmpeg = Get-Command ffmpeg -ErrorAction SilentlyContinue | |
if (-not $ffprobe -or -not $ffmpeg) { | |
Write-Host "Error: ffmpeg and ffprobe must be installed and in your system's PATH." -ForegroundColor Red | |
exit 1 | |
} | |
# Get the filename without extension | |
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($inputFile) | |
# Get the duration of the video | |
$duration = & ffprobe -i $inputFile -show_entries format=duration -v quiet -of csv="p=0" | |
$duration = [math]::Round([double]$duration, 2) | |
# Calculate segment duration | |
$segmentDuration = [math]::Round($duration / $numSplits, 2) | |
Write-Host "Video Duration: $duration seconds" | |
Write-Host "Splitting into $numSplits parts, each part will be $segmentDuration seconds long." | |
# Split the video | |
for ($i = 0; $i -lt $numSplits; $i++) { | |
$startTime = [math]::Round($i * $segmentDuration, 2) | |
$outputFile = "$baseName`_part$($i+1).mp4" | |
Write-Host "Creating $outputFile (Start: $startTime sec, Duration: $segmentDuration sec)" | |
& ffmpeg -i $inputFile -ss $startTime -t $segmentDuration -c copy $outputFile | |
} | |
Write-Host "Splitting complete!" -ForegroundColor Green |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment