You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

批量将文件夹中MP4文件压缩并转换为GIF的FFmpeg实现方案咨询

批量将文件夹中MP4文件压缩并转换为GIF的FFmpeg实现方案咨询

Hey there! Let's work through your problem together. You're looking to batch process all MP4s in a folder with FFmpeg—compress them with a customizable CRF value, convert them to GIFs, and keep the original filenames recognizable so you don't lose track of which file is which. Here's exactly how to do this, tailored for both Windows and Linux/macOS:


For Windows (Batch Script)

Create a new text file, rename it to mp4-to-gif.bat, and paste this code inside:

@echo off
setlocal enabledelayedexpansion

:: 自定义CRF值(0-63,数值越大压缩率越高,质量越低)
set CRF=40

:: 遍历当前文件夹下所有MP4文件
for %%F in (*.mp4) do (
    echo Processing "%%F"...
    :: 生成调色板(大幅提升GIF颜色质量,避免失真)
    ffmpeg -i "%%F" -vf "palettegen=stats_mode=full" -y "%%~nF_palette.png"
    :: 用调色板转换为GIF,同时应用CRF压缩
    ffmpeg -i "%%F" -i "%%~nF_palette.png" -lavfi "paletteuse=dither=sierra2_4a" -crf !CRF! -y "%%~nF_compressed.gif"
    :: 删除临时调色板文件
    del "%%~nF_palette.png"
)

echo All files processed successfully!
pause

What this does:

  • set CRF=40: Adjust this number to tweak compression—go up for smaller files (lower quality) or down for better quality (larger files).
  • %%~nF_compressed.gif: Outputs GIFs with a _compressed suffix (e.g., video.mp4 becomes video_compressed.gif) so you can easily link them to the original files.
  • The palette step is key: Skipping it would result in washed-out, low-color GIFs. This ensures your GIFs retain as much visual detail as possible.

If you don't care about color quality and just want a super-simple script, you can skip the palette step:

@echo off
set CRF=40
for %%F in (*.mp4) do (
    ffmpeg -i "%%F" -crf !CRF! -y "%%~nF_compressed.gif"
)
pause

For Linux/macOS (Terminal Command)

Open your terminal, navigate to the folder with your MP4s, and run this command (you can adjust the CRF value here too):

# Set your desired CRF value (0-63)
CRF=40

# Batch process all MP4s
for f in *.mp4; do
    echo "Processing $f..."
    # Generate color palette
    ffmpeg -i "$f" -vf "palettegen=stats_mode=full" -y "${f%.mp4}_palette.png"
    # Convert to compressed GIF
    ffmpeg -i "$f" -i "${f%.mp4}_palette.png" -lavfi "paletteuse=dither=sierra2_4a" -crf $CRF -y "${f%.mp4}_compressed.gif"
    # Clean up temporary palette
    rm "${f%.mp4}_palette.png"
done

Again, if you want to skip the palette for simplicity:

CRF=40
for f in *.mp4; do ffmpeg -i "$f" -crf $CRF -y "${f%.mp4}_compressed.gif"; done

Quick note on your previous attempts

I noticed your earlier batch code had a tiny typo (missing a closing quote on the MP3 conversion line) and was targeting the wrong output format. The scripts above fix that, add the customizable CRF you wanted, and ensure your GIFs are either high-quality (with palette) or lightweight (without).

Just drop the batch file in your MP4 folder (or run the terminal command from the folder) and let FFmpeg do the rest!

备注:内容来源于stack exchange,提问作者RayRay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 07:28:11