Linux – FFMpeg convert jpeg images to video


I'm attempting to take a set of images taken at 1-second intervals to form a timelapse video. I figured this would be easy with ffmpeg, but i'm running into cryptic errors.

All the images are yuv420 jpegs, taken from an android camera. They're saved as individual jpegs in a folder, they're all identically sized and formatted. They are named as a sequence of 7-digit names, starting at zero (so "0000000.jpg" to "0001000.jpg", as an example).

This is my string (folder names redacted for clarity):

avconv -r 1 -f image2 -i %07d.jpg -vcodec libx264 -crf 18 -preset slower video.mp4

Seems great, i see plenty of posts from people who say that works for them.

However ffmpeg gives me this (bold indicates error)

ffmpeg version 0.8.5-6:0.8.5-1, Copyright (c) 2000-2012 the Libav developers
  built on Jan 13 2013 12:05:48 with gcc 4.7.2
This program is only provided for compatibility and will be removed in a future release. Please use avconv instead.
[mjpeg @ 0x233aea0] overread 8
Input #0, image2, from '%07d.jpg':
  Duration: 00:00:00.04, start: 0.000000, bitrate: N/A
    Stream #0.0: Video: mjpeg, yuvj420p, 640x480 [PAR 1:1 DAR 4:3], 25 tbr, 25 tbn, 25 tbc
File 'video.mp4' already exists. Overwrite ? [y/N] y
[buffer @ 0x233b340] w:640 h:480 pixfmt:yuvj420p
[libx264 @ 0x2339fa0] using SAR=1/1
[libx264 @ 0x2339fa0] using cpu capabilities: MMX2 SSE2Fast FastShuffle SSEMisalign LZCNT
[libx264 @ 0x2339fa0] profile Main, level 3.1
[libx264 @ 0x2339fa0] 264 - core 123 r2189 35cf912 - H.264/MPEG-4 AVC codec - Copyleft 2003-2012 - - options: cabac=1 ref=8 deblock=1:0:0 analyse=0x1:0x131 me=umh subme=9 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=2 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=0 b_adapt=2 b_bias=0 direct=3 weightb=0 open_gop=1 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=60 rc=crf mbtree=1 crf=18.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.25 aq=1:1.00
Output #0, mp4, to 'video.mp4':
    encoder         : Lavf53.21.1
    Stream #0.0: Video: libx264, yuvj420p, 640x480 [PAR 1:1 DAR 4:3], q=-1--1, 25 tbn, 25 tbc
Stream mapping:
  Stream #0.0 -> #0.0
Press ctrl-c to stop encoding
[mjpeg @ 0x233aea0] overread 8
frame=    0 fps=  0 q=0.0 Lsize=       0kB time=10000000000.00 bitrate=   0.0kbits/s    
video:0kB audio:0kB global headers:0kB muxing overhead 610.000000%

The import lines (which show up in red) are the ones that say "[mjpeg @ 0x233aea0] overread 8".

This makes me think that perhaps it's trying to read the images as motion jpegs instead of plain jpeg files, and is failing somehow. I've tried with/without "-f image2", with the same result. And i've also tried using avconv, with the same result.

I should note that the images are valid, and if i convert them all to *.png (a costly and time-consuming process) the whole thing works. But i'd prefer to just be able to use the jpegs, without an intermediate conversion. After all, that's what ffmpeg is for, transcoding.

Does anyone have a good way to convert *.jpg to video using ffmpeg?

Best Answer

[mjpeg @ 0x233aea0] overread 8 is an error that is thrown when ffmpeg tries to read a JPEG image (or a frame from an MJPEG stream) block-by-block, but there are still leftover bits.

In that case, even though your images might be showing fine in other viewers, it's something FFmpeg can't (or won't) handle. To fix this you need to re-save the images, either as PNGs as you tried before, or as JPEGs. You can do that kind of batch conversion with ImageMagick, for example:

for f in *.JPG; do mogrify "$f"; done

This would re-save the JPEG files under the same name. Note that you'll lose quality in that process, so it's not ideal and I'd go with PNGs as intermediates as they're lossless.

By the way: It's always a good idea to use the latest version of FFmpeg, which you can download as a static build (just download, extract, use), or compile yourself. The packages that come with Ubuntu are always outdated, and – depending on your Ubuntu version – not even the "real" FFmpeg, but the Libav fork (which is fine to use, but one should know the difference).

Don't forget that the images need to have sequential names when you use the %07d.jpg syntax for ffmpeg, without missing numbers. See also the documentation on the image2 muxer for more options.

Related Question