Search code examples
androidffmpegandroid-ffmpeg

Text over Image using FFMPEG library on Android


Good day fellows. I am trying to add a text over an image using this ffmpeg command

String exe = " -i /storage/emulated/0/Download/test1.jpg -vf drawtext=text='Test Text':fontcolor=white:fontsize=75:x=1002:y=100: " + file.getAbsolutePath();

But unfortunately I meet this error,

Input #0, image2, from '/storage/emulated/0/Download/test1.jpg':
Duration: 00:00:00.04, start: 0.000000, bitrate: 8264 kb/s
Stream #0:0: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 960x1280, 25 fps, 25 tbr, 25 tbn
Stream mapping:
Stream #0:0 -> #0:0 (mjpeg (native) -> mjpeg (native))
Press [q] to stop, [?] for help
[Parsed_drawtext_0 @ 0xa38921b0] Cannot find a valid font for the family Sans
[AVFilterGraph @ 0xedd90500] Error initializing filter 'drawtext'[AVFilterGraph @ 0xedd90500]  with args 'text=Test Text:fontcolor=white:fontsize=75:x=1002:y=100:'[AVFilterGraph @ 0xedd90500] 
Error reinitializing filters!
Failed to inject frame into filter network: No such file or directory
Error while processing the decoded data for stream #0:0
Conversion failed!

Does someone had the same error as I have? Thank you!


Solution

  • I found a very simple and faster solution to this problem using Canvas on Android instead of using ffmpeg library.

    Here is my code solution:

        ImageView mImageView = view.findViewById(R.id.imgView1);
        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.test);
    
        Bitmap.Config config = bm.getConfig();
        int width = bm.getWidth();
        int height = bm.getHeight();
    
        Bitmap newImage = Bitmap.createBitmap(width, height, config);
    
        Canvas c = new Canvas(newImage);
        c.drawBitmap(bm, 0, 0, null);
    
        Paint paint = new Paint();
        paint.setColor(Color.YELLOW);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextSize(150);
    
        c.drawText("Testare hai noroc",  bm.getWidth()/3, bm.getHeight()/2, paint);
    
        mImageView.setImageBitmap(newImage);
    
        File file = new File("/storage/emulated/0/Download/",System.currentTimeMillis() + ".jpeg");
    
        try {
            newImage.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(file));
        } catch (Exception e) {
            e.printStackTrace();
        } 
    

    I hope this solution will be useful to someone else.