本文目录导读:

我来介绍几种用脚本转换视频为动图的方法,涵盖不同工具和场景。
使用 FFmpeg(最推荐,功能最强)
FFmpeg 是处理视频的瑞士军刀,以下各种场景的脚本:
基础转换脚本
#!/bin/bash
# 简单转换:video2gif.sh
# 用法: ./video2gif.sh input.mp4 output.gif
input="${1:-input.mp4}"
output="${2:-output.gif}"
# 直接转换
ffmpeg -i "$input" "$output"
高质量+可定制转换脚本
#!/bin/bash
# 高质量动图转换:video2gif_quality.sh
# 用法: ./video2gif_quality.sh input.mp4 [fps] [width]
input="$1"
fps="${2:-15}" # 帧率,默认15fps
width="${3:-640}" # 宽度,默认640px
output="${input%.*}_animated.gif" # 输出文件名
# 使用调色板方式生成高质量GIF
ffmpeg -y -i "$input" -vf "fps=$fps,scale=$width:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse" -loop 0 "$output"
echo "✅ 已生成动图: $output"
提取视频片段转GIF
#!/bin/bash
# 提取指定时间段转换:video2gif_segment.sh
# 用法: ./video2gif_segment.sh input.mp4 00:00:05 00:00:10
input="$1"
start_time="${2:-00:00:00}" # 开始时间
duration="${3:-5}" # 时长(秒)
fps="${4:-15}"
output="${input%.*}_segment.gif"
ffmpeg -y -ss "$start_time" -t "$duration" -i "$input" \
-vf "fps=$fps,scale=640:-1" -loop 0 "$output"
echo "✅ 已生成片段动图: $output"
Python脚本(适合批量处理)
使用 imageio/moviepy
#!/usr/bin/env python3
# video2gif.py
import imageio.v3 as iio
import sys
import os
def video_to_gif(video_path, output_path=None, fps=15, start_time=0, duration=None):
"""视频转GIF"""
if not output_path:
output_path = video_path.rsplit('.', 1)[0] + '.gif'
# 读取视频
frames = iio.imread(video_path, plugin='pyav', fps=fps)
# 截取指定时间段
frames_per_second = len(frames) / (duration or 1)
start_frame = int(start_time * frames_per_second)
if duration:
end_frame = int((start_time + duration) * frames_per_second)
frames = frames[start_frame:end_frame]
# 保存为GIF
iio.imwrite(output_path, frames, duration=1/fps*1000, loop=0)
print(f"✅ 已生成: {output_path}")
return output_path
# 批量转换
def batch_convert(folder):
"""批量转换文件夹内所有视频"""
videos = [f for f in os.listdir(folder)
if f.endswith(('.mp4', '.avi', '.mov', '.mkv'))]
for video in videos:
video_path = os.path.join(folder, video)
video_to_gif(video_path)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("用法: python video2gif.py [视频文件] [fps] 或 [文件夹名]")
sys.exit(1)
target = sys.argv[1]
if target.endswith(('.mp4', '.avi', '.mov', '.mkv')):
fps_param = int(sys.argv[2]) if len(sys.argv) > 2 else 15
video_to_gif(target, fps=fps_param)
else:
batch_convert(target)
使用 ImageMagick(简单场景)
#!/bin/bash
# image_magick_gif.sh
# 依赖:ImageMagick (需要先安装)
input="$1"
# 先提取帧,再合成GIF
mkdir -p /tmp/gif_frames
ffmpeg -i "$input" -vf "fps=10,scale=480:-1" /tmp/gif_frames/frame_%04d.png
# 使用ImageMagick合成
convert -delay 10 -loop 0 /tmp/gif_frames/*.png "${input%.*}.gif"
# 清理
rm -rf /tmp/gif_frames
echo "✅ 转换完成"
进阶:交互式选项脚本
#!/bin/bash
# interactive_video2gif.sh - 交互式转换工具
echo "🎬 视频转GIF工具"
echo "================="
# 选择文件
echo "请选择视频文件:"
read -e input
if [ ! -f "$input" ]; then
echo "❌ 文件不存在!"
exit 1
fi
# 设置参数
echo "帧率 (默认15):"
read fps
fps=${fps:-15}
echo "宽度像素 (默认640):"
read width
width=${width:-640}
echo "开始时间 (格式 HH:MM:SS,直接回车从开头):"
read start_time
echo "时长 (秒,直接回车为全部):"
read duration
# 构建ffmpeg命令
cmd="ffmpeg -y"
if [ ! -z "$start_time" ]; then
cmd="$cmd -ss $start_time"
fi
if [ ! -z "$duration" ]; then
cmd="$cmd -t $duration"
fi
cmd="$cmd -i '$input' -vf 'fps=$fps,scale=$width:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse' -loop 0 '$input.gif'"
# 执行
eval $cmd
echo "✅ 转换完成: ${input}.gif"
在线API方案(适合无本地环境)
#!/usr/bin/env python3
# cloud_convert_video2gif.py
import requests
import sys
def convert_online(video_path):
"""使用在线服务转换(示例:Giphy API需要注册)"""
api_key = "YOUR_API_KEY" # 需要在服务商注册
with open(video_path, 'rb') as f:
files = {'file': f}
# 示例:使用 EzGif(需要网络)
response = requests.post(
'https://ezgif.com/video-to-gif',
files=files
)
print("请访问返回的网页完成转换")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("用法: python cloud_convert_video2gif.py video.mp4")
sys.exit(1)
convert_online(sys.argv[1])
安装依赖
# Debian/Ubuntu sudo apt install ffmpeg imagemagick python3-pip pip install imageio imageio-ffmpeg # macOS brew install ffmpeg imagemagick python pip3 install imageio imageio-ffmpeg # Windows (Check Chocolatey) choco install ffmpeg imagemagick pip install imageio imageio-ffmpeg
使用建议
- 如果追求质量:使用FFmpeg + 调色板方法
- 需要批量处理:推荐Python脚本
- 简单快速:直接命令行 FFmpeg
- 在线处理:EzGif或Giphy等网站
选择适合你需求的方案即可!
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播