How to add watermark on video using mp4parser in android?

2.8k Views Asked by At

I had successfully convert recorded video into "out.h264" format and also audio into ".AAC" format using mp4parser. Now I want to implement "watermark image" on my recorded video. Is this possible with mp4parser to add watermark on video? I have checked GPUimages too. But there is no way to add effect on video, Its' example shows effect for only Images. So my question is How can I add watermark on video?

Below is my code for audio video :

    File sdCard = Environment.getExternalStorageDirectory();
    IsoFile isoFile = new IsoFile(videosPath);
        TrackBox trackBox = (TrackBox) Path.getPath(isoFile, "/moov/trak/mdia/minf/stbl/stsd/avc1/../../../../../");
        SampleList sl = new SampleList(trackBox);
        File out = new File(sdCard + "/out.h264");
        if (out.exists()) {
            out.delete();
        }
        FileChannel fc = new RandomAccessFile(out, "rw").getChannel();
        ByteBuffer separator = ByteBuffer.wrap(new byte[] { 0, 0, 0, 1 });

        fc.write((ByteBuffer) separator.rewind());
        // Write SPS
        fc.write(ByteBuffer.wrap(((AvcConfigurationBox) Path.getPath(trackBox, "mdia/minf/stbl/stsd/avc1/avcC")).getSequenceParameterSets().get(0)));
        // Warning:
        // There might be more than one SPS (I've never seen that but it is possible)

        fc.write((ByteBuffer) separator.rewind());
        // Write PPS
        fc.write(ByteBuffer.wrap(((AvcConfigurationBox) Path.getPath(trackBox, "mdia/minf/stbl/stsd/avc1/avcC")).getPictureParameterSets().get(0)));
        // Warning:
        // There might be more than one PPS (I've never seen that but it is possible)

        int lengthSize = ((AvcConfigurationBox) Path.getPath(trackBox, "mdia/minf/stbl/stsd/avc1/avcC")).getLengthSizeMinusOne() + 1;
        for (Sample sample : sl) {
            ByteBuffer bb = sample.asByteBuffer();
            while (bb.remaining() > 0) {
                int length = (int) IsoTypeReaderVariable.read(bb, lengthSize);
                fc.write((ByteBuffer) separator.rewind());
                fc.write((ByteBuffer) bb.slice().limit(length));
                bb.position(bb.position() + length);
            }

        }
        fc.close();

        Log.e(TAG, "Converted Path: " + out.getAbsolutePath() + " Start Time Convert: " + new Date());

        H264TrackImpl h264Track = new H264TrackImpl(new FileDataSourceImpl(out.getAbsoluteFile()));

        AACTrackImpl aacTrack = new AACTrackImpl(new FileDataSourceImpl(audioPath));
        CroppedTrack aacTrackShort = new CroppedTrack(aacTrack, 1, aacTrack.getSamples().size());
        // MP3TrackImpl accTrackImpl = new MP3TrackImpl(new FileDataSourceImpl(audioPath));

        Movie movie = new Movie();
        movie.addTrack(h264Track);
        movie.addTrack(aacTrackShort);

        Container mp4file = new DefaultMp4Builder().build(movie);
        File output = new File(sdCard + "/output_KanAK.mp4");

        if (output.exists()) {
            output.delete();
        }
        @SuppressWarnings("resource")
        FileChannel fc1 = new RandomAccessFile(output, "rw").getChannel();
        mp4file.writeContainer(fc1);
        fc1.close();

        Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.velfee);
        gpuImage.saveToPictures(largeIcon, output, 100, new OnPictureSavedListener() {

            @Override
            public void onPictureSaved(Uri uri) {
                // TODO Auto-generated method stub
                Log.e(TAG, "Picture save Uri");
                GPUImageDifferenceBlendFilter filter;
                filter.setBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
            }
        });

Any help would be appreciated!! Thanks in advance.

1

There are 1 best solutions below

3
On

You can use a String as a watermark using the subtitle example in the github page. Set the limits to your own but ofcourse you wont be able to use image.