ffmpeg 并创建由视频缩略图网格组成的单个图像

ffmpeg 并创建由视频缩略图网格组成的单个图像

Ffmpeg 是一款功能强大但又复杂的工具。我想创建一个由行和列组成的单一图像文件,其中包含来自视频文件的静态图像(缩略图)。

最困难但至关重要的功能是缩略图必须均匀分布在视频中。例如,如果视频时长为 60 秒,网格为 4x4(16 个缩略图),则每个缩略图之间的间隔为 60/16(3.7 秒)。

另一个例子。如果视频长 10 分钟,所需网格为 7 行 8 列,则间隔必须是 600/7*8(10.7 秒)。

额外要求(不错,但不是必需的):

  • 图像尺寸应该可以在代码中调整,例如宽 1920 像素,高 900 像素
  • 每个缩略图上均有时间戳(hh:mm:ss)

此处有一个显示满足所有要求的示例(使用商业程序)> https://i.stack.imgur.com/7gz3S.jpg

提前致谢。

答案1

我创建了一个脚本,还需要图像魔法

#!/usr/bin/env bash

directory="/tmp/your/path"
mkdir -p "$directory"

length="$(ffprobe -i "$1" -show_entries format=duration -v quiet -of csv='p=0')"
image_count="$( echo "${length}/3.75" | bc -l )"
length="${length%.*}"
image_count="${image_count%.*}"

for (( i=0; i<$image_count ; i++ )); do
    time=$(echo "${i} * 3.75" | bc -l)
    timestamp=$(date -u -d "@${time}" "+%H:%M:%S")
    ffmpeg -y -i "$1" -ss "$timestamp" -vframes 1 -vcodec png -an -y "${directory}/${timestamp}.png"
    mogrify -font helvetica -fill red -pointsize 100 -draw "text 50,50 '${timestamp}'" "${directory}/${timestamp}.png"
done
montage "${directory}/*" -geometry +10+10 grid.png

运行此脚本需要一些时间,但它也会为每个图像添加时间戳。

ffmpeg -i input.mp4 -frames 1 -vf "select=not(mod(n\,1000)),scale=-1:240,tile=3x2" out.png

这个快了很多检查一下:https://askubuntu.com/questions/874042/how-to-make-screenshot-gallery-from-command-line

相关内容