Skip to content

Commit 184e4f8

Browse files
committed
Update Trim-VideoWithFFMpeg.ps1
1 parent 4287560 commit 184e4f8

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed
+39-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,49 @@
1+
Param(
2+
[Parameter(Mandatory = $false)]
3+
[string]$InputVideo,
4+
[Parameter(Mandatory = $false)]
5+
[string]$OutputVideo,
6+
[Parameter(Mandatory = $true)]
7+
[string]$StartCut,
8+
[Parameter(Mandatory = $false)]
9+
[string]$EndCut
10+
)
111

2-
# Define the input video file
3-
$inputVideo = "xxx"
4-
$outputVideo = "xxx"
5-
$startCut = 45 # Starting cut time in seconds
6-
$endCut = 12 # Ending cut time in seconds
12+
# function to get time in hh:mm:ss format
13+
function Get-TimeInHHMMSSFormat {
14+
param(
15+
[string]$Time
16+
)
17+
18+
$timeParts = $time.Split(':')
19+
20+
switch ($timeParts.Count) {
21+
1 { return [TimeSpan]::FromSeconds([int]$timeParts[0]) }
22+
2 { return [TimeSpan]::FromMinutes([int]$timeParts[0]).Add([TimeSpan]::FromSeconds([int]$timeParts[1])) }
23+
3 { return [TimeSpan]::FromHours([int]$timeParts[0]).Add([TimeSpan]::FromMinutes([int]$timeParts[1])).Add([TimeSpan]::FromSeconds([int]$timeParts[2])) }
24+
default { throw "Invalid time format. Please use one of the following formats: SS, MM:SS, or HH:MM:SS." }
25+
}
26+
}
727

828
# Get video duration using FFmpeg
9-
$duration = & ffmpeg -i $inputVideo 2>&1 | Select-String -Pattern "Duration: (\d+):(\d+):(\d+\.\d+)" | ForEach-Object { $_.Matches.Groups[1].Value, $_.Matches.Groups[2].Value, $_.Matches.Groups[3].Value -join ":" }
29+
$duration = & ffmpeg -i $InputVideo 2>&1 | Select-String -Pattern "Duration: (\d+):(\d+):(\d+\.\d+)" | ForEach-Object { $_.Matches.Groups[1].Value, $_.Matches.Groups[2].Value, $_.Matches.Groups[3].Value -join ":" }
30+
31+
# Use the custom Parse-Time function
32+
$startCutTimeSpan = Get-TimeInHHMMSSFormat -Time $StartCut
33+
34+
# If no end cut time is provided, set it to the duration of the video
35+
if ([string]::IsNullOrWhitespace($EndCut)) {
36+
Write-Host -ForegroundColor Cyan "No end cut time provided. Using the full duration of the video $duration"
37+
$endCut = $duration
38+
}
1039

11-
# Convert duration string to TimeSpan
12-
$durationTimeSpan = [TimeSpan]::Parse($duration)
40+
$endCutTimeSpan = Get-TimeInHHMMSSFormat -Time $EndCut
1341

14-
# Calculate new duration by subtracting 10 seconds (5 seconds from the start and 5 from the end)
15-
$newDuration = $durationTimeSpan.Subtract([TimeSpan]::FromSeconds($startCut + $endCut))
42+
# Calculate new duration by subtracting start cut from end cut
43+
$newDuration = $endCutTimeSpan.Subtract($startCutTimeSpan)
1644

1745
# Format new duration for FFmpeg
1846
$newDuration = "{0:00}:{1:00}:{2:00}.{3:000}" -f $newDuration.Hours, $newDuration.Minutes, $newDuration.Seconds, $newDuration.Milliseconds
1947

2048
# Use FFmpeg to cut the video
21-
& ffmpeg -ss 00:00:$startCut -i $inputVideo -t $newDuration -c copy $outputVideo
49+
& ffmpeg -ss $StartCut -i $InputVideo -t $newDuration -c copy $OutputVideo

0 commit comments

Comments
 (0)