-
-
Save AliAryanTech/80f977146c5135052d47d572a7625e9d to your computer and use it in GitHub Desktop.
This file contains 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
$videoPath = Read-Host "Enter the input video path:" | |
$outputDirectory = Read-Host "Enter the output directory path:" | |
# Get the total duration of the video using FFprobe | |
$duration = & ffprobe.exe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 $videoPath | |
# Convert the duration to seconds | |
$durationSeconds = [math]::Round($duration, 0) | |
# Calculate the number of parts based on a 30-second duration | |
$numberOfParts = [math]::Floor($durationSeconds / 30) | |
# Calculate the duration of the last part | |
$lastPartDurationSeconds = $durationSeconds % 30 | |
# Create the output directory if it doesn't exist | |
if (-not (Test-Path $outputDirectory)) { | |
New-Item -ItemType Directory -Path $outputDirectory | Out-Null | |
} | |
# Split the video into parts | |
for ($i = 0; $i -lt $numberOfParts; $i++) { | |
$startTime = $i * 30 | |
$outputFileName = "part_$i.mp4" | |
$outputFilePath = Join-Path -Path $outputDirectory -ChildPath $outputFileName | |
# Use FFmpeg to split the video | |
ffmpeg.exe -i $videoPath -ss $startTime -t 30 -c copy -avoid_negative_ts 1 $outputFilePath | |
# Check the size of the output file | |
$outputFileSize = (Get-Item $outputFilePath).Length / 1MB | |
# If the size is larger than 14 MB, re-encode the video with a lower bitrate | |
if ($outputFileSize -gt 14) { | |
$outputFileName = "part_$i_reencoded.mp4" | |
$outputFilePath = Join-Path -Path $outputDirectory -ChildPath $outputFileName | |
# Use FFmpeg to re-encode the video with a lower bitrate | |
ffmpeg.exe -i $outputFilePath -b:v 1M $outputFilePath | |
# Check the size of the re-encoded output file | |
$outputFileSize = (Get-Item $outputFilePath).Length / 1MB | |
# If the re-encoded size is still larger than 14 MB, display a warning | |
if ($outputFileSize -gt 14) { | |
Write-Warning "The re-encoded video '$outputFileName' is still larger than 14 MB." | |
} | |
} | |
} | |
# Split the last part | |
$outputFileName = "part_$numberOfParts.mp4" | |
$outputFilePath = Join-Path -Path $outputDirectory -ChildPath $outputFileName | |
# Use FFmpeg to split the last part of the video | |
ffmpeg.exe -i $videoPath -ss ($numberOfParts * 30) -t $lastPartDurationSeconds -c copy -avoid_negative_ts 1 $outputFilePath | |
# Check the size of the last part file | |
$outputFileSize = (Get-Item $outputFilePath).Length / 1MB | |
# If the size is larger than 14 MB, re-encode the video with a lower bitrate | |
if ($outputFileSize -gt 14) { | |
$outputFileName = "part_$numberOfParts_reencoded.mp4" | |
$outputFilePath = Join-Path -Path $outputDirectory -ChildPath $outputFileName | |
# Use FFmpeg to re-encode the last part of the video with a lower bitrate | |
ffmpeg.exe -i $outputFilePath -b:v 1M $outputFilePath | |
# Check the size of the re-encoded last part file | |
$outputFileSize = (Get-Item $outputFilePath).Length / 1MB | |
# If the re-encoded size is still larger than 14 MB, display a warning | |
if ($outputFileSize -gt 14) { | |
Write-Warning "The re-encoded last part video '$outputFileName' is still larger than 14 MB." | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment