我在ffmpeg 文档( section 3.2 How do I encode single pictures into movies?)上阅读了以下内容:
要将单张图片编码为电影,请运行以下命令:
ffmpeg -f image2 -i img%d.jpg movie.mpg
Run Code Online (Sandbox Code Playgroud)
请注意,`%d' 被图像编号替换: img%03d.jpg 表示序列 img001.jpg、img002.jpg 等...
我的问题是:谁在img%03d.jpg和之间进行翻译img001.jpg, img002.jpg, etc?它是shell还是ffmpeg?
我想请 ffmpeg 将一系列图像编码为视频。然而,我的序列通常以一个不同于 1 的索引开始,(例如,我们可以称之为start_index),并以我们可以称之为 的索引结束end_index。此外,该序列使用 value 的增量increment,例如:
img_0025.png, img_0030.png, img_0035.png, ... img_0100.png
Run Code Online (Sandbox Code Playgroud)
其中start_index是 25,end_index是 100,increment是 5。
我想将上述图像序列提供给 ffmpeg,而不必先重命名序列。文档解释了如何使用符号链接来做到这一点,但我想知道是否有办法完全避免它们,也许在 zsh 上使用高级通配符。
第 1 部分: %不是特殊字符,因此img%d.jpg参数按原样传递给ffmpeg“完成工作”本身。
第 2 部分:查看ffmpeg文档,我认为没有其他方法可以提供输入文件,因此您可能必须使用符号链接或等待“修复”:
如果模式包含 "%d" 或 "%0Nd",则模式指定的文件列表的第一个文件名必须包含一个介于 0 和 4 之间的数字,后面的所有数字都必须是连续的。这个限制有望得到修复。
它只能使用管道来完成,即。无需重命名且无需符号链接。
这是我放在一起的脚本,评论和所有内容。
我已将其设置为以每秒 1 帧的速度播放。
图像处理使用包netpbm
例如: images-to-vid '$HOME/images" '\./img_[0-9]{4}[^/0-9]*' 1024 768
# Make a video from images whose sizes and aspect-ratios may vary.
#
# Each image is resized to fit maximized within the video frame.
# The images are positioned centrally and padded (when required)
#
# Images are sourced using 'find'.
# They are selected according to a regular expression.
# Symbolic links are ignored.
#
# Output from 'find' is sorted by path, ie. not by name alone,
# but with a -maxlevel 1, this is typically the same...
# You will need to customize the sort if this is not suitable.
#
# Note about 'find':
# This script uses 'find -regex' instead of 'find -name'.
# The pattern must match the whole returned path,
# from ^ to $ inclusive
# The -regextype option is: posix-extended
#
srceD="${1}" # Source Directory
srceR="${2}" # Source imaage extended Regex pattern
targW="${3:-800}" # Target pixel Width
targH="${4:-600}" # Target pixel Height
targB="${5:-black}" # Target Background colour (X11 colours)
targM="${6:-4}" # Target screen geometry Modulo (as required by Target codec)
TARGw=$((targW-(targW%targM))); ((TARGw==targW)) || { echo "Target Width must be divisible by $targM. Rounding down to $TARGw" 1>&2 ; targW=$TARGw; }
TARGh=$((targH-(targH%targM))); ((TARGh==targH)) || { echo "Target Height must be divisible by $targM. Rounding down to $TARGh" 1>&2 ; targH=$TARGh; }
# TODO: test for W/H == 0
cd "$srceD" || exit 2
find . -maxdepth 1 \
\( -type f \) -and -not \
\( -type l \) \
-regextype posix-extended \
-regex "$srceR" \
-print0 |
sort -z |
{
while IFS= read -d $'\0' -r file ;do
# make Composite image on coloured Background
pnmcomp -align=center -valign=middle \
<(anytopnm "$file" 2>/dev/null |
pnmscale -xysize $targW $targH) \
<(ppmmake "$targB" $targW $targH)
done |
ffmpeg -r 1 \
-f image2pipe \
-vcodec ppm \
-i - \
-y -s ${targW}x${targH} \
-vcodec mjpeg \
images.avi
}
Run Code Online (Sandbox Code Playgroud)