Created
February 3, 2025 18:49
-
-
Save PhilMurwin/82f29832c38e042e6b8fc830c911bbe7 to your computer and use it in GitHub Desktop.
Script to remux video files to mp4
This file contains hidden or 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
function makemp4 { | |
# List of supported video file extensions, including 3G2 | |
$extensions = @("*.mkv", "*.avi", "*.mov", "*.3g2") # Add other extensions as needed | |
# Display the supported formats at the start of the script. | |
$formats = $extensions -replace "\*\.", "" -join ", " | |
Write-Host "Supported formats: $formats" | |
# Get all video files in the current directory that match the supported extensions. | |
$videoFiles = Get-ChildItem .\* -Include $extensions | |
# Print the list of files found for debugging | |
Write-Host "Files found:" -ForegroundColor Yellow | |
$videoFiles | ForEach-Object { Write-Host $_.FullName } | |
# If no video files are found, print a message and stop the script. | |
if ($videoFiles.Count -eq 0) { | |
Write-Host "No video files found in the current directory." -ForegroundColor Red | |
return | |
} | |
# Loop through each found video file and convert it to MP4 using FFmpeg. | |
$videoFiles | ForEach-Object { | |
try { | |
# Define the output file name by appending .mp4 to the original file name (without extension). | |
$output = "$($_.BaseName).mp4" | |
# Check the audio codec of the file using FFmpeg. | |
$audioCodec = ffmpeg -i $_.FullName 2>&1 | Select-String -Pattern "Audio: (\w+)" | ForEach-Object { $_.Matches.Groups[1].Value } | |
# Decide whether to copy the audio stream or convert to AAC based on the codec. | |
if ($audioCodec -eq "mp4a" -or $audioCodec -eq "qcelp" -or $audioCodec -eq "some_other_unsupported_codec") { | |
# Convert audio to AAC if it's not supported by Windows or if it's QCELP. | |
Write-Host "Converting audio codec from $audioCodec to AAC for file: $($_.Name)" | |
ffmpeg -i $_.FullName -c:v copy -c:a aac $output *>&1 | Out-Null | |
} | |
else { | |
# Copy both video and audio without re-encoding if the codec is supported. | |
Write-Host "Copying video and audio streams for file: $($_.Name)" | |
ffmpeg -i $_.FullName -c:v copy -c:a copy $output *>&1 | Out-Null | |
} | |
# Print a success message for each file converted. | |
Write-Host "Converted: $($_.Name) to $output" | |
} | |
catch { | |
# If there's an error during conversion, catch the error and display a message. | |
Write-Host "Error converting: $($_.Name) - $_" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment