54

I have some images and I'd like to make an animated gif with ffmpeg. The images have names as:

837_1.png
838_1.png
...

I'm trying to unserstand the -i command line option of ffmpeg but I am crashing against some problems.

If I don't specify anything it ask me to replace the files:

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]

And if I try to use one of the most used format (%d) in the internet.. ffmpeg does not find the files:

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

So.. here is the question, how to use the -i command line option with ffmpg?

For sake of clarity, I am on Mac OSX 10.8, ffmpeg version 1.2.1, zsh 4.3.11

4 Answers 4

81

Without any further options, ffmpeg's image2 demuxer will look for a sequence starting with 0. It'll also check around this index, with a default range of 5 (that's why it'll complain about no index in the range 0–4). Per the documentation, you have to set the start number if you want to start at an arbitrary index, like 837.

ffmpeg -start_number 837 -i '%3d_1.png' -c:v libx264 out.mp4

Color space in PNG to H.264 conversion

Since PNG files use the RGB color space to represent pixels, the conversion to H.264 would end up being YUV 4:4:4 (non-subsampled). The resulting video may not be playable on all players, notably anything non-FFmpeg-based. Your player would show only black frames, or possibly crash. To fix that, change the pixel format to YUV 4:2:0:

ffmpeg -start_number 837 -i '%3d_1.png' -c:v libx264 -pix_fmt yuv420p out.mp4

In order to control the quality, use the -crf option. See the x264 encoding guide for more info.

Why the * glob does not work

Don't use the * as an input option. The shell will expand it to all files in the current directory before ffmpeg sees it, so the command would expand to ffmpeg -i file1 file2 … filen. Since all input files in ffmpeg need the -i option, it'll take file2 … filen as output files instead and overwrite them. Not good.

7
  • very complete answer! and do you also have some advice on animated gif?
    – nkint
    Oct 29, 2013 at 21:16
  • For animated GIFs, you want to update to ffmpeg version 2.0 and above, because they greatly improved the visual quality. The download page has some links to static builds for OS X. For older ffmpeg, see: ffmpeg converts .flv video to .gif with awful quality
    – slhck
    Oct 29, 2013 at 21:29
  • In fact, it not only checks +0..4 but also 8,16,32,64 etc. This is noticeable when using HTTP url's. However, image2 fails to recognize that 200 OK is the equivalent of "file found" so it doesn't stop searching.
    – MSalters
    Jul 18, 2017 at 15:38
  • 1
    I'm getting the same thing even after trying to implement your suggestions. My files are named DSCxxxxx.jpg where xxxxx is a 5-digit number starting with zero (note I have no underscores). Still, after running ffmpeg -r 30 -start_number 04606 -i DSC%d.jpg out.mp4 I get the same error as in the original post above Nov 26, 2017 at 23:31
  • 2
    What got me was that the -start_number parameter has to be before the -i parameter. Also use -vframes for a stop number.
    – MDMoore313
    Feb 10, 2018 at 17:37
36

Globbing can be enabled by specifying -pattern_type glob option:

ffmpeg -pattern_type glob -i '*.png' -vcodec libx264 out.mp4
4
  • 2
    Much better way :)
    – Gustav
    May 25, 2016 at 8:47
  • 5
    Only this worked for me. No way to get this xxx%d.png, xxx%3d.png, 'xxx%d.png' or 'xxx%3d.png' thing working
    – yPhil
    May 26, 2016 at 23:21
  • I get the same error after trying this: [image2 @ 0x2552560] Could find no file with path '*.jpg' and index in the range 0-4 *.jpg: No such file or directory Nov 26, 2017 at 23:26
  • 3
    Pattern type 'glob' was selected but globbing is not supported by this libavformat build [using Windows ffmpeg] [oof, should have scrolled down one answer] Jul 15, 2019 at 5:25
10

Note: Pattern globbing does not work under Windows (and may never due to lack of support for globbing in libavfilter under Windows.) So, if your doing this under Windows, you'll have to rename the files so that they're numbered sequentially and without gaps.

You can rename them with a simple Powershell command line:

dir *.jpg | %{$x=0} {Rename-Item $_ -NewName “Base$x”; $x++ }
2
  • This will return file1.jpg file2.jpgfile10.jpg which if you sort alphabetically would end up file1.jpg, file10.jpgfile2.jpg
    – stib
    Apr 10, 2018 at 0:04
  • Shouldn't that be Base$x.jpg? As-is, yours renamed all mine and killed the file extensions. Jul 15, 2019 at 5:38
10

better way to rename with PowerShell (preserve order) :

dir *.jpg | %{$x=0} {Rename-Item $_ -NewName "Base$($x.tostring('000000')).jpg"; $x++ }
1
  • 1
    Does dir always return the files sorted alphabetically? Would it be safer to add a sort-object into the mix?
    – stib
    Apr 10, 2018 at 0:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .