filter - mixing transparent FLV video and background-image with FFMPEG

24
2013-08
  • Ernie

    I'm having a hard time mixing a background png with a transparent flv. I scale and crop an flv video with filter_complex, and then i want to overlay it on a background png.

    When I put the png in the background by setting it as the first input in the overlay filter i'm getting errors, and the result is a video with only the png and audio from the video. This is my command + complete output:

    iMacBert:test bert$ ffmpeg -i test.flv -i test.png -filter_complex '[0:v]scale=max(552\, 420*iw/ih):-1[OUT1]; [OUT1]crop=552:420[OUT2];[1:v][OUT2]overlay[out]' -map [out] -map 0:a -t 00:00:10 -s 552x420 out.mp4
    ffmpeg version 1.1.2 Copyright (c) 2000-2013 the FFmpeg developers
      built on Mar  4 2013 11:19:29 with Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
      configuration: --prefix=/opt/local --enable-swscale --enable-avfilter --enable-libmp3lame --enable-libvorbis --enable-libopus --enable-libtheora --enable-libschroedinger --enable-libopenjpeg --enable-libmodplug --enable-libvpx --enable-libspeex --enable-libfreetype --mandir=/opt/local/share/man --enable-shared --enable-pthreads --cc=/usr/bin/clang --arch=x86_64 --enable-yasm --enable-gpl --enable-postproc --enable-libx264 --enable-libxvid --enable-nonfree --enable-libfaac
      libavutil      52. 13.100 / 52. 13.100
      libavcodec     54. 86.100 / 54. 86.100
      libavformat    54. 59.106 / 54. 59.106
      libavdevice    54.  3.102 / 54.  3.102
      libavfilter     3. 32.100 /  3. 32.100
      libswscale      2.  1.103 /  2.  1.103
      libswresample   0. 17.102 /  0. 17.102
      libpostproc    52.  2.100 / 52.  2.100
    Input #0, flv, from 'test.flv':
      Metadata:
        audiodelay      : 0
        canSeekToEnd    : true
      Duration: 00:00:41.48, start: 0.000000, bitrate: 6444 kb/s
        Stream #0:0: Video: vp6a, yuva420p, 1920x1080, 10240 kb/s, 25 tbr, 1k tbn, 1k tbc
        Stream #0:1: Audio: mp3, 44100 Hz, stereo, s16p, 128 kb/s
    Input #1, image2, from 'test.png':
      Duration: 00:00:00.04, start: 0.000000, bitrate: N/A
        Stream #1:0: Video: png, rgba, 552x420 [SAR 2835:2835 DAR 46:35], 25 tbr, 25 tbn, 25 tbc
    File 'out.mp4' already exists. Overwrite ? [y/N] y
    using SAR=1/1
    [libx264 @ 0x7fe10b042a00] using cpu capabilities: none!
    [libx264 @ 0x7fe10b042a00] profile High, level 3.0
    [libx264 @ 0x7fe10b042a00] 264 - core 128 - H.264/MPEG-4 AVC codec - Copyleft 2003-2012 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
    Output #0, mp4, to 'out.mp4':
      Metadata:
        audiodelay      : 0
        canSeekToEnd    : true
        encoder         : Lavf54.59.106
        Stream #0:0: Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 552x420 [SAR 1:1 DAR 46:35], q=-1--1, 12800 tbn, 25 tbc
        Stream #0:1: Audio: aac ([64][0][0][0] / 0x0040), 44100 Hz, stereo, s16, 128 kb/s
    Stream mapping:
      Stream #0:0 (vp6a) -> scale (graph 0)
      Stream #1:0 (png) -> overlay:main (graph 0)
      overlay (graph 0) -> Stream #0:0 (libx264)
      Stream #0:1 -> #0:1 (mp3 -> libfaac)
    Press [q] to stop, [?] for help
    [Parsed_overlay_2 @ 0x7fe10ac1e680] Buffer queue overflow, dropping.
        Last message repeated 14 times
    Buffer queue overflow, dropping.      0kB time=00:00:01.81 bitrate=   0.2kbits/s    
    [Parsed_overlay_2 @ 0x7fe10ac1e680] Buffer queue overflow, dropping.
        Last message repeated 47 times
    Buffer queue overflow, dropping.      0kB time=00:00:03.76 bitrate=   0.1kbits/s    
    [Parsed_overlay_2 @ 0x7fe10ac1e680] Buffer queue overflow, dropping.
        Last message repeated 50 times
    Buffer queue overflow, dropping.      0kB time=00:00:05.85 bitrate=   0.1kbits/s    
    [Parsed_overlay_2 @ 0x7fe10ac1e680] Buffer queue overflow, dropping.
        Last message repeated 50 times
    Buffer queue overflow, dropping.      0kB time=00:00:07.91 bitrate=   0.0kbits/s    
    [Parsed_overlay_2 @ 0x7fe10ac1e680] Buffer queue overflow, dropping.
        Last message repeated 48 times
    frame=    1 fps=0.4 q=28.0 Lsize=     162kB time=00:00:10.00 bitrate= 132.4kbits/s    
    video:2kB audio:156kB subtitle:0 global headers:0kB muxing overhead 1.862288%
    [libx264 @ 0x7fe10b042a00] frame I:1     Avg QP:26.10  size:  1776
    [libx264 @ 0x7fe10b042a00] mb I  I16..4: 61.4% 35.3%  3.3%
    [libx264 @ 0x7fe10b042a00] 8x8 transform intra:35.3%
    [libx264 @ 0x7fe10b042a00] coded y,uvDC,uvAC intra: 18.6% 13.4% 2.2%
    [libx264 @ 0x7fe10b042a00] i16 v,h,dc,p: 65% 23%  1% 11%
    [libx264 @ 0x7fe10b042a00] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 27% 29% 23%  5%  3%  3%  4%  2%  4%
    [libx264 @ 0x7fe10b042a00] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 18% 64% 16%  0%  0%  0%  0%  0%  1%
    [libx264 @ 0x7fe10b042a00] i8c dc,h,v,p: 72% 19%  9%  0%
    [libx264 @ 0x7fe10b042a00] kb/s:355.20
    

    Strangely when I put it on the foreground, by setting it as the second parameter I don't get errors, but this is not what i want to achieve, the image has to be in the background.

    Anyone has a solution?

    This is my command word-wrapped which is easier to read:

    ffmpeg -i test.flv -i test.png -filter_complex '[0:v]scale=max(552\, 420*iw/ih):-1[OUT1]; [OUT1]crop=552:420[OUT2];[1:v][OUT2]overlay[out]' -map [out] -map 0:a -t 00:00:10 -s 552x420 out.mp4

  • Answers
  • LordNeckbeard

    You must loop your PNG file, since you are getting a Buffer queue overflow, dropping. error.

    ffmpeg -i test.flv -loop 1 -i test.png -filter_complex '[0:v]scale=max(552\, 420*iw/ih):-1[OUT1]; [OUT1]crop=552:420[OUT2];[1:v][OUT2]overlay[out]' -map [out] -map 0:a -t 00:00:10 out.mp4
    

    If that's not working it has something to do with the frame sizes. This Stack Overflow question might help you as well: Superimposing two videos onto a static image


  • Related Question

    ffmpeg trying to remove the first few seconds of video
  • Alan Blount

    I'm trying to remove a few seconds off the front of a video stream (audio already excluded), and I'm getting strange results: I would think that for every second I increase -ss by, the resulting file would be a second shorter... that doesn't seem to be the case.

    original     ==> 01:04:52.84
    -ss 19       ==> 01:04:42.84 (diff = 10) [command history shown below]
    -ss 20       ==> 01:04:32.84 (diff = 20) [command history shown below]
    -ss 21       ==> 01:04:32.84 (diff = 20)
    -ss 25       ==> 01:04:32.84 (diff = 20)
    -ss 0:0:25.0 ==> 01:04:32.84 (diff = 20)
    -ss 0:0:25.5 ==> 01:04:32.84 (diff = 20) [command history shown below]
    

    Command: ffmpeg -ss # -i temp.mp4 -y -vcodec copy temp_croppedFromStart.mp4

    Here's the command history for 19 & 20

    # ffmpeg -ss 19 -i temp.mp4 -y -vcodec copy  temp_croppedFromStart.mp4; ffmpeg -i temp.mp4 2>&1 | grep Duration; ffmpeg -i temp_croppedFromStart.mp4 2>&1 | grep Duration
    ffmpeg version N-31809-g9acffed, Copyright (c) 2000-2011 the FFmpeg developers
      built on Aug 10 2011 21:25:11 with gcc 4.4.5
      configuration: --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid --enable-x11grab
      libavutil    51. 11. 1 / 51. 11. 1
      libavcodec   53. 10. 0 / 53. 10. 0
      libavformat  53.  6. 0 / 53.  6. 0
      libavdevice  53.  2. 0 / 53.  2. 0
      libavfilter   2. 28. 1 /  2. 28. 1
      libswscale    2.  0. 0 /  2.  0. 0
      libpostproc  51.  2. 0 / 51.  2. 0
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'temp.mp4':
      Metadata:
        major_brand     : isom
        minor_version   : 512
        compatible_brands: isomiso2avc1mp41
        creation_time   : 1970-01-01 00:00:00
        encoder         : Lavf53.6.0
      Duration: 01:04:52.84, start: 0.000000, bitrate: 553 kb/s
        Stream #0.0(und): Video: h264 (Constrained Baseline), yuv420p, 960x640, 552 kb/s, 25 fps, 25 tbr, 25 tbn, 50 tbc
        Metadata:
          creation_time   : 1970-01-01 00:00:00
    Output #0, mp4, to 'temp_croppedFromStart.mp4':
      Metadata:
        major_brand     : isom
        minor_version   : 512
        compatible_brands: isomiso2avc1mp41
        creation_time   : 1970-01-01 00:00:00
        encoder         : Lavf53.6.0
        Stream #0.0(und): Video: libx264, yuv420p, 960x640, q=2-31, 552 kb/s, 25 tbn, 25 tbc
        Metadata:
          creation_time   : 1970-01-01 00:00:00
    Stream mapping:
      Stream #0.0 -> #0.0
    Press [q] to stop, [?] for help
    frame=97071 fps=42523 q=-1.0 Lsize=  262684kB time=01:04:33.84 bitrate= 555.5kbits/s
    video:261923kB audio:0kB global headers:0kB muxing overhead 0.290418%
      Duration: 01:04:52.84, start: 0.000000, bitrate: 553 kb/s
      Duration: 01:04:42.84, start: 0.000000, bitrate: 554 kb/s
    
    
    # ffmpeg -ss 20 -i temp.mp4 -y -vcodec copy  temp_croppedFromStart.mp4; ffmpeg -i temp.mp4 2>&1 | grep Duration; ffmpeg -i temp_croppedFromStart.mp4 2>&1 | grep Duration
    ffmpeg version N-31809-g9acffed, Copyright (c) 2000-2011 the FFmpeg developers
      built on Aug 10 2011 21:25:11 with gcc 4.4.5
      configuration: --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid --enable-x11grab
      libavutil    51. 11. 1 / 51. 11. 1
      libavcodec   53. 10. 0 / 53. 10. 0
      libavformat  53.  6. 0 / 53.  6. 0
      libavdevice  53.  2. 0 / 53.  2. 0
      libavfilter   2. 28. 1 /  2. 28. 1
      libswscale    2.  0. 0 /  2.  0. 0
      libpostproc  51.  2. 0 / 51.  2. 0
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'temp.mp4':
      Metadata:
        major_brand     : isom
        minor_version   : 512
        compatible_brands: isomiso2avc1mp41
        creation_time   : 1970-01-01 00:00:00
        encoder         : Lavf53.6.0
      Duration: 01:04:52.84, start: 0.000000, bitrate: 553 kb/s
        Stream #0.0(und): Video: h264 (Constrained Baseline), yuv420p, 960x640, 552 kb/s, 25 fps, 25 tbr, 25 tbn, 50 tbc
        Metadata:
          creation_time   : 1970-01-01 00:00:00
    Output #0, mp4, to 'temp_croppedFromStart.mp4':
      Metadata:
        major_brand     : isom
        minor_version   : 512
        compatible_brands: isomiso2avc1mp41
        creation_time   : 1970-01-01 00:00:00
        encoder         : Lavf53.6.0
        Stream #0.0(und): Video: libx264, yuv420p, 960x640, q=2-31, 552 kb/s, 25 tbn, 25 tbc
        Metadata:
          creation_time   : 1970-01-01 00:00:00
    Stream mapping:
      Stream #0.0 -> #0.0
    Press [q] to stop, [?] for help
    frame=96821 fps=47168 q=-1.0 Lsize=  262003kB time=01:04:32.84 bitrate= 554.2kbits/s
    video:261244kB audio:0kB global headers:0kB muxing overhead 0.290410%
      Duration: 01:04:52.84, start: 0.000000, bitrate: 553 kb/s
      Duration: 01:04:32.84, start: 0.000000, bitrate: 554 kb/s
    
    # ffmpeg -ss 0:0:25.5 -i temp.mp4 -y -vcodec copy  temp_croppedFromStart.mp4; ffmpeg -i temp.mp4 2>&1 | grep Duration; ffmpeg -i temp_croppedFromStart.mp4 2>&1 | grep Duration
    ffmpeg version N-31809-g9acffed, Copyright (c) 2000-2011 the FFmpeg developers
      built on Aug 10 2011 21:25:11 with gcc 4.4.5
      configuration: --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid --enable-x11grab
      libavutil    51. 11. 1 / 51. 11. 1
      libavcodec   53. 10. 0 / 53. 10. 0
      libavformat  53.  6. 0 / 53.  6. 0
      libavdevice  53.  2. 0 / 53.  2. 0
      libavfilter   2. 28. 1 /  2. 28. 1
      libswscale    2.  0. 0 /  2.  0. 0
      libpostproc  51.  2. 0 / 51.  2. 0
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'temp.mp4':
      Metadata:
        major_brand     : isom
        minor_version   : 512
        compatible_brands: isomiso2avc1mp41
        creation_time   : 1970-01-01 00:00:00
        encoder         : Lavf53.6.0
      Duration: 01:04:52.84, start: 0.000000, bitrate: 553 kb/s
        Stream #0.0(und): Video: h264 (Constrained Baseline), yuv420p, 960x640, 552 kb/s, 25 fps, 25 tbr, 25 tbn, 50 tbc
        Metadata:
          creation_time   : 1970-01-01 00:00:00
    Output #0, mp4, to 'temp_croppedFromStart.mp4':
      Metadata:
        major_brand     : isom
        minor_version   : 512
        compatible_brands: isomiso2avc1mp41
        creation_time   : 1970-01-01 00:00:00
        encoder         : Lavf53.6.0
        Stream #0.0(und): Video: libx264, yuv420p, 960x640, q=2-31, 552 kb/s, 25 tbn, 25 tbc
        Metadata:
          creation_time   : 1970-01-01 00:00:00
    Stream mapping:
      Stream #0.0 -> #0.0
    Press [q] to stop, [?] for help
    frame=96821 fps=45920 q=-1.0 Lsize=  262003kB time=01:04:27.32 bitrate= 555.0kbits/s
    video:261244kB audio:0kB global headers:0kB muxing overhead 0.290424%
      Duration: 01:04:52.84, start: 0.000000, bitrate: 553 kb/s
      Duration: 01:04:32.84, start: 0.000000, bitrate: 554 kb/s
    

  • Related Answers
  • Steven Penny

    With H.264 video you have group of pictures.

    To save space some of the frames are inter frames

    You can only cut H.264 on an Intra-frame AKA I-frame.

    What I did was transcode using a lossless codec, then reencode

    ffmpeg -i input.mkv -c:v ffvhuff FFVhuff.mkv
    
    ffmpeg -ss 26.818 -i FFVhuff.mkv trim.mkv
    

    http://svnpenn.blogspot.com/2011/10/ffmpeg-cut-to-frame-same-audio.html