Last active
July 8, 2025 04:27
-
-
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
<# | |
.SYNOPSIS | |
Converts video files of various formats (e.g., MKV, AVI, MOV) to MP4 using FFmpeg. | |
.DESCRIPTION | |
This script converts video files to MP4 format using FFmpeg. If a specific file is provided | |
via the -InputFile parameter, only that file will be converted. If no file is specified, | |
the script searches the current directory for video files with specified extensions | |
and converts them to MP4 format without re-encoding (using stream copy). | |
The script uses FFmpeg for conversion and suppresses detailed output during the process. | |
Any errors encountered during the conversion are logged. | |
.PARAMETER InputFile | |
(Optional) A single video file to convert to MP4. If not provided, all supported video files | |
in the current directory will be converted. | |
.NOTES | |
- Requires FFmpeg to be installed and available in the system's PATH. | |
- Supported input formats: MKV, AVI, MOV, 3G2 (others can be added as needed). | |
- Audio is converted to AAC if using QCELP, MP4A, or other unsupported formats. | |
- The conversion uses stream copy (`-codec copy`), so the process is fast, without re-encoding. | |
- Suppresses FFmpeg's output using `Out-Null`. | |
#> | |
function makemp4 { | |
param( | |
[string]$InputFile | |
) | |
# List of supported video file extensions | |
$extensions = @("*.mkv", "*.avi", "*.mov", "*.3g2") # Add other extensions as needed | |
# Display the supported file formats | |
$formats = $extensions -replace "\*\.", "" -join ", " | |
Write-Host "Supported formats: $formats" | |
# Determine which files to convert | |
if ($InputFile) { | |
if (Test-Path $InputFile -PathType Leaf) { | |
$videoFiles = ,(Get-Item $InputFile) # Comma to ensure it's always an array | |
} else { | |
Write-Host "The specified file does not exist: $InputFile" -ForegroundColor Red | |
return | |
} | |
} else { | |
# Get all video files in the current directory that match the supported extensions. | |
$videoFiles = Get-ChildItem .\* -Include $extensions | |
# 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." | |
return | |
} | |
} | |
# Loop through each found video file and convert it to MP4 using FFmpeg. | |
$videoFiles | ForEach-Object { | |
try { | |
Write-Host "`nINPUT : $($_.Name)" | |
# 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 | |
} | |
# Quick Print line to find what audio codec is the problem | |
#Write-Host "$($_.Name) has audio codec $audioCodec." | |
#return | |
# Decide whether to copy the audio stream or convert to AAC based on the codec | |
if ($audioCodec -eq "mp4a" -or $audioCodec -eq "qcelp") { | |
# Convert audio to AAC if it's not supported by Windows. | |
Write-Host " Converting audio codec from $audioCodec to AAC" | |
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" | |
ffmpeg -i $_.FullName -c:v copy -c:a copy $output *>&1 | Out-Null | |
} | |
# Print a success message for each file converted. | |
Write-Host "OUTPUT: $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