-
-
Save halr9000/5d67b2aa81033150227b11a41be8f16d to your computer and use it in GitHub Desktop.
Mp4Utils: a PowerShell module for converting animated GIF to MP4 using ffmpeg
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
<# | |
.SYNOPSIS | |
Converts a GIF file to an MP4 file using ffmpeg. | |
.DESCRIPTION | |
The ConvertTo-Mp4 function takes a GIF file as input and converts it into an MP4 file. | |
It creates a directory named "mp4" to store the converted files. | |
If the MP4 file already exists, it will not perform the conversion. | |
# Flags used with ffmpeg | |
movflags – This option optimizes the structure of the MP4 file so the browser can load it as quickly as possible. | |
pix_fmt – MP4 videos store pixels in different formats. We include this option to specify a specific format which has maximum compatibility across all browsers. | |
vf – MP4 videos using H.264 need to have a dimensions that are divisible by 2. This option ensures that’s the case. | |
Add "-r 30" to specify the frame rate 30 frames/sec. So if you want 10 sec movie with frame rate 30/sec, you make GIF animation that has total 300 frames, then use it. | |
# Features | |
* Output mp4 is encoded with h264, support Firefox/Chrome/Safari in Windows, Mac OSX, Android, and iOS. | |
* One mp4 file for all platforms, there is no need to encode an extra "webm" movie, which encoding speed is pretty slow. | |
* Format as "yuv420p" for Firefox compatibility, the downside is color becomes less-saturate than original gif. | |
* yuv420p only support even width/height, so crop filter is required | |
* "-movflags +faststart" flags are optimized for online view in browser | |
.PARAMETER Path | |
The path of the GIF file to be converted. This parameter is mandatory and can accept input from the pipeline. | |
.INPUTS | |
System.IO.FileInfo | |
.EXAMPLE | |
PS> Get-ChildItem *.gif | ConvertTo-Mp4 | |
This will convert all GIF files in the current folder to MP4 videos with the same base name, and store them in the 'mp4' directory. | |
.NOTES | |
The function uses ffmpeg for conversion. It can be installed with the following command: | |
PS> winget install Gyan.FFmpeg | |
## TODO: Hanbdle filename string as a parameter. Works well when you pipe files to it, however. | |
.LINK | |
https://www.ffmpeg.org/ | |
#> | |
function ConvertTo-Mp4 { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | |
[ValidateScript({ $_.Extension -eq ".gif" })] | |
[System.IO.FileInfo]$Path | |
) | |
begin { | |
try { | |
New-Item -ItemType Directory -Name "mp4" -ErrorAction Stop | |
} | |
catch { | |
} | |
} | |
process { | |
if (-not (Test-Path (Join-Path "mp4" ($_.BaseName + ".mp4") )) ) { | |
ffmpeg.exe -i $_ -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" (Join-Path "mp4" ($_.BaseName + ".mp4") ) | |
} | |
} | |
end { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment