0

I am adding image watermark to video with help of FFmpeg but FFmpeg takes an inordinate amount of time with the below command-

String[] cmd = {"-i",videoPath, "-i", waterMark.toString(),"-filter_complex","overlay=5:5","-codec:a", "copy", outputPath};

so i tried another command which was little bit faster but increase output file size(which i do not want)

String[] cmd = {"-y","-i", videoPath, "-i", waterMark.toString(), "-filter_complex", "overlay=5:5", "-c:v","libx264","-preset", "ultrafast", outputPath};

Some one please explain to me how to increase the speed of FFmpeg watermarking speed without increasing the size of output. Thanks.

Karan Harsh Wardhan
  • 1,096
  • 9
  • 22
Tushar Lathiya
  • 940
  • 9
  • 26
  • What's input video size (resolution / bytes) and what device are You running on? What is "very slow" in your opinion? I don't know if You realize it but adding a watermark requires reencoding of entire video stream (frame by frame) so it's not feasible to be very fast. – Pawel Apr 09 '19 at 09:55
  • @Pawel video size is < 7mb. Running on Moto G3. adding watermark to video is slow. – Tushar Lathiya Apr 09 '19 at 09:58
  • @Pawel see here [https://stackoverflow.com/questions/16907050/ffmpeg-watermarking-processing-is-very-slow]. If you have any idea please let me know. tnx. – Tushar Lathiya Apr 09 '19 at 10:02
  • You say slow, how long does it take when using a 7MB video? – HB. Apr 09 '19 at 11:11
  • @HB. it is taking 30 sec to 60 sec. – Tushar Lathiya Apr 09 '19 at 11:27

2 Answers2

5

You mentioned that a 7MB video takes between 30-60 seconds.

There is always a trade off when choosing between speed and quality.

I tested on my phone using a 7MB file and it took 13 seconds, still slow, but we can't expect much better then that.

Ways to increase speed:

  • Lowering the frame rate, using the -r command
  • Changing the bitrate, using the -b:v and -b:a commands
  • Change the Constant Rate Factor, using -crf. The default value is 21

The range of the quantizer scale is 0-51: where 0 is lossless, 23 is default, and 51 is worst possible. A lower value is a higher quality and a subjectively sane range is 18-28. Consider 18 to be visually lossless or nearly so: it should look the same or nearly the same as the input but it isn't technically lossless.

This is what I have found works the best on most android devices:

String[] s = {"-i", VideoPath, "-i", ImagePath, "-filter_complex", "[0:v]pad=iw:if(lte(ih\\,iw)\\,ih\\,2*trunc(iw*16/9/2)):(ow-iw)/2:(oh-ih)/2[v0];[1:v][v0]scale2ref[v1][v0];[v0][v1]overlay=x=(W-w)/2:y=(H-h)/2[v]", "-map", "[v]", "-map", "0:a", "-c:v", "libx264", "-preset", "ultrafast", "-r", myFrameRate, directoryToStore[0] + "/" + SavedVideoName};

I reduced my framerate slightly, you can experiment what works best for you. I'm using mp4parser to retrieve the frame rate.

I have to give credit to @Gyan that provided me with a way to perfectly scale images that is being placed on top of a video, you can look at the question I asked here.

If you are unsure about the frame rate, you can remove it from the command and first test if your speed is reduced.

Try it, if you have any questions, please ask.


OP opted to go with the following command:

String[] cmd = {"-y","-i", videoPath, "-i", waterMark.toString(), "-filter_complex", "overlay=(main_w-overlay_w-10):5", "-map", "0:a","-c:v", "libx264", "-crf", "28","-preset", "ultrafast" ,outputPath};

Edit

Just to add on the command I mentioned and provide a detailed explanation of how to use it etc:

String[] cmd = {"-i", videoPath, "-i", waterMark.toString(), "-filter_complex", "[0:v]pad=iw:if(lte(ih\\,iw)\\,ih\\,2*trunc(iw*16/9/2)):(ow-iw)/2:(oh-ih)/2[v0];[1:v][v0]scale2ref[v1][v0];[v0][v1]overlay=x=(W-w)/2:y=(H-h)/2[v]", "-map", "[v]", "-map", "0:a", "-c:v", "libx264", "-preset", "ultrafast", "-r", myFrameRate, outputPath};

This is for device's that has a display aspect ratio of 16:9. If you want this filter to work on all device's you will have to get the aspect ratio of the device and change the filter 16/9/2 respectively.

You can get the device aspect ratio by creating this methods:

int gcd(int p, int q) { 
    if (q == 0) return p; 
    else return gcd(q, p % q); 
} 
void ratio(int a, int b) { 
    final int gcd = gcd(a,b); 
    if(a > b) { 
        setAspectRatio(a/gcd, b/gcd); 
    } else { 
        setAspectRatio(b/gcd, a/gcd); 
    } 
} 
void setAspectRatio(int a, int b) { 
    System.out.println("aspect ratio = "+a + " " + b); 
    //This is the string that will be used in the filter (instead of hardcoding 16/9/2
    filterAspectRatio = a + "/" + b + "/" + "2"; 
}

Now you have the correct aspect ratio and you can change the filter accordingly.

Next, create a watermark and add it to a view, make that view the size of the device (match_parent) and scale/place the watermark where you would like it to be. You can then get the bitmap by calling:

Bitmap waterMarkBitmap = watermarkView.getDrawingCache();

and create a file from the Bitmap, like this:

String outputFilename = "myCoolWatermark.png"; //provide a name for you saved watermark
File path = Environment.getExternalStorageDirectory(); //this can be changed to where you want to store the bitmap
File waterMark = new File(path, outputFilename); 

try (FileOutputStream out = new FileOutputStream(waterMark)) { 
    waterMarkBitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // PNG is a lossless format, the compression factor (100) is ignored 
} catch (IOException e) { 
    e.printStackTrace(); 
}

The watermark is created and can be reused, or you can delete it when you are done with it.

Now you can call the command mentioned above.

HB.
  • 4,116
  • 4
  • 29
  • 53
2

This is a very common question here. The simple answer is that you can't increase the encoding speed of ffmpeg on Android. You're encoding on a phone, so you don't expect desktop/server performance using an encoder and no hardware acceleration support.

There are a few things users can do:

  • Stream copy the audio with -c:a copy (you're already doing that).

  • Use -preset ultrafast to give up encoding efficiency for encoding speed (you're also already doing that).

  • Make the output width x height smaller with the scale filter (probably not an acceptable option for you).

  • Make sure your x264 was not compiled with --disable-asm so you can take advantage of the various ARM and NEON optimizations in x264 for a significant increase in encoding speed. However, I don't know which Android devices support that, but it's something to look into. For a quick check to see if you are using any optimizations refer to the console output from ffmpeg and search for using cpu capabilities. If none! then it is not using any optimizations, otherwise it may say ARMv7 NEON or something like that.

  • Offload the encoding to a server. Saves your users' battery life too.

  • All this for an annoying watermark? Avoid re-encoding and use a player to overlay the watermark.

  • Apparently FFmpeg has MediaCodec decoding support on Android, but encoding is the bottleneck here. However maybe it will save a few fps.

  • Send a patch to FFmpeg that enables MediaCodec encoding support or wait a few years for someone else to do so.

  • Forget ffmpeg and use MediaCodec directly. I am clueless about this and too lazy to look it up, but I assume it uses hardware to encode and I'll guess you can use it to make an overlay. Someone correct me if I am wrong.

llogan
  • 121,796
  • 28
  • 232
  • 243
  • Great answer. I also wanted to mention mediacodec, which is a lot faster, but a lot more complex. I tested with the same video (7MB) and the time was reduced from 13seconds to 8seconds using mediacodec. You are correct with your last point. – HB. Apr 10 '19 at 03:53
  • @HB. please explain more about your solution with mediacodec. if possible share your working piece of code. – Tushar Lathiya Apr 10 '19 at 06:23
  • I have tried [https://stackoverflow.com/questions/25358489/adding-watermark-bitmap-over-video-in-android-4-3s-mediamuxer-or-ffmpeg] but this solution also increase output size as like ffmpeg. – Tushar Lathiya Apr 10 '19 at 06:28
  • @HB. i want answer provide by you – Tushar Lathiya Apr 10 '19 at 06:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191576/discussion-between-tushar-lathiya-and-hb). – Tushar Lathiya Apr 10 '19 at 06:41