Convert MP4 to Animated WebP Using FFmpeg in PowerShell

Created: 2025-03-27 20:32:10 | Last updated: 2025-03-27 20:32:10 | Status: Public

This document provides a PowerShell command to batch convert all MP4 files in the current directory to animated WebP format using FFmpeg.

Prerequisites

  • FFmpeg installed and added to your system PATH
  • PowerShell

Command

Get-ChildItem -Filter *.mp4 | ForEach-Object {
    ffmpeg -i $_.FullName -vf "fps=15,scale=800:-1:flags=lanczos" -c:v libwebp -lossless 0 -compression_level 6 -q:v 80 -loop 0 -preset picture -an -vsync 0 "$($_.BaseName).webp"
}

Parameters Explained

  • Get-ChildItem -Filter *.mp4: Finds all MP4 files in the current directory
  • $_.FullName: The full path to the current MP4 file
  • -vf "fps=15,scale=800:-1:flags=lanczos":
  • Sets the frame rate to 15 fps
  • Resizes width to 800px while maintaining aspect ratio
  • Uses the lanczos scaling algorithm for better quality
  • -c:v libwebp: Uses the WebP codec
  • -lossless 0: Disables lossless compression
  • -compression_level 6: Sets compression level (0-6)
  • -q:v 80: Sets quality (0-100, where higher is better quality but larger file size)
  • -loop 0: Makes the WebP loop infinitely
  • -preset picture: Uses the picture preset for encoding
  • -an: Removes audio
  • -vsync 0: Prevents frame duplication or dropping
  • "$($_.BaseName).webp": Names the output file with the same base name as the input but with .webp extension

Customization Options

You can adjust these parameters to meet your specific needs:

  • Frame rate: Change fps=15 to your desired frame rate (higher values create smoother animation but larger files)
  • Resolution: Modify scale=800:-1 to change the output size (e.g., scale=400:-1 for smaller files)
  • Quality: Adjust -q:v 80 to balance quality vs file size:
  • Lower values (e.g., 50-70): Smaller files, lower quality
  • Higher values (e.g., 90-95): Larger files, higher quality
  • Animation speed: Add setpts=0.5*PTS to the -vf parameter to make the animation play at 2x speed (adjust the multiplier as needed)

Example With Additional Options

Get-ChildItem -Filter *.mp4 | ForEach-Object {
    # Create a directory for the output if it doesn't exist
    if (-not (Test-Path -Path ".\webp_output")) {
        New-Item -ItemType Directory -Path ".\webp_output"
    }

    # Convert with custom settings
    ffmpeg -i $_.FullName -vf "fps=10,scale=400:-1:flags=lanczos" -c:v libwebp -lossless 0 -compression_level 6 -q:v 70 -loop 0 -preset picture -an -vsync 0 ".\webp_output\$($_.BaseName).webp"

    Write-Host "Converted $($_.Name) to WebP format"
}