"Cannot allocate memory" error when crossfading

I adapted Gyan's algorithm for crossfading - which I prefer the results of to xfade - to concat two videos together and crossfade with a third:

ffmpeg -i "source1.mp4" -i "source2.mp4" -i "source3.mp4" \
-preset ultrafast \
-movflags faststart \
-filter_complex \
"color=black:1440x1080:d=1789.145[base]; \
[0:v]scale=1440x1080,setpts=PTS-STARTPTS[v0]; \
[1:v]scale=1440x1080,trim=start=15.5,setpts=PTS-STARTPTS+((593.106000)/TB)[v1]; \
[2:v]scale=1440x1080,format=yuva420p,fade=in:st=0:d=2:alpha=1,setpts=PTS-STARTPTS+((1186.028000)/TB)[v2]; \
[base][v1]overlay[tmp]; \
[tmp][v2]overlay,format=yuv420p[crossfade]; \
[v0][crossfade]concat=n=2:v=1:a=0[fv]; \
[1:a]atrim=start=15.5,asetpts=PTS-STARTPTS[a1]; \
[0:a][a1]concat=n=2:v=0:a=1[aconcat]; \
[aconcat][2:a]acrossfade=d=2[fa]" \
-map "[fv]" -map "[fa]" -c:v libx264 -crf 15 -c:a libopus -shortest "test1.mp4"

After encoding for about halfway of what would be the final length of the video and using up all 16GB of my RAM, FFmpeg finally runs out and crashes.

Here's what the memory usage looked like in Task Manager just after the crash:

enter image description here

And here's the full output:

ffmpeg version N-100422-gff6f8c272e-g1eb751955e+1 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 10.2.0 (Rev6, Built by MSYS2 project)
  configuration:  --pkg-config=pkgconf --cc='ccache gcc' --cxx='ccache g++' --disable-autodetect --enable-amf --enable-bzlib --enable-
cuda --enable-cuvid --enable-d3d11va --enable-dxva2 --enable-iconv --enable-lzma --enable-nvenc --enable-zlib --enable-sdl2 --enable-f
fnvcodec --enable-nvdec --enable-cuda-llvm --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264 --
enable-libx265 --enable-libdav1d --enable-libaom --disable-debug --enable-libfdk-aac --enable-openssl --extra-libs=-liconv --enable-gp
l --enable-nonfree
  libavutil      56. 62.100 / 56. 62.100
  libavcodec     58.115.102 / 58.115.102
  libavformat    58. 65.100 / 58. 65.100
  libavdevice    58. 11.103 / 58. 11.103
  libavfilter     7. 93.100 /  7. 93.100
  libswscale      5.  8.100 /  5.  8.100
  libswresample   3.  8.100 /  3.  8.100
  libpostproc    55.  8.100 / 55.  8.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'source1.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.65.100
  Duration: 00:09:54.11, start: 0.000000, bitrate: 372 kb/s
    Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 320x240, 242 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'source2.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.65.100
  Duration: 00:09:52.92, start: 0.000000, bitrate: 315 kb/s
    Stream #1:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 320x240 [SAR 1:1 DAR 4:3], 181 kb/s, 25 fps, 25 tbr, 12800 tbn,
 50 tbc (default)
    Metadata:
      handler_name    : ISO Media file produced by Google Inc.
      vendor_id       : [0][0][0][0]
    Stream #1:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : ISO Media file produced by Google Inc.
      vendor_id       : [0][0][0][0]
Input #2, mov,mp4,m4a,3gp,3g2,mj2, from 'source3.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.65.100
  Duration: 00:10:02.12, start: 0.000000, bitrate: 314 kb/s
    Stream #2:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 320x240 [SAR 1:1 DAR 4:3], 180 kb/s, 25 fps, 25 tbr, 12800 tbn,
 50 tbc (default)
    Metadata:
      handler_name    : ISO Media file produced by Google Inc.
      vendor_id       : [0][0][0][0]
    Stream #2:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 127 kb/s (default)
    Metadata:
      handler_name    : ISO Media file produced by Google Inc.
      vendor_id       : [0][0][0][0]
File 'test1.mp4' already exists. Overwrite? [y/N] y
Stream mapping:
  Stream #0:0 (h264) -> scale
  Stream #0:1 (aac) -> concat:in0:a0
  Stream #1:0 (h264) -> scale
  Stream #1:1 (aac) -> atrim
  Stream #2:0 (h264) -> scale
  Stream #2:1 (aac) -> acrossfade:crossfade1
  concat -> Stream #0:0 (libx264)
  acrossfade -> Stream #0:1 (libopus)
Press [q] to stop, [?] for help
[libx264 @ 000002475283b600] using SAR=1/1
[libx264 @ 000002475283b600] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 000002475283b600] profile Constrained Baseline, level 4.0, 4:2:0, 8-bit
[libx264 @ 000002475283b600] 264 - core 161 r3027 4121277 - H.264/MPEG-4 AVC codec - Copyleft 2003-2020 - http://www.videolan.org/x264
.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 tre
llis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 in
terlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=25 scenecut=0 intra_refresh=0 rc=crf mbtree=0
 crf=15.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
[libopus @ 0000024752837f40] No bit rate set. Defaulting to 96000 bps.
Output #0, mp4, to 'test1.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.65.100
    Stream #0:0: Video: h264 (avc1 / 0x31637661), yuv420p(tv, progressive), 1440x1080 [SAR 1:1 DAR 4:3], q=2-31, 25 fps, 12800 tbn (de
fault)
    Metadata:
      encoder         : Lavc58.115.102 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
    Stream #0:1: Audio: opus (Opus / 0x7375704F), 48000 Hz, stereo, flt, 96 kb/s (default)
    Metadata:
      encoder         : Lavc58.115.102 libopus
Error while filtering: Cannot allocate memorye=00:16:48.79 bitrate=7107.6kbits/s speed=3.19x
    Last message repeated 1 times
Failed to inject frame into filter network: Cannot allocate memory
Error while processing the decoded data for stream #1:0
[libx264 @ 000002475283b600] frame I:101   Avg QP: 7.17  size:104091
[libx264 @ 000002475283b600] frame P:25125 Avg QP: 9.65  size: 34810
[libx264 @ 000002475283b600] mb I  I16..4: 100.0%  0.0%  0.0%
[libx264 @ 000002475283b600] mb P  I16..4:  3.9%  0.0%  0.0%  P16..4: 37.3%  0.0%  0.0%  0.0%  0.0%    skip:58.8%
[libx264 @ 000002475283b600] coded y,uvDC,uvAC intra: 36.5% 45.6% 16.1% inter: 23.8% 13.9% 0.3%
[libx264 @ 000002475283b600] i16 v,h,dc,p: 43% 29% 11% 16%
[libx264 @ 000002475283b600] i8c dc,h,v,p: 39% 26% 14% 21%
[libx264 @ 000002475283b600] kb/s:7017.47
[libopus @ 0000024752837f40] 1 frames left in the queue on closing
Conversion failed!

Is this a bug, or just something in my command that I'm missing?


Solution 1:

After a few days of grappling with this problem I've determined that, at least in my own experience, it's always caused by numbers that don't add up, such as an -ss that exceeds the length of the video or a miscalculated SETPTS values.

I still believe this is a bug for two reasons:

  • Because it isn't just a case of FFmpeg exhausting through all available memory as I first suspected, but using up significant amounts of memory quickly before eventually crashing while there's still memory available

  • Because FFmpeg should really detect and throw errors when nonsensical parameters are inevitably going to consume large amounts of memory before crashing to a halt

That being said, because it's caused by the user failing to accurately calculate the values of SETPTS properly (which, it has to be said, is not easy to do and requires some serious thinking through), it isn't as critical a bug as I first suspected.