1

I want to put watermarks in my video (in Specific time). I researched some sources, and it said I need to (video filter) reencode the whole video to put a watermark, but I don't have a high-end PC to do this. If I reencode the whole video, it takes about 6 to 7 hours.

However, I have come up with some good ideas to do this. I created three segments, the first segment (19 mins) is the original video, the second segment (21 seconds) is where the watermark needs to be, and the third segment (15 mins) is also the original video. So, I put the watermark in the second segment and then concat them with ffmpeg (Note: I didn't re-encode all segments. I re-encoded only the second segment and merged them wit without re-encoded videos).

However,when going through segment 2, it encounters playback issues such as being stuck. However, I played the segments individually before concatting them, and they played smoothly. When concatenated, the playback issues occurred. FFmpeg details for the three segments are provided below

Segment 1 (Original video)


  libavutil      56. 70.100 / 56. 70.100
  libavcodec     58.134.100 / 58.134.100
  libavformat    58. 76.100 / 58. 76.100
  libavdevice    58. 13.100 / 58. 13.100
  libavfilter     7.110.100 /  7.110.100
  libswscale      5.  9.100 /  5.  9.100
  libswresample   3.  9.100 /  3.  9.100
  libpostproc    55.  9.100 / 55.  9.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'output_1.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.76.100
  Duration: 00:01:30.09, start: 0.000000, bitrate: 2258 kb/s
  Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], 2056 kb/s, 30 fps, 30 tbr, 90k tbn, 60 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
  Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 195 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]

Segment 2 (Watermark video)

  libavutil      56. 70.100 / 56. 70.100
  libavcodec     58.134.100 / 58.134.100
  libavformat    58. 76.100 / 58. 76.100
  libavdevice    58. 13.100 / 58. 13.100
  libavfilter     7.110.100 /  7.110.100
  libswscale      5.  9.100 /  5.  9.100
  libswresample   3.  9.100 /  3.  9.100
  libpostproc    55.  9.100 / 55.  9.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'output_2.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.76.100
  Duration: 00:00:22.10, start: 0.000000, bitrate: 5648 kb/s
  Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], 5443 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
  Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 196 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]


Segment 3 (Original video)

  libavutil      56. 70.100 / 56. 70.100
  libavcodec     58.134.100 / 58.134.100
  libavformat    58. 76.100 / 58. 76.100
  libavdevice    58. 13.100 / 58. 13.100
  libavfilter     7.110.100 /  7.110.100
  libswscale      5.  9.100 /  5.  9.100
  libswresample   3.  9.100 /  3.  9.100
  libpostproc    55.  9.100 / 55.  9.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'output_3.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.76.100
  Duration: 00:01:53.11, start: 0.000000, bitrate: 2269 kb/s
  Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], 2067 kb/s, 30 fps, 30 tbr, 90k tbn, 60 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
  Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 195 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]


Concated full video


  libavutil      56. 70.100 / 56. 70.100
  libavcodec     58.134.100 / 58.134.100
  libavformat    58. 76.100 / 58. 76.100
  libavdevice    58. 13.100 / 58. 13.100
  libavfilter     7.110.100 /  7.110.100
  libswscale      5.  9.100 /  5.  9.100
  libswresample   3.  9.100 /  3.  9.100
  libpostproc    55.  9.100 / 55.  9.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'output_merged.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.76.100
  Duration: 00:03:45.29, start: 0.000000, bitrate: 2596 kb/s
  Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], 2393 kb/s, 29.99 fps, 30 tbr, 90k tbn, 60 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
  Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 195 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]
      

UPDATE 1 - Used FFMPEG Commands

Split command

ffmpeg -hide_banner -ss "0" -i "output.mp4" -t "90" -avoid_negative_ts make_zero -map "0:0" "-c:0" copy -map "0:1" "-c:1" copy -map_metadata 0 -movflags "+faststart" -ignore_unknown -f mp4 -y "tmp/output_1.mp4"

ffmpeg -hide_banner -ss "90" -i "output.mp4" -t "20" -avoid_negative_ts make_zero -map "0:0" "-c:0" copy -map "0:1" "-c:1" copy -map_metadata 0 -movflags "+faststart" -ignore_unknown -f mp4 -y "tmp/output_2.mp4"

ffmpeg -hide_banner -ss "110" -i "output.mp4" -t "111" -avoid_negative_ts make_zero -map "0:0" "-c:0" copy -map "0:1" "-c:1" copy -map_metadata 0 -movflags "+faststart" -ignore_unknown -f mp4 -y "tmp/output_3.mp4"


Applying watermark command

ffmpeg -hide_banner -i "tmp/output_2.mp4" -vf "drawtext='fontfile=DejaVu Sans\:style=Semibold:text=HelloWorld:fontcolor=white:fontsize=30:y=h-50*t:x=w-(t-4.5)*(w+tw)/15'" -vcodec libx264 -crf 15 -c:a copy "tmp/watermark_output_2.mp4"

Concat files command

ffmpeg -f concat -safe 0 -i "../filelist.txt" -c copy "output_merged.mp4"

UPDATE - 2 Uploaded output videos.

Sgement 1 original video (Without re-encode) https://drive.google.com/file/d/1Mg7rJxXGcxg25328flJ_CD9NJhfpFj37/view?usp=sharing

Sgement 2 watermark video (With re-encode) - watermark starts at 4 seconds and plays without any issue.

https://drive.google.com/file/d/141KYQ1m94bIpxKj5n7_wCcbTOWQ9uO17/view?usp=sharing

Sgement 3 original video (Without re-encode)

https://drive.google.com/file/d/1fYkFvD6Yg81Un7YhfUJbSJ-VBk2qlFvJ/view?usp=sharing

Concated full video (The issue starts when the watermark appears. (time 00:01:28 Minutes)

https://drive.google.com/file/d/1Tq6wXSWGSeAHBwfsV31-Qpt5BhJeWwSJ/view?usp=sharing

repeated error appearing during the concat process

[mp4 @ 0x5594ce3c8280] Non-monotonous DTS in output stream 0:0; previous: 8099300, current: 1715482; changing to 8099301. This may result in incorrect timestamps in the output file.

6
  • 1
    Please edit your post, and add all the FFmpeg commands you have used.
    – Rotem
    Commented Feb 17 at 9:31
  • Yes please, do edit your post, I do not see your FFMPEG command(s) at all.
    – vssher
    Commented Feb 17 at 10:09
  • different tbn, add -video_track_timescale 90000 Commented Feb 17 at 13:23
  • Can you explain 'tbn' and -video_track_timescale 90000, and where they need to be used? Is it in the split command, applying watermark, or concatenating files command?
    – Raymond
    Commented Feb 17 at 13:40
  • to Applying watermark command before tmp/watermark_output_2.mp4 Commented Feb 18 at 3:20

1 Answer 1

1

As commented and documented, for using Concat Demuxer, all the files must have the same streams (same codecs, same time base, etc.), and in your case the time base of the re-encoded part is different.

There are other issues:

  • For concatenating without re-encoding, we usually like all the parts starring with a key-frame.
  • MP4 container is not designed to be "segmented", and we better use other container.

The following solution may not work in all cases, but I think it is going to work in your case (H.264 video codec, and AAC audio codec):

Instead of using -ss and -t, we better use Segment Muxer.
We may also use FLV container format (it is better designed for concatenation, and solves the time base issue):

ffmpeg -i input.mp4 -c copy -f segment -segment_time 20 -segment_list list.txt -segment_list_type ffconcat %04d.flv

By default, Segment Muxer breaks the parts only at key-frames (-break_non_keyframes is false by default)

Find the relevant segment, and create a copy: copy 0001.flv original_0001.flv.


Add the watermark to the relevant segment (probably have to manually adjust the timing):

ffmpeg -y -hide_banner -i "original_0001.flv" -vf "drawtext='fontfile=DejaVuSans.ttf:text=HelloWorld:fontcolor=white:fontsize=30:y=h-50*t:x=w-(t-4.5)*(w+tw)/15'" -vcodec libx264 -crf 15 -c:a copy "0001.flv"

(Note that the style=Semibold argument gave me an error).


Concatenate the segments back, without re-encoding (may use MP4 output file format):

ffmpeg -y -f concat -safe 0 -i list.txt -c copy output.mp4

You must log in to answer this question.

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