Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to record a video that each frame has watermark. #1064

Open
monxarat opened this issue Sep 27, 2018 · 7 comments
Open

How to record a video that each frame has watermark. #1064

monxarat opened this issue Sep 27, 2018 · 7 comments

Comments

@monxarat
Copy link

monxarat commented Sep 27, 2018

I have using FFmpegFrameRecorder to write video. I want to add watermark to Frame, But load image png(transparent) and using addWeighted to add. But opaque of the image has lost and image after loaded incorrectly.

opencv_core.Mat videoFrame = opencv_imgcodecs.imread(fileName);
opencv_core.Mat borderFrame =  mFrameBorder.getMat();
destFrame = new opencv_core.Mat(videoFrame.size(), videoFrame.type());
opencv_core.addWeighted(videoFrame, 1.0, borderFrame, 1.0, 0, destFrame, -1);

I have using FFmpegFrameFilter. But not effect.

// Filter adds WaterMark
                String watermark = "movie="+mFrameBorder.getFilePath()+" [watermark];[in][watermark]overlay=W-w-15:15:format=rgb[out]";//[logo];[in][logo]overlay=0:0:1:format=rgb [out]
                frameFilter = new FFmpegFrameFilter(watermark, 540, 960);
                frameFilter.setPixelFormat(avutil.AV_PIX_FMT_BGR24);
                frameFilter.start();

            for (int i = 0; i < frameCount; i++) {
                    frameFilter.push(videoFrame);
                    recorder.record(frameFilter.pull());
            }
  • Because In OpenCV has not support load png transparent.
@monxarat monxarat changed the title Add watermark to frame when recorder video. How to record a video that each frame has watermark. Sep 27, 2018
@saudet
Copy link
Member

saudet commented Sep 27, 2018

There is a working example here, we just need to add it to the samples: #974 (comment)

/cc @guikeller

@monxarat
Copy link
Author

@saudet @guikeller
This example I have read and applied to my example, but have not effect. I don't understand.

FFmpegFrameRecorder recorder = null;
        FFmpegFrameFilter frameFilter = null;
        String videoPath = null;
        try {
            String videoFile = mVideoConfig.getImageSource().getParentFile().getName();
            videoPath = mVideoConfig.getImageSource().getAbsoluteFile() + File.separator + Utils.hashKeyForDisk(String.valueOf(videoFile)) + "."+FORMAT;
            if (mVideoConfig.getOrientation() == Configuration.ORIENTATION_LANDSCAPE) {
                recorder = new FFmpegFrameRecorder(videoPath, Config.HEIGHT_SCALE, Config.WIDTH_SCALE, 1);
            } else {
                recorder = new FFmpegFrameRecorder(videoPath, Config.WIDTH_SCALE, Config.HEIGHT_SCALE, 1);
            }
            recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
            recorder.setFrameRate(mVid
```eoConfig.getSpeed().value());
            recorder.setFormat(FORMAT);
            recorder.setVideoOption("crf", "23");
            recorder.setVideoOption("preset", "ultrafast");//ultrafast/superfast
            recorder.setVideoOption("tune", "zerolatency");
            //recorder.setVideoOption("setpts", String.valueOf(recorder.getFrameRate() * 2.0f));
            recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);
            recorder.setVideoQuality(0);
            recorder.setVideoBitrate(400000);
            recorder.setSampleRate(44100);
            recorder.start();

            OpenCVFrameConverter.ToIplImage grabberConverter = new OpenCVFrameConverter.ToIplImage();

            if (mFrameBorder != null) {
                // Filter adds WaterMark
                String watermark = "movie="+mFrameBorder.getFilePath()+" [watermark];[in][watermark]overlay=W-w-0:0:format=rgb[out]";
//[logo];[in][logo]overlay=0:0:1:format=rgb [out]
//[watermark];[in][watermark]overlay=W-w-15:15:format=rgb[out]
//[img];[in][img]overlay=W-w-15:15:format=rgb[out]
                frameFilter = new FFmpegFrameFilter(watermark, 540, 960);
                frameFilter.setPixelFormat(avutil.AV_PIX_FMT_BGR24);
                frameFilter.start();
            }

            for (int i = 0; i < mVideoConfig.getCount(); i++) {
                String fileName = String.format("%s/%s", mVideoConfig.getImageSource().getAbsoluteFile(), getImageName(i));

                if (frameFilter != null) {
                    opencv_core.Mat videoFrame = opencv_imgcodecs.imread(fileName);
                    frameFilter.push(grabberConverter.convert(videoFrame));
                    recorder.record(frameFilter.pull());

                    videoFrame.release();

                } else {
                    recorder.record(grabberConverter.convert(opencv_imgcodecs.imread(fileName)));
                }

                publishProgress((i * 100.0f) / mVideoConfig.getCount(), (float) i);
            }

        } catch (Exception e) {
            e.printStackTrace();
            videoPath = null;
        } finally {
            try {
                recorder.stop();
                recorder.release();

                if (mFrameBorder != null) {
                    // Stops the filter
                    frameFilter.stop();
                    frameFilter.release();
                }

            } catch (Exception e) {
                e.printStackTrace();
                videoPath = null;
            }
        }

        return videoPath;
    }

@saudet
Copy link
Member

saudet commented Sep 27, 2018 via email

@monxarat
Copy link
Author

monxarat commented Sep 28, 2018

My Log.
Above my code when I ran, below logs exported.
But have not had any logs related with Filter and add watermark. And no error.
Video has created, but watermark has not.

09-28 11:22:30.284 9940-10235/com.example I/System.out: [libx264 @ 0x94599400] frame I:1     Avg QP:20.00  size: 37532
    [libx264 @ 0x94599400] frame I:1     Avg QP:20.00  size: 37532
    [libx264 @ 0x94599400] frame P:44    Avg QP:24.50  size: 15012
    [libx264 @ 0x94599400] frame P:44    Avg QP:24.50  size: 15012
    [libx264 @ 0x94599400] mb I  I16..4: 100.0%  0.0%  0.0%
    [libx264 @ 0x94599400] mb I  I16..4: 100.0%  0.0%  0.0%
09-28 11:22:30.285 9940-10235/com.example I/System.out: [libx264 @ 0x94599400] mb P  I16..4: 19.9%  0.0%  0.0%  P16..4: 57.3%  0.0%  0.0%  0.0%  0.0%    skip:22.8%
    [libx264 @ 0x94599400] mb P  I16..4: 19.9%  0.0%  0.0%  P16..4: 57.3%  0.0%  0.0%  0.0%  0.0%    skip:22.8%
    [libx264 @ 0x94599400] coded y,uvDC,uvAC intra: 29.7% 44.9% 7.6% inter: 31.0% 36.0% 1.9%
    [libx264 @ 0x94599400] coded y,uvDC,uvAC intra: 29.7% 44.9% 7.6% inter: 31.0% 36.0% 1.9%
    [libx264 @ 0x94599400] i16 v,h,dc,p: 46% 30% 15%  9%
    [libx264 @ 0x94599400] i16 v,h,dc,p: 46% 30% 15%  9%
09-28 11:22:30.286 9940-10235/com.example I/System.out: [libx264 @ 0x94599400] i8c dc,h,v,p: 34% 26% 31%  8%
    [libx264 @ 0x94599400] i8c dc,h,v,p: 34% 26% 31%  8%
    [libx264 @ 0x94599400] kb/s:3722.94
    [libx264 @ 0x94599400] kb/s:3722.94
09-28 11:22:30.293 9940-10235/com.example I/System.out: [aac @ 0x94599c00] Qavg: nan
    [aac @ 0x94599c00] Qavg: nan
09-28 11:22:30.341 9940-9940/com.example D/>>>>onStart: onStart
09-28 11:22:30.341 9940-9940/com.example E/>>>>Progress: Total: 195.55556 = 97.77778 + 97.77778
09-28 11:22:30.409 9940-10003/com.example D/FFmpeg: Running publishing updates method
09-28 11:22:30.441 9940-9940/com.example D/>>>>onProgress: ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
09-28 11:22:30.442 9940-9940/com.example D/>>>>onProgress:   built with gcc 4.8 (GCC)
      configuration: --target-os=linux --cross-prefix=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/vagrant/SourceCode/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/vagrant/SourceCode/ffmpeg-android/build/armeabi-v7a --extra-cflags='-I/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all' --extra-ldflags='-L/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
09-28 11:22:30.443 9940-9940/com.example D/>>>>onProgress:   
      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
09-28 11:22:30.444 9940-9940/com.example D/>>>>onProgress:  
      libavfilter     6. 31.100 /  6. 31.100
      libswscale      4.  0.100 /  4.  0.100
      libswresample   2.  0.101 /  2.  0.101
09-28 11:22:30.445 9940-9940/com.example D/>>>>onProgress:   
       libpostproc    54.  0.100 / 54.  0.100
09-28 11:22:30.482 9940-9940/com.example D/>>>>onProgress: Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/storage/emulated/0/DCIM/11d026a3714284a7b5ee79576fb9107b7.mp4':
09-28 11:22:30.483 9940-9940/com.example D/>>>>onProgress:   Metadata:
        major_brand     : isom
09-28 11:22:30.484 9940-9940/com.example D/>>>>onProgress:     minor_version   : 512
        compatible_brands: isomiso2avc1mp41
        title           : Video example
09-28 11:22:30.485 9940-9940/com.example D/>>>>onProgress:     encoder         : Lavf57.56.100
        description     : Created by example with version 1.1
09-28 11:22:30.486 9940-9940/com.example D/>>>>onProgress:                     :  https://example.com
      Duration: 00:00:01.50, start: 0.000000, bitrate: 3732 kb/s
09-28 11:22:30.487 9940-9940/com.example D/>>>>onProgress:     Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 540x960, 3726 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
09-28 11:22:30.488 9940-9940/com.example D/>>>>onProgress:     Metadata:
          handler_name    : VideoHandler
09-28 11:22:30.520 9940-9940/com.example D/>>>>onProgress: Input #1, mov,mp4,m4a,3gp,3g2,mj2, from '/storage/emulated/0/DCIM/21d026a3714284a7b5ee79576fb9107b7.mp4':
09-28 11:22:30.521 9940-9940/com.example D/>>>>onProgress:   Metadata:
        major_brand     : isom
09-28 11:22:30.522 9940-9940/com.example D/>>>>onProgress:     minor_version   : 512
        compatible_brands: isomiso2avc1mp41
09-28 11:22:30.524 9940-9940/com.example D/>>>>onProgress:     title           : Video example
09-28 11:22:30.525 9940-9940/com.example D/>>>>onProgress:     encoder         : Lavf57.56.100
        description     : Created by example with version 1.1
                        :  https://example.com
      Duration: 00:00:01.50, start: 0.000000, bitrate: 3732 kb/s
        Stream #1:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 540x960, 3726 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
        Metadata:
          handler_name    : VideoHandler
09-28 11:22:30.551 9940-9940/com.example D/>>>>onProgress: [libx264 @ 0xb5bc4400] using cpu capabilities: none!
09-28 11:22:30.606 9940-9940/com.example D/>>>>onProgress: [libx264 @ 0xb5bc4400] profile Constrained Baseline, level 3.2
    [libx264 @ 0xb5bc4400] 264 - core 148 - H.264/MPEG-4 AVC codec - Copyleft 2003-2015 - 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=6 lookahead_threads=1 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=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
09-28 11:22:30.614 9940-9940/com.example D/>>>>onProgress: Output #0, mp4, to '/storage/emulated/0/DCIM/out_11d026a3714284a7b5ee79576fb9107b7.mp4':
09-28 11:22:30.615 9940-9940/com.example D/>>>>onProgress:   Metadata:
        major_brand     : isom
09-28 11:22:30.616 9940-9940/com.example D/>>>>onProgress:     minor_version   : 512
        compatible_brands: isomiso2avc1mp41
09-28 11:22:30.617 9940-9940/com.example D/>>>>onProgress:     title           : Video example
09-28 11:22:30.618 9940-9940/com.example D/>>>>onProgress:     description     : Created by example with version 1.1
                        :  https://example.com
09-28 11:22:30.619 9940-9940/com.example D/>>>>onProgress:     encoder         : Lavf57.25.100
09-28 11:22:30.620 9940-9940/com.example D/>>>>onProgress:     Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1080x960, 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)
09-28 11:22:30.622 9940-9940/com.example D/>>>>onProgress: Stream mapping:
09-28 11:22:30.623 9940-9940/com.example D/>>>>onProgress:   Stream #0:0 (h264) -> hstack:input0
09-28 11:22:30.624 9940-9940/com.example D/>>>>onProgress:   Stream #1:0 (h264) -> hstack:input1
09-28 11:22:30.625 9940-9940/com.example D/>>>>onProgress:   hstack -> Stream #0:0 (libx264)
09-28 11:22:30.626 9940-9940/com.example D/>>>>onProgress: Press [q] to stop, [?] for help
09-28 11:22:31.138 9940-9940/com.example D/>>>>onProgress: frame=   13 fps=0.0 q=24.0 size=     203kB time=00:00:00.20 bitrate=8313.6kbits/s speed=0.393x    
09-28 11:22:31.665 9940-9940/com.example D/>>>>onProgress: frame=   26 fps= 25 q=24.0 size=     528kB time=00:00:00.63 bitrate=6833.7kbits/s speed=0.606x    
09-28 11:22:32.183 9940-9940/com.example D/>>>>onProgress: frame=   38 fps= 24 q=24.0 size=     899kB time=00:00:01.03 bitrate=7126.7kbits/s speed=0.661x    
09-28 11:22:32.568 9940-9940/com.example D/>>>>onProgress: frame=   45 fps= 23 q=-1.0 Lsize=    1285kB time=00:00:01.50 bitrate=7018.2kbits/s speed=0.768x    
09-28 11:22:32.569 9940-9940/com.example D/>>>>onProgress: video:1284kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.085870%
09-28 11:22:32.585 9940-9940/com.example D/>>>>onProgress: [libx264 @ 0xb5bc4400] frame I:1     Avg QP:20.00  size: 74248
09-28 11:22:32.586 9940-9940/com.example D/>>>>onProgress: [libx264 @ 0xb5bc4400] frame P:44    Avg QP:24.02  size: 28180
    [libx264 @ 0xb5bc4400] mb I  I16..4: 100.0%  0.0%  0.0%
09-28 11:22:32.587 9940-9940/com.example D/>>>>onProgress: [libx264 @ 0xb5bc4400] mb P  I16..4: 17.7%  0.0%  0.0%  P16..4: 53.6%  0.0%  0.0%  0.0%  0.0%    skip:28.7%
    [libx264 @ 0xb5bc4400] coded y,uvDC,uvAC intra: 33.7% 44.0% 12.4% inter: 28.1% 24.6% 1.3%
09-28 11:22:32.588 9940-9940/com.example D/>>>>onProgress: [libx264 @ 0xb5bc4400] i16 v,h,dc,p: 47% 31% 14%  9%
    [libx264 @ 0xb5bc4400] i8c dc,h,v,p: 33% 26% 33%  8%
09-28 11:22:32.589 9940-9940/com.example D/>>>>onProgress: [libx264 @ 0xb5bc4400] kb/s:7009.01
09-28 11:22:32.600 9940-9940/com.example D/>>>>onSuccess: ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
      built with gcc 4.8 (GCC)
      configuration: --target-os=linux --cross-prefix=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/vagrant/SourceCode/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/vagrant/SourceCode/ffmpeg-android/build/armeabi-v7a --extra-cflags='-I/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all' --extra-ldflags='-L/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
      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
      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 '/storage/emulated/0/DCIM/11d026a3714284a7b5ee79576fb9107b7.mp4':
      Metadata:
        major_brand     : isom
        minor_version   : 512
        compatible_brands: isomiso2avc1mp41
        title           : Video example
        encoder         : Lavf57.56.100
        description     : Created by example with version 1.1
                        :  https://example.com
      Duration: 00:00:01.50, start: 0.000000, bitrate: 3732 kb/s
        Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 540x960, 3726 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
        Metadata:
          handler_name    : VideoHandler
    Input #1, mov,mp4,m4a,3gp,3g2,mj2, from '/storage/emulated/0/DCIM/21d026a3714284a7b5ee79576fb9107b7.mp4':
      Metadata:
        major_brand     : isom
        minor_version   : 512
        compatible_brands: isomiso2avc1mp41
        title           : Video example
        encoder         : Lavf57.56.100
        description     : Created by example with version 1.1
                        :  https://example.com
      Duration: 00:00:01.50, start: 0.000000, bitrate: 3732 kb/s
        Stream #1:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 540x960, 3726 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
        Metadata:
          handler_name    : VideoHandler
    [libx264 @ 0xb5bc4400] using cpu capabilities: none!
    [libx264 @ 0xb5bc4400] profile Constrained Baseline, level 3.2
    [libx264 @ 0xb5bc4400] 264 - core 148 - H.264/MPEG-4 AVC codec - Copyleft 2003-2015 - 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=6 lookahead_threads=1 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=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
    Output #0, mp4, to '/storage/emulated/0/DCIM/out_11d026a3714284a7b5ee79576fb9107b7.mp4':
      Metadata:
        major_brand     : isom
        minor_version   : 512
        compatible_brands: isomiso2avc1mp41
        title           : Video example
        description     : Created by example with version 1.1
                        :  https://example.com
        encoder         : Lavf57.25.100
        Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1080x960, q=-1--1, 30 fps, 15360 tbn, 30 tbc (default)
        Metadata:
          encoder         : Lavc57.24.102 libx264
        Side data:
          unkn

@monxarat
Copy link
Author

I have using syntax. Video after the export has been repeated(strange)
"movie= <file_iamge_full_path> [img];[in][img] overlay=0:0:enable='between(t,0,20)'"

Look like this.
dublicated

@guy-keller
Copy link

Hi @monxarat

First and foremost try and make a very basic example that works with the code that I have provided.
Then little by little add your code around it and make sure it all still works to your needs.

I hope this helps, best of luck! 👍

Cheers, Gui

@monxarat
Copy link
Author

@guikeller
the code is the same but has no effect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants