Created
April 25, 2023 19:18
-
-
Save sharunkumar/59ad9a72c4dac16f60de41abd6cfd387 to your computer and use it in GitHub Desktop.
Convert PDF to GIF
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
function Convert-PdfToGif($pdfpath) { | |
$pdf = Get-Item $pdfpath | |
$name = $pdf.BaseName | |
pdftoppm -png "$($pdf.FullName)" $name | |
$pngs = Get-ChildItem "$name-*.png" | |
$num = [math]::Ceiling([math]::Log10($pngs.Length + 1)) | |
$format = "%0" + $num + "d" | |
# converting to gif directly makes a large file, so convert to mp4 first to optimize | |
ffmpeg -y -framerate 1/1.5 -start_number 1 -i "$name-$format.png" "$name.mp4" | |
ffmpeg -y -i "$name.mp4" -filter_complex "[0:v] fps=2,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 "$name.gif" | |
Remove-Item "$name-*.png" | |
Remove-Item "$name.mp4" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment