在 Android 上使用 FFMPEG 库在图像上显示文本

发布于 2025-01-11 08:20:43 字数 1075 浏览 0 评论 0原文

朋友们,美好的一天。 我正在尝试使用此 ffmpeg 命令在图像上添加文本,

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

但不幸的是我遇到了此错误,

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!

有人有与我相同的错误吗?谢谢你!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

同尘 2025-01-18 08:20:43

这是您需要关注的错误消息(FFmpeg 的错误日志有时会产生误导):

[Parsed_drawtext_0 @ 0xa38921b0] Cannot find a valid font for the family Sans

发生这种情况是因为您没有指定字体,并且在您的系统上找不到默认字体“Sans”。因此,您需要明确指定一个。

这里是Android 字体资源参考的链接
FFmpeg drawtext 文档中的第一个示例说明如何指定字体文件。

(我不是 Android 开发人员,所以希望您可以从这些链接中找到答案。)

This is the error message that you need to focus on (FFmpeg's error logs are sometimes misleading):

[Parsed_drawtext_0 @ 0xa38921b0] Cannot find a valid font for the family Sans

And this happened because you did not specify the font, and the default font "Sans" couldn't be found on your system. So, you need to specify one explicitly.

Here is a link to the Android font resources reference,
and the first example in FFmpeg drawtext documentation illustrates how to specify a font file.

(I'm not an Android dev, so hopefully you can figure it out from these links.)

那片花海 2025-01-18 08:20:43

我在 Android 上使用 Canvas 而不是使用 ffmpeg 库找到了一个非常简单且更快的解决方案。

这是我的代码解决方案:

    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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文