Clarification for ffmpeg input option with image files as input

35

5

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

nkint

Posted 2013-10-29T13:10:44.130

Reputation: 569

Answers

49

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.

slhck

Posted 2013-10-29T13:10:44.130

Reputation: 182 472

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 – 2017-07-18T15:38:18.707

1I'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 – Anonymous – 2017-11-26T23:31:24.870

@jphollowed Did you put the file name pattern in quotes? – slhck – 2017-11-27T10:17:00.387

What got me was that the -start_number parameter has to be before the -i parameter. Also use -vframes for a stop number. – MDMoore313 – 2018-02-10T17:37:53.690

You rock. This also worked for me on Windows 10. The color output detail was what I needed. – motorbaby – 2018-10-02T18:30:56.007

very complete answer! and do you also have some advice on animated gif? – nkint – 2013-10-29T21:16:54.637

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 – 2013-10-29T21:29:58.973

29

Globbing can be enabled by specifying -pattern_type glob option:

ffmpeg -pattern_type glob -i '*.png' -vcodec libx264 out.mp4

yohanson

Posted 2013-10-29T13:10:44.130

Reputation: 291

2Much better way :) – Gustav – 2016-05-25T08:47:11.773

3Only 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 – 2016-05-26T23:21:59.850

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 – Anonymous – 2017-11-26T23:26:38.760

1Pattern type 'glob' was selected but globbing is not supported by this libavformat build [using Windows ffmpeg] [oof, should have scrolled down one answer] – Katastic Voyage – 2019-07-15T05:25:08.687

7

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++ }

Bill Westrup

Posted 2013-10-29T13:10:44.130

Reputation: 193

This will return file1.jpg file2.jpgfile10.jpg which if you sort alphabetically would end up file1.jpg, file10.jpgfile2.jpg – stib – 2018-04-10T00:04:34.060

Shouldn't that be Base$x.jpg? As-is, yours renamed all mine and killed the file extensions. – Katastic Voyage – 2019-07-15T05:38:05.440

5

better way to rename with PowerShell (preserve order) :

dir *.jpg | %{$x=0} {Rename-Item $_ -NewName "Base$($x.tostring('000000')).jpg"; $x++ }

pBac

Posted 2013-10-29T13:10:44.130

Reputation: 51

Does dir always return the files sorted alphabetically? Would it be safer to add a sort-object into the mix? – stib – 2018-04-10T00:05:18.863