我有一些图像,我想用 ffmpeg 制作动画 gif。图像的名称为:
837_1.png
838_1.png
...
Run Code Online (Sandbox Code Playgroud)
我正在尝试取消ffmpeg的-i命令行选项,但我遇到了一些问题。
如果我没有指定任何内容,它会要求我替换文件:
ffmpeg -i * -vcodec libx264 out.mp4
ffmpeg version 1.2.1 Copyright (c) 2000-2013 the FFmpeg developers
built on Jul 26 2013 20:18:03 with Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
configuration: --prefix=/usr/local/Cellar/ffmpeg/1.2.1 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-nonfree --enable-hardcoded-tables --enable-avresample --enable-vda --cc=cc --host-cflags= --host-ldflags= --enable-libx264 --enable-libfaac --enable-libmp3lame --enable-libxvid
libavutil 52. 18.100 / 52. 18.100
libavcodec 54. 92.100 / 54. 92.100
libavformat 54. 63.104 / 54. 63.104
libavdevice 54. 3.103 / 54. 3.103
libavfilter 3. 42.103 / 3. 42.103
libswscale 2. 2.100 / 2. 2.100
libswresample 0. 17.102 / 0. 17.102
libpostproc 52. 2.100 / 52. 2.100
Input #0, image2, from '358_1.png':
Duration: 00:00:00.04, start: 0.000000, bitrate: N/A
Stream #0:0: Video: png, rgb24, 550x550, 25 tbr, 25 tbn, 25 tbc
File '359_1.png' already exists. Overwrite ? [y/N]
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用互联网上最常用的格式之一 (%d) .. ffmpeg 找不到文件:
fmpeg -i '%3d_1.png' -vcodec libx264 out.mp4
ffmpeg version 1.2.1 Copyright (c) 2000-2013 the FFmpeg developers
built on Jul 26 2013 20:18:03 with Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
configuration: --prefix=/usr/local/Cellar/ffmpeg/1.2.1 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-nonfree --enable-hardcoded-tables --enable-avresample --enable-vda --cc=cc --host-cflags= --host-ldflags= --enable-libx264 --enable-libfaac --enable-libmp3lame --enable-libxvid
libavutil 52. 18.100 / 52. 18.100
libavcodec 54. 92.100 / 54. 92.100
libavformat 54. 63.104 / 54. 63.104
libavdevice 54. 3.103 / 54. 3.103
libavfilter 3. 42.103 / 3. 42.103
libswscale 2. 2.100 / 2. 2.100
libswresample 0. 17.102 / 0. 17.102
libpostproc 52. 2.100 / 52. 2.100
[image2 @ 0x7f963a006600] Could find no file with with path '%3d_1.png' and index in the range 0-4
%3d_1.png: No such file or directory
Run Code Online (Sandbox Code Playgroud)
所以..这里的问题是,如何在ffmpg 中使用-i命令行选项?
为清楚起见,我使用的是 Mac OSX 10.8,ffmpeg 版本 1.2.1,zsh 4.3.11
slh*_*hck 72
没有任何进一步的选项,ffmpeg 的image2demuxer 将查找从 0 开始的序列。它还会检查这个索引,默认范围为 5(这就是为什么它会抱怨 0-4 范围内没有索引)。根据文档,如果要从任意索引(例如 837)开始,则必须设置起始编号。
ffmpeg -start_number 837 -i '%3d_1.png' -c:v libx264 out.mp4
Run Code Online (Sandbox Code Playgroud)
由于 PNG 文件使用 RGB 颜色空间来表示像素,因此转换为 H.264 最终将是 YUV 4:4:4(非二次采样)。生成的视频可能无法在所有播放器上播放,尤其是任何非基于 FFmpeg 的播放器。您的播放器只会显示黑框,或者可能会崩溃。要解决此问题,请将像素格式更改为 YUV 4:2:0:
ffmpeg -start_number 837 -i '%3d_1.png' -c:v libx264 -pix_fmt yuv420p out.mp4
Run Code Online (Sandbox Code Playgroud)
为了控制质量,请使用-crf选项。有关更多信息,请参阅x264 编码指南。
*glob 不起作用不要将*用作输入选项。在ffmpeg看到它之前,shell 会将其扩展到当前目录中的所有文件,因此该命令将扩展为ffmpeg -i file1 file2 … filen. 由于所有输入文件都ffmpeg需要该-i选项,因此它将file2 … filen作为输出文件并覆盖它们。不好。
小智 36
可以通过指定-pattern_type glob选项启用通配:
ffmpeg -pattern_type glob -i '*.png' -vcodec libx264 out.mp4
Run Code Online (Sandbox Code Playgroud)
小智 9
注意:模式通配在 Windows 下不起作用(并且可能永远不会因为在 Windows 下的 libavfilter 中缺乏对通配的支持。)所以,如果你在 Windows 下这样做,你必须重命名文件,以便它们按顺序编号并且没有缝隙。
您可以使用简单的 Powershell 命令行重命名它们:
dir *.jpg | %{$x=0} {Rename-Item $_ -NewName “Base$x”; $x++ }
Run Code Online (Sandbox Code Playgroud)
小智 9
使用 PowerShell 重命名的更好方法(保留顺序):
dir *.jpg | %{$x=0} {Rename-Item $_ -NewName "Base$($x.tostring('000000')).jpg"; $x++ }
Run Code Online (Sandbox Code Playgroud)