Skip to content

Instantly share code, notes, and snippets.

@MichaelGatesDev
Created September 24, 2024 19:53
Show Gist options
  • Save MichaelGatesDev/1d4a94b07d3e848f13d4d81f549ce850 to your computer and use it in GitHub Desktop.
Save MichaelGatesDev/1d4a94b07d3e848f13d4d81f549ce850 to your computer and use it in GitHub Desktop.
Combine all MP4 files in the current directory using FFMPEG
# Set the working directory to the script's location
Set-Location -Path $PSScriptRoot
# Initialize an empty string to store the concatenated file list
$fileList = ""
# Define the path to files.txt
$filesTxtPath = "$PSScriptRoot\files.txt"
# Define the output file path
$outputFilePath = "$PSScriptRoot\output.mp4"
# Loop through each MP4 file in the current directory
foreach ($file in Get-ChildItem .\*.mp4) {
# Append the current file path to the file list string with the "file" keyword
$fileList += "file '$($file.FullName)'" + [Environment]::NewLine
# Print a message indicating the file being processed
Write-Host "Processing file: $($file.Name)"
}
# Write the file list string to a text file without BOM
$fileList | Out-File -FilePath $filesTxtPath -Encoding ascii
# Print a message indicating the file list has been written
Write-Host "File list written to $filesTxtPath"
# Define the ffmpeg arguments as an array
$ffmpegArgs = @(
"-f", "concat",
"-safe", "0",
"-i", "`"$filesTxtPath`"",
"-c", "copy",
"`"$outputFilePath`""
)
# Run the ffmpeg process directly in the same window
& ffmpeg @ffmpegArgs
# Print a message indicating the concatenation process is complete
Write-Host "Concatenation process completed. Output file: $outputFilePath"
# Cleanup: Remove the log files and files.txt
Remove-Item -Path "ffmpeg_output.txt", "ffmpeg_error.txt", $filesTxtPath -ErrorAction SilentlyContinue
# Print a message indicating cleanup is complete
Write-Host "Cleanup completed. Log files and files.txt have been deleted."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment