1

I have one ffmpeg command that merges 3 mp4 videos together and then another command that applies a watermark to the output video of the first command. The commands are:

Merge mp4 videos with blending them together

ffmpeg -y -threads 0  -i vid-1.mp4 -i vid-2.mp4 -i vid-3.mp4 -f lavfi -i color=black -filter_complex "[0:v]format=pix_fmts=yuva420p,fade=t=in:st=0:d=0.5:alpha=1,fade=t=out:st=5:d=0.5:alpha=1,setpts=PTS-STARTPTS[va0];[1:v]format=pix_fmts=yuva420p,fade=t=in:st=0:d=0.5:alpha=1,fade=t=out:st=5:d=0.5:alpha=1,setpts=PTS-STARTPTS+5/TB[va1];[2:v]format=pix_fmts=yuva420p,fade=t=in:st=0:d=0.5:alpha=1,fade=t=out:st=5:d=0.5:alpha=1,setpts=PTS-STARTPTS+10/TB[va2];[3:v]scale=1280x720,trim=duration=15[over0];[over0][va0]overlay[over1];[over1][va1]overlay[over2];[over2][va2]overlay=format=yuv420[outv]" -vcodec libx264 -map [outv] -s 1280x720 -aspect 16:9 -r 30 -crf 1 -preset ultrafast output.mp4

Output:

[jstevens@jr testing]$ ffmpeg -y -threads 0  -i vid-1.mp4 -i vid-2.mp4 -i vid-3.mp4 -f lavfi -i color=black -filter_complex "[0:v]format=pix_fmts=yuva420p,fade=t=in:st=0:d=0.5:alpha=1,fade=t=out:st=5:d=0.5:alpha=1,setpts=PTS-STARTPTS[va0];[1:v]format=pix_fmts=yuva420p,fade=t=in:st=0:d=0.5:alpha=1,fade=t=out:st=5:d=0.5:alpha=1,setpts=PTS-STARTPTS+5/TB[va1];[2:v]format=pix_fmts=yuva420p,fade=t=in:st=0:d=0.5:alpha=1,fade=t=out:st=5:d=0.5:alpha=1,setpts=PTS-STARTPTS+10/TB[va2];[3:v]scale=1280x720,trim=duration=15[over0];[over0][va0]overlay[over1];[over1][va1]overlay[over2];[over2][va2]overlay=format=yuv420[outv]" -vcodec libx264 -map [outv] -s 1280x720 -aspect 16:9 -r 30 -crf 1 -preset ultrafast output.mp4
ffmpeg version 3.0.2 Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 6.1.1 (GCC) 20160510 (Red Hat 6.1.1-2)
  configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --enable-bzlib --disable-crystalhd --enable-frei0r --enable-gnutls --enable-ladspa --enable-libass --enable-libcdio --enable-libdc1394 --disable-indev=jack --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-openal --enable-libopencv --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-x11grab --enable-avfilter --enable-avresample --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-runtime-cpudetect
  libavutil      55. 17.103 / 55. 17.103
  libavcodec     57. 24.102 / 57. 24.102
  libavformat    57. 25.100 / 57. 25.100
  libavdevice    57.  0.101 / 57.  0.101
  libavfilter     6. 31.100 /  6. 31.100
  libavresample   3.  0.  0 /  3.  0.  0
  libswscale      4.  0.100 /  4.  0.100
  libswresample   2.  0.101 /  2.  0.101
  libpostproc    54.  0.100 / 54.  0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'vid-1.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.25.101
  Duration: 00:00:05.00, start: 0.000000, bitrate: 1085 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 1081 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'vid-2.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.25.101
  Duration: 00:00:05.00, start: 0.000000, bitrate: 1018 kb/s
    Stream #1:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 1014 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
Input #2, mov,mp4,m4a,3gp,3g2,mj2, from 'vid-3.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.25.101
  Duration: 00:00:05.00, start: 0.000000, bitrate: 823 kb/s
    Stream #2:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 819 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
Input #3, lavfi, from 'color=black':
  Duration: N/A, start: 0.000000, bitrate: N/A
    Stream #3:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 320x240 [SAR 1:1 DAR 4:3], 25 tbr, 25 tbn, 25 tbc
[libx264 @ 0x125c040] using SAR=1/1
[libx264 @ 0x125c040] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2
[libx264 @ 0x125c040] profile Constrained Baseline, level 3.1
[libx264 @ 0x125c040] 264 - core 148 r2699 a5e06b9 - H.264/MPEG-4 AVC codec - Copyleft 2003-2016 - 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 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=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=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
Output #0, mp4, to 'output.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf57.25.100
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 30 fps, 15360 tbn, 30 tbc (default)
    Metadata:
      encoder         : Lavc57.24.102 libx264
    Side data:
      unknown side data type 10 (24 bytes)
Stream mapping:
  Stream #0:0 (h264) -> format
  Stream #1:0 (h264) -> format
  Stream #2:0 (h264) -> format
  Stream #3:0 (rawvideo) -> scale
  overlay -> Stream #0:0 (libx264)
Press [q] to stop, [?] for help
frame=  450 fps=118 q=-1.0 Lsize=  133025kB time=00:00:15.00 bitrate=72649.6kbits/s dup=75 drop=0 speed=3.93x    
video:133021kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.003404%
[libx264 @ 0x125c040] frame I:2     Avg QP: 0.00  size:301111
[libx264 @ 0x125c040] frame P:448   Avg QP: 2.03  size:302702
[libx264 @ 0x125c040] mb I  I16..4: 100.0%  0.0%  0.0%
[libx264 @ 0x125c040] mb P  I16..4:  8.1%  0.0%  0.0%  P16..4: 90.2%  0.0%  0.0%  0.0%  0.0%    skip: 1.8%
[libx264 @ 0x125c040] coded y,uvDC,uvAC intra: 97.1% 94.1% 93.7% inter: 90.5% 69.8% 68.7%
[libx264 @ 0x125c040] i16 v,h,dc,p: 35% 55%  6%  3%
[libx264 @ 0x125c040] i8c dc,h,v,p: 21% 43% 28%  7%
[libx264 @ 0x125c040] kb/s:72646.80

Apply watermark

ffmpeg -y -threads 0 -i output.mp4 -i watermark.png -filter_complex fade=t=in:st=0:d=0.5,fade=t=out:st=14.5:d=0.5,overlay=(main_w-overlay_w):(main_h-overlay_h) -pix_fmt yuv420p -crf 1 -preset ultrafast final.mp4

Output:

[jstevens@jr testing]$ ffmpeg -y -threads 0 -i output.mp4 -i watermark.png -filter_complex "fade=t=in:st=0:d=0.5,fade=t=out:st=14.5:d=0.5,overlay=(main_w-overlay_w):(main_h-overlay_h)" -pix_fmt yuv420p -crf 1 -preset ultrafast final.mp4
ffmpeg version 3.0.2 Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 6.1.1 (GCC) 20160510 (Red Hat 6.1.1-2)
  configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --enable-bzlib --disable-crystalhd --enable-frei0r --enable-gnutls --enable-ladspa --enable-libass --enable-libcdio --enable-libdc1394 --disable-indev=jack --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-openal --enable-libopencv --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-x11grab --enable-avfilter --enable-avresample --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-runtime-cpudetect
  libavutil      55. 17.103 / 55. 17.103
  libavcodec     57. 24.102 / 57. 24.102
  libavformat    57. 25.100 / 57. 25.100
  libavdevice    57.  0.101 / 57.  0.101
  libavfilter     6. 31.100 /  6. 31.100
  libavresample   3.  0.  0 /  3.  0.  0
  libswscale      4.  0.100 /  4.  0.100
  libswresample   2.  0.101 /  2.  0.101
  libpostproc    54.  0.100 / 54.  0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'output.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf57.25.100
  Duration: 00:00:15.00, start: 0.000000, bitrate: 72649 kb/s
    Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 72647 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
    Metadata:
      handler_name    : VideoHandler
Input #1, png_pipe, from 'watermark.png':
  Duration: N/A, bitrate: N/A
    Stream #1:0: Video: png, rgb24(pc), 1280x720 [SAR 1:1 DAR 16:9], 25 tbr, 25 tbn, 25 tbc
[libx264 @ 0x1b80e00] using SAR=1/1
[libx264 @ 0x1b80e00] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2
[libx264 @ 0x1b80e00] profile Constrained Baseline, level 3.1
[libx264 @ 0x1b80e00] 264 - core 148 r2699 a5e06b9 - H.264/MPEG-4 AVC codec - Copyleft 2003-2016 - 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 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=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=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
Output #0, mp4, to 'final.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf57.25.100
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 30 fps, 15360 tbn, 30 tbc (default)
    Metadata:
      encoder         : Lavc57.24.102 libx264
    Side data:
      unknown side data type 10 (24 bytes)
Stream mapping:
  Stream #0:0 (h264) -> fade
  Stream #1:0 (png) -> overlay:overlay
  overlay -> Stream #0:0 (libx264)
Press [q] to stop, [?] for help
frame=  450 fps=188 q=-1.0 Lsize=    1470kB time=00:00:15.00 bitrate= 802.8kbits/s speed=6.26x    
video:1467kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.175498%
[libx264 @ 0x1b80e00] frame I:2     Avg QP: 0.00  size:573234
[libx264 @ 0x1b80e00] frame P:448   Avg QP: 0.02  size:   794
[libx264 @ 0x1b80e00] mb I  I16..4: 100.0%  0.0%  0.0%
[libx264 @ 0x1b80e00] mb P  I16..4:  0.0%  0.0%  0.0%  P16..4:  4.3%  0.0%  0.0%  0.0%  0.0%    skip:95.7%
[libx264 @ 0x1b80e00] coded y,uvDC,uvAC intra: 100.0% 94.1% 93.9% inter: 1.2% 0.1% 0.1%
[libx264 @ 0x1b80e00] i16 v,h,dc,p: 24% 32% 22% 23%
[libx264 @ 0x1b80e00] i8c dc,h,v,p: 29% 34% 25% 13%
[libx264 @ 0x1b80e00] kb/s:801.06

Is there anyway I can apply the watermark at the same time as blending the mp4's? I want to be able to do this in one command.

5
  • Remember to always include the complete console output with each command (or at least the first one in this case); you'll get answers faster and they will be more accurate. What's the reason for adding -threads 0 as an input option? Why use 1 as your crf value?
    – llogan
    Commented Oct 4, 2016 at 16:39
  • I will add the console output. But, this is meant to run concurrent alongside multiple processes and the threads are determined by how many processes are running so the cores are distributed evenly between the ffmpeg commands. I was only running one process for this so it is allocating all cores to the command. And I'm using "-crf 1" so the quality is as high as possible
    – jrkt
    Commented Oct 4, 2016 at 17:04
  • -threads as an input option will only apply to decoding the input that follows it. As for encoding with libx264, -threads 0 is the default; 0 means "automatically choose the appropriate number of threads". Highest quality (lossless) is -crf 0.
    – llogan
    Commented Oct 4, 2016 at 18:16
  • Ok, that makes sense. I thought -crf 1 was the highest quality. I'll need to change that.
    – jrkt
    Commented Oct 4, 2016 at 18:24
  • If you do not need true lossless, with the giant files it produces, you can use -crf 18 or so which is roughly "visually lossless". See FFmpeg Wiki: H.264.
    – llogan
    Commented Oct 5, 2016 at 18:31

1 Answer 1

1
ffmpeg -i vid-1.mp4 -i vid-2.mp4 -i vid-3.mp4 -i vid-4.mp4 -f lavfi -i color=black:r=ntsc-film:s=1280x720:d=18.5 -loop 1 -i watermark.png -filter_complex \
"[0:v]setpts=PTS-STARTPTS,fade=t=out:st=5:d=0.5:alpha=1[va0]; \
 [1:v]format=yuva420p,fade=t=in:st=0:d=0.5:alpha=1,fade=t=out:st=5:d=0.5:alpha=1,setpts=PTS-STARTPTS+4.5/TB[va1]; \
 [2:v]format=yuva420p,fade=t=in:st=0:d=0.5:alpha=1,fade=t=out:st=5:d=0.5:alpha=1,setpts=PTS-STARTPTS+9/TB[va2]; \
 [3:v]format=yuva420p,fade=t=in:st=0:d=0.5:alpha=1,setpts=PTS-STARTPTS+13.5/TB[va3]; \
 [4:v][va0]overlay[over1]; \
 [over1][va1]overlay[over2]; \
 [over2][va2]overlay[over3]; \
 [over3][va3]overlay[over4]; \
 [over4][5:v]overlay=W-w:H-h:shortest=1,fade=t=in:st=0:d=0.5,fade=t=out:st=18:d=0.5,format=yuv420p[outv]" \
-c:v libx264 -map "[outv]" -crf 0 -preset ultrafast output.mp4
  • I eliminated [3:v]scale=1280x720,trim=duration=15[over0] because you can set size and duration (and frame rate) in the color source filter itself.

  • I removed -s 1280x720 -aspect 16:9 because your inputs are already 1280x720 and 16:9 display aspect ratio. I also removed -r 30 because it seems unecessary to change from 25 to 30 frames per second and would result in duplicated frames.

  • You don't need to declare pix_fmts in the format filter.

  • Added a -loop to watermark.png.

7
  • That's working to apply the watermark at the same time as the concatenation but the blend between the videos is gone. It just fades to black between the videos. Also, I have one other video I need to add to the command "vid-4.mp4" so it is 4 videos now. "vid-4.mp4" is the exact same format and length as the others.
    – jrkt
    Commented Oct 5, 2016 at 13:32
  • @JonStevens I didn't get to actually test it before. Try edited command.
    – llogan
    Commented Oct 5, 2016 at 18:53
  • Fantastic. It worked like a charm. You're the man.
    – jrkt
    Commented Oct 5, 2016 at 21:02
  • I've got another complicated ffmpeg question if you could look at it? superuser.com/questions/1132276/…
    – jrkt
    Commented Oct 6, 2016 at 21:52
  • @JonStevens I'm guessing all of these questions are related to your business. Nothing wrong with that, but have you considered hiring a consultant? See FFmpeg: Hire Developers or see my profile. As of now there are too many other questions in my FFmpeg volunteer answer queue.
    – llogan
    Commented Oct 6, 2016 at 22:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .