dothreadonme
New Member
- Jan 22, 2019
- 11
- 7
- 109
For the people who want to cut up the long multiple variant files, I have coerced a chatbot into writing the below powershell ffmpeg script, that will cut them into 30sec chunks
Unfortunately the video's aren't exactly split at the 30 second mark so I also coerced it to write a little thing to trim the last and first couple seconds.
Run both scripts from the folder where the relevant files are. The former will make a new folder with segments, the latter will overwrite segments with trimmed versions
If your files are so short their length is less than the trim weird shit might happen, but idk what.
Edit: I should note that I care little for keeping 4K quality, so ffmpeg isn't trying to in this script.
Code:
# ----------------------------------------
# CONFIGURATION
# ----------------------------------------
$InputDir = "C:\PATH\To\God" # <-- CHANGE THIS
$OutputDir = "$InputDir\output"
$ChunkLength = 30 # Duration in seconds
$SupportedExtensions = @(".mp4", ".mov", ".mkv", ".avi")
# Create output folder if it doesn't exist
if (!(Test-Path -Path $OutputDir)) {
New-Item -ItemType Directory -Path $OutputDir | Out-Null
}
# Get all video files with supported extensions
$videoFiles = Get-ChildItem -Path .\* -File | Where-Object {
$SupportedExtensions -contains $_.Extension.ToLower()
}
# ----------------------------------------
# PROCESS EACH VIDEO
# ----------------------------------------
foreach ($video in $videoFiles) {
$filename = $video.BaseName
$ext = $video.Extension
$inputPath = $video.FullName
Write-Host "`nProcessing: $filename$ext"
# Get video duration using ffprobe
$durationStr = ffprobe -v error -select_streams v:0 -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "`"$inputPath`""
$totalDuration = [math]::Floor([double]$durationStr)
$counter = 1
for ($start = 0; $start -lt $totalDuration; $start += $ChunkLength) {
$outputName = "$filename" + "_$counter$ext"
$outputPath = Join-Path $OutputDir $outputName
Write-Host " ➤ Creating chunk $counter at $start sec $outputName"
# Run FFmpeg as a single-line command
ffmpeg -hide_banner -loglevel error -ss $start -i "`"$inputPath`"" -t $ChunkLength -c:v libx264 -preset veryfast -crf 23 -c:a aac -b:a 128k "`"$outputPath`""
$counter++
}
}
Code:
# ----------------------------
# CONFIGURATION
# ----------------------------
$InputDir = "C:\Users\teije\Documents\Lmao\Webms\Veedeeshka\New folder - Copy\output" # <-- CHANGE THIS
$TrimStart = 1 # Seconds to trim from start
$TrimEnd = 1 # Seconds to trim from end
# Get all .mp4 files in the folder
$videoFiles = Get-ChildItem -Path .\* -Filter *.mp4 -File
foreach ($video in $videoFiles) {
$inputPath = $video.FullName
$tempPath = [System.IO.Path]::ChangeExtension($inputPath, ".trimmed_temp.mp4")
$filename = $video.Name
# Get total duration using ffprobe
$durationStr = ffprobe -v error -select_streams v:0 -show_entries format=duration `
-of default=noprint_wrappers=1:nokey=1 "`"$inputPath`""
$totalDuration = [double]::Parse($durationStr.Trim())
$newDuration = $totalDuration - $TrimStart - $TrimEnd
Write-Host "Trimming '$filename' Removing first $TrimStart sec and last $TrimEnd sec..."
# Trim using re-encode for precise cut
ffmpeg -hide_banner -loglevel error -ss $TrimStart -i "`"$inputPath`"" -t $newDuration `
-c:v libx264 -preset veryfast -crf 23 -c:a aac -b:a 128k "`"$tempPath`""
# If the trimmed file was created, overwrite the original
if (Test-Path $tempPath){
Remove-Item -Path $inputPath
Rename-Item -Path $tempPath -NewName $filename
Write-Host "Trimmed and replaced: $filename"
} else{
Write-Warning "Failed to create trimmed file for $filename"
}
}
If your files are so short their length is less than the trim weird shit might happen, but idk what.
Edit: I should note that I care little for keeping 4K quality, so ffmpeg isn't trying to in this script.
Last edited: