alpha channel - How do i convert a transparent .mov-file to an mp4 file overlayed on a background-image using FFMPEG

24
2013-08
  • Ernie

    I'm trying to convert a mov file with the following specs:

    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'test.mov':
    Metadata:
        major_brand     : qt  
        minor_version   : 537199360
        compatible_brands: qt  
        creation_time   : 2012-10-09 13:34:01
    Duration: 00:00:51.60, start: 0.000000, bitrate: 217433 kb/s
        Stream #0:0(eng): Video: prores (ap4h / 0x68347061), yuv444p10le, 1920x1080, 215844 kb/s, SAR 1:1 DAR 16:9, 25 fps, 25 tbr, 25 tbn, 25 tbc
        Metadata:
            creation_time   : 2012-10-09 13:34:01
            handler_name    : Apple Alias Data Handler
            timecode        : 01:00:00:00
        Stream #0:1(eng): Audio: pcm_s16le (sowt / 0x74776F73), 48000 Hz, stereo, s16, 1536 kb/s
        Metadata:
            creation_time   : 2012-10-09 13:34:01
            handler_name    : Apple Alias Data Handler
        Stream #0:2(eng): Data: none (tmcd / 0x64636D74)
        Metadata:
            creation_time   : 2012-10-09 13:42:30
            handler_name    : Apple Alias Data Handler
            timecode        : 01:00:00:00
    

    I want a background-image with that video on top of it and save it as an mp4-file. In the meantime the video has to be scaled and cropped I need a solution which can be automated and placed on a server, that is why i tried ffmpeg

    This is wat have at the moment:

    ffmpeg -loop 1 -f image2 -i background.png -r 25 -an -vcodec libx264 -b:v 1500k -vf "movie=test.mov[movie];[movie]scale=max(552\, 420*iw/ih):-1[OUT1]; [OUT1]crop=552:420[OUT2]; [0][OUT2] overlay=0:0 [out]" -s 552x420 -y output.mp4

    The problem is, somehow, the transparency is lost, and the background is black, instead of the underlying image. When i try the same ffmpeg command with a transparent FLV as input, it works, but i would like it to work without having to convert al source files first to another format

    Any help with this would be greatly appreciated !

    This is my current ffmpeg configuration:

    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
    

    EDIT

    This is the complete console output for the command:

    ffmpeg -loop 1 -f image2 -i background.jpg -r 25 -an -vframes 2500  -vcodec libx264 -b:v 1500k -vf "movie=test.mov[movie];[movie]scale=max(552\, 420*iw/ih):-1[OUT1]; [OUT1]crop=552:420[OUT2]; [0][OUT2] overlay=0:0 [out]" -s 552x420 -y output.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, image2, from 'background.jpg':
      Duration: 00:00:00.04, start: 0.000000, bitrate: N/A
        Stream #0:0: Video: mjpeg, yuvj444p, 1024x576, 25 fps, 25 tbr, 25 tbn, 25 tbc
    [libx264 @ 0x7fddc3813600] using cpu capabilities: none!
    [libx264 @ 0x7fddc3813600] profile High, level 3.0
    [libx264 @ 0x7fddc3813600] 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=abr mbtree=1 bitrate=1500 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
    Output #0, mp4, to 'output.mp4':
      Metadata:
        encoder         : Lavf54.59.106
        Stream #0:0: Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 552x420, q=-1--1, 1500 kb/s, 12800 tbn, 25 tbc
    Stream mapping:
      Stream #0:0 -> #0:0 (mjpeg -> libx264)
    Press [q] to stop, [?] for help
    frame= 2500 fps= 53 q=32766.0 Lsize=    9772kB time=00:01:39.92 bitrate= 801.2kbits/s    
    video:9742kB audio:0kB subtitle:0 global headers:0kB muxing overhead 0.309877%
    [libx264 @ 0x7fddc3813600] frame I:10    Avg QP: 1.97  size: 58522
    [libx264 @ 0x7fddc3813600] frame P:634   Avg QP: 4.19  size:  7889
    [libx264 @ 0x7fddc3813600] frame B:1856  Avg QP: 7.63  size:  2364
    [libx264 @ 0x7fddc3813600] consecutive B-frames:  1.0%  0.0%  0.1% 98.9%
    [libx264 @ 0x7fddc3813600] mb I  I16..4: 47.6% 16.4% 36.0%
    [libx264 @ 0x7fddc3813600] mb P  I16..4:  0.1%  0.1%  0.3%  P16..4:  2.9%  2.9%  2.6%  0.0%  0.0%    skip:91.1%
    [libx264 @ 0x7fddc3813600] mb B  I16..4:  0.0%  0.0%  0.0%  B16..8:  2.3%  1.1%  1.1%  direct: 3.0%  skip:92.4%  L0:23.8% L1:24.1% BI:52.0%
    [libx264 @ 0x7fddc3813600] final ratefactor: -0.23
    [libx264 @ 0x7fddc3813600] 8x8 transform intra:16.7% inter:26.0%
    [libx264 @ 0x7fddc3813600] coded y,uvDC,uvAC intra: 65.5% 84.3% 83.9% inter: 5.8% 4.8% 3.8%
    [libx264 @ 0x7fddc3813600] i16 v,h,dc,p: 60% 21%  8% 12%
    [libx264 @ 0x7fddc3813600] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 26% 31%  9%  3%  5%  8%  6%  6%  5%
    [libx264 @ 0x7fddc3813600] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 22% 27% 11%  4%  7%  9%  7%  6%  6%
    [libx264 @ 0x7fddc3813600] i8c dc,h,v,p: 34% 43% 17%  7%
    [libx264 @ 0x7fddc3813600] Weighted P-Frames: Y:0.0% UV:0.0%
    [libx264 @ 0x7fddc3813600] ref P L0: 61.2% 19.9% 10.0%  8.8%
    [libx264 @ 0x7fddc3813600] ref B L0: 91.0%  7.3%  1.7%
    [libx264 @ 0x7fddc3813600] ref B L1: 98.7%  1.3%
    [libx264 @ 0x7fddc3813600] kb/s:798.01
    
  • Answers
  • mark4o

    ffmpeg does not currently support ProRes alpha channels. (This is an open enhancement request.) If you can instead use the QuickTime Animation (RLE) codec (qtrle) for the overlay movie then that should work.


  • Related Question

    ubuntu - Conversion from WMV to MP4 file fails with FFmpeg
  • Tim

    When converting a WMV file to an MP4 file, it says

    [mp4 @ 0x989c160] track 1: could not find tag, codec not currently supported in container

    and

    Could not write header for output file #0 (incorrect codec parameters ?): Operation not permitted.

    But converting the WMV file to an MKV file is fine. Wonder why?

    Following are the outputs of both cases.

    Converting from the wmv file to a mp4 file is not working

    $ ffmpeg -i "1.wmv" -c:v libx264 -crf 23 -c:a copy  "1.mp4" 
    ffmpeg version git-2012-06-03-169dae8 Copyright (c) 2000-2012 the FFmpeg developers
      built on Jun  3 2012 09:14:54 with gcc 4.6.3
      configuration: --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-nonfree --enable-version3 --enable-x11grab
      libavutil      51. 56.100 / 51. 56.100
      libavcodec     54. 23.100 / 54. 23.100
      libavformat    54.  6.101 / 54.  6.101
      libavdevice    54.  0.100 / 54.  0.100
      libavfilter     2. 77.100 /  2. 77.100
      libswscale      2.  1.100 /  2.  1.100
      libswresample   0. 15.100 /  0. 15.100
      libpostproc    52.  0.100 / 52.  0.100
    Guessed Channel Layout for  Input Stream #0.0 : stereo
    Input #0, asf, from '1.wmv':
      Metadata:
        WMFSDKVersion   : 9.00.00.3267
        WMFSDKNeeded    : 0.0.0.0000
        IsVBR           : 0
      Duration: 01:40:50.70, start: 0.000000, bitrate: 2100 kb/s
        Stream #0:0(eng): Audio: wmav2 (a[1][0][0] / 0x0161), 44100 Hz, stereo, s16, 128 kb/s
        Stream #0:1(eng): Video: wmv3 (Main) (WMV3 / 0x33564D57), yuv420p, 640x480, 1974 kb/s, 29.97 tbr, 1k tbn, 1k tbc
    [buffer @ 0x98c28c0] w:640 h:480 pixfmt:yuv420p tb:1/1000 sar:0/1 sws_param:flags=2
    [buffersink @ 0x9894320] No opaque field provided
    [libx264 @ 0x98b0ec0] using cpu capabilities: MMX2 SSE2Fast SSSE3 FastShuffle SSE4.1 Cache64
    [libx264 @ 0x98b0ec0] profile High, level 3.0
    [libx264 @ 0x98b0ec0] 264 - core 125 r2200 999b753 - 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=3 lookahead_threads=1 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
    [mp4 @ 0x989c160] track 1: could not find tag, codec not currently supported in container
    Output #0, mp4, to '1.mp4':
      Metadata:
        WMFSDKVersion   : 9.00.00.3267
        WMFSDKNeeded    : 0.0.0.0000
        IsVBR           : 0
        encoder         : Lavf54.6.101
        Stream #0:0(eng): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 640x480, q=-1--1, 30k tbn, 29.97 tbc
        Stream #0:1(eng): Audio: wmav2 (a[1][0][0] / 0x0161), 44100 Hz, stereo, 128 kb/s
    Stream mapping:
      Stream #0:1 -> #0:0 (wmv3 -> libx264)
      Stream #0:0 -> #0:1 (copy)
    Could not write header for output file #0 (incorrect codec parameters ?): Operation not permitted
    

    Converting from the wmv file to a mkv file is fine:

    $ ffmpeg -i "1.wmv" -c:v libx264 -crf 23 -c:a copy  "1.mkv" 
    ffmpeg version git-2012-06-03-169dae8 Copyright (c) 2000-2012 the FFmpeg developers
      built on Jun  3 2012 09:14:54 with gcc 4.6.3
      configuration: --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-nonfree --enable-version3 --enable-x11grab
      libavutil      51. 56.100 / 51. 56.100
      libavcodec     54. 23.100 / 54. 23.100
      libavformat    54.  6.101 / 54.  6.101
      libavdevice    54.  0.100 / 54.  0.100
      libavfilter     2. 77.100 /  2. 77.100
      libswscale      2.  1.100 /  2.  1.100
      libswresample   0. 15.100 /  0. 15.100
      libpostproc    52.  0.100 / 52.  0.100
    Guessed Channel Layout for  Input Stream #0.0 : stereo
    Input #0, asf, from '1.wmv':
      Metadata:
        WMFSDKVersion   : 9.00.00.3267
        WMFSDKNeeded    : 0.0.0.0000
        IsVBR           : 0
      Duration: 01:40:50.70, start: 0.000000, bitrate: 2100 kb/s
        Stream #0:0(eng): Audio: wmav2 (a[1][0][0] / 0x0161), 44100 Hz, stereo, s16, 128 kb/s
        Stream #0:1(eng): Video: wmv3 (Main) (WMV3 / 0x33564D57), yuv420p, 640x480, 1974 kb/s, 29.97 tbr, 1k tbn, 1k tbc
    File '1.mkv' already exists. Overwrite ? [y/N] y
    w:640 h:480 pixfmt:yuv420p tb:1/1000 sar:0/1 sws_param:flags=2
    [buffersink @ 0xa0a2760] No opaque field provided
    [libx264 @ 0xa0b6ec0] using cpu capabilities: MMX2 SSE2Fast SSSE3 FastShuffle SSE4.1 Cache64
    [libx264 @ 0xa0b6ec0] profile High, level 3.0
    [libx264 @ 0xa0b6ec0] 264 - core 125 r2200 999b753 - 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=3 lookahead_threads=1 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, matroska, to '1.mkv':
      Metadata:
        WMFSDKVersion   : 9.00.00.3267
        WMFSDKNeeded    : 0.0.0.0000
        IsVBR           : 0
        encoder         : Lavf54.6.101
        Stream #0:0(eng): Video: h264, yuv420p, 640x480, q=-1--1, 1k tbn, 29.97 tbc
        Stream #0:1(eng): Audio: wmav2 (a[1][0][0] / 0x0161), 44100 Hz, stereo, 128 kb/s
    Stream mapping:
      Stream #0:1 -> #0:0 (wmv3 -> libx264)
      Stream #0:0 -> #0:1 (copy)
    Press [q] to stop, [?] for help
    frame=  325 fps=106 q=29.0 size=     411kB time=00:00:09.20 bitrate= 366.0kbits/s    
    

  • Related Answers
  • slhck

    codec not currently supported in container

    That's your error.

    Try specifying another audio codec like with -c:a libfaac. MP4 doesn't take Windows Media Audio in its container. You have to choose a matching audio codec, like AAC. Right now all you do is try to copy the bitstream, and that fails – as indicated by the error message.

    The same problem of non-matching codecs goes for other containers, although MKV is way more open to other codecs than MP4 is, therefore you can use almost any codecs within an MKV container. This does not work for MP4.

    In general though, for maximum compatibility, try to stick to the most common formats for containers, e.g.

    • AVI: MPEG-4 video, MP3 audio
    • WMV: Windows Media Video and Audio
    • MKV, MOV, MP4: h.264, AAC audio (although MKV really takes anything, here the limitation is on the playback device side)
  • Justin Buser

    try just doing:

    ffmpeg -y -i 1.wmv -b:v 2000k 1.mp4
    

    ffmpeg should automatically pick the most appropriate audio format and use libx264 for the video, the 2000k is essentially telling it to encode at the same bitrate as the original video file.