Skip to content

Instantly share code, notes, and snippets.

@michalciurus
Created June 20, 2023 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michalciurus/2d71583fbf271315a24519a85767f0a5 to your computer and use it in GitHub Desktop.
Save michalciurus/2d71583fbf271315a24519a85767f0a5 to your computer and use it in GitHub Desktop.
# Define a function to run ffmpeg with arguments
function RunFfmpeg($arguments)
{
Write-Host "Running FFmpeg with arguments: $arguments"
$processStartInfo = New-Object System.Diagnostics.ProcessStartInfo
$processStartInfo.FileName = "ffmpeg"
$processStartInfo.Arguments = $arguments
$processStartInfo.UseShellExecute = $false
$processStartInfo.RedirectStandardOutput = $true
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $processStartInfo
$process.Start() | Out-Null
$process.WaitForExit()
}
# Get the script directory
$scriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
# Input video file
$input = Join-Path $scriptDir "YOUR_FILENAME.mp4" # Replace with your file name
# Output video file prefix
$output = Join-Path $scriptDir "output"
# Debug: Print the absolute path to the input file
Write-Host "Input file path: $input"
Write-Host "Extracting audio..."
# Get the audio from the input video
RunFfmpeg "-i `"$input`" -vn -acodec copy `"$scriptDir\audio.aac`""
Write-Host "Splitting and scaling videos..."
# Split the video into four 4K videos
for ($i=0; $i -lt 2; $i++)
{
for ($j=0; $j -lt 2; $j++)
{
RunFfmpeg "-i `"$input`" -filter:v `"crop=1920:1080:$($i*1920):$($j*1080), scale=3840:2160`" -vcodec libx264 -crf 23 -preset veryfast `"$output`_$(($i*2+$j+1)).mp4`""
}
}
Write-Host "Adding audio to videos..."
# Add the audio to the 4K videos
for ($i=1; $i -le 4; $i++)
{
RunFfmpeg "-i `"$output`_${i}.mp4`" -i `"$scriptDir\audio.aac`" -c:v copy -c:a aac -strict experimental `"$output`_${i}_final.mp4`""
}
Write-Host "Cleaning up intermediate files..."
# Clean up the intermediate files
if (Test-Path "$scriptDir\audio.aac") { Remove-Item "$scriptDir\audio.aac" }
for ($i=1; $i -le 4; $i++)
{
if (Test-Path "$output`_${i}.mp4") { Remove-Item "$output`_${i}.mp4" }
Write-Host "Process completed."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment