FFmpeg 向视频中添加文字

news/2024/7/10 19:21:52 标签: ffmpeg, python, java

原文地址:http://www.cnblogs.com/wanggang123/p/6707985.html

  FFmpeg支持添加文字功能,具体如何将文字叠加到视频中的每一张图片,FFmpeg调用了文字库FreeSerif.ttf。当我们

用到ffmpeg 添加文字功能时 我们需要先下载改文字库,下载地址是http://www.fonts2u.com/free-serif.font,这算是

前期准备工作。准备工作完成以后,我来介绍下Ffmpeg实现视频文件添加文字功能的基本流程,流程图如下图所示:

      图1. Ffmpeg 向视频添加文字流程

Ffmpeg 以Filter的方式添加文字,下面给出具体的代码。

1. 打开视频文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int  OpenInput( char  *fileName)
{
     context = avformat_alloc_context();
     context->interrupt_callback.callback = interrupt_cb;
     AVDictionary *format_opts =   nullptr ;
 
     int  ret = avformat_open_input(&context, fileName,  nullptr , &format_opts);
     if (ret < 0)
     {
         return   ret;
     }
     ret = avformat_find_stream_info(context, nullptr );
     av_dump_format(context, 0, fileName, 0);
     if (ret >= 0)
     {
         std::cout << "open input stream successfully"  << endl;
     }
     return  ret;
}

 2. 初始化 初始化包括输出上下文以及Filter,解码器,编码器四个部分。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//初始化输出上下文
int  OpenOutput( char  *fileName)
{
     int  ret = 0;
     ret  = avformat_alloc_output_context2(&outputContext,  nullptr "mpegts" , fileName);
     if (ret < 0)
     {
         goto  Error;
     }
     ret = avio_open2(&outputContext->pb, fileName, AVIO_FLAG_READ_WRITE, nullptr nullptr );  
     if (ret < 0)
     {
         goto  Error;
     }
 
     for ( int  i = 0; i < context->nb_streams; i++)
     {
         AVStream * stream = avformat_new_stream(outputContext, outPutEncContext->codec);
         stream->codec = outPutEncContext;
         if (ret < 0)
         {
             goto  Error;
         }
     }
     av_dump_format(outputContext, 0, fileName, 1);
     ret = avformat_write_header(outputContext,  nullptr );
     if (ret < 0)
     {
         goto  Error;
     }
     if (ret >= 0)
         cout << "open output stream successfully"  << endl;
     return  ret ;
Error:
     if (outputContext)
     {
         avformat_close_input(&outputContext);
     }
     return  ret ;
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  //初始化filter<br>int InitFilter(AVCodecContext * codecContext)
{
     char  args[512];
     int  ret = 0;   
 
     AVFilter *buffersrc  = avfilter_get_by_name( "buffer" );
     AVFilter *buffersink = avfilter_get_by_name( "buffersink" );
     AVFilterInOut *outputs = avfilter_inout_alloc();
     AVFilterInOut *inputs  = avfilter_inout_alloc();
     string  filters_descr = "drawtext=fontfile=D\\\\:FreeSerif.ttf:fontsize=100:text=hello world:x=100:y=100" ;
     enum  AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P};
 
     filter_graph = avfilter_graph_alloc();
     if  (!outputs || !inputs || !filter_graph) {
         ret = AVERROR(ENOMEM);
         goto  end;
     }
 
     /* buffer video source: the decoded frames from the decoder will be inserted here. */
     sprintf_s(args,  sizeof (args),
         "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d" ,
         codecContext->width, codecContext->height, codecContext->pix_fmt,
         codecContext->time_base.num, codecContext->time_base.den,
         codecContext->sample_aspect_ratio.num, codecContext->sample_aspect_ratio.den);
 
     ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc,  "in" ,
         args, NULL, filter_graph);
     if  (ret < 0) {
         av_log(NULL, AV_LOG_ERROR,  "Cannot create buffer source\n" );
         goto  end;
     }
 
     /* buffer video sink: to terminate the filter chain. */
     ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink,  "out" ,
         NULL, NULL, filter_graph);
     if  (ret < 0) {
         av_log(NULL, AV_LOG_ERROR,  "Cannot create buffer sink\n" );
         goto  end;
     }
 
     ret = av_opt_set_int_list(buffersink_ctx,  "pix_fmts" , pix_fmts,
         AV_PIX_FMT_YUV420P, AV_OPT_SEARCH_CHILDREN);
     if  (ret < 0) {
         av_log(NULL, AV_LOG_ERROR,  "Cannot set output pixel format\n" );
         goto  end;
     }
 
     /* Endpoints for the filter graph. */
     outputs->name       = av_strdup( "in" );
     outputs->filter_ctx = buffersrc_ctx;
     outputs->pad_idx    = 0;
     outputs->next       = NULL;
 
     inputs->name       = av_strdup( "out" );
     inputs->filter_ctx = buffersink_ctx;
     inputs->pad_idx    = 0;
     inputs->next       = NULL;   
     if  ((ret = avfilter_graph_parse_ptr(filter_graph, filters_descr.c_str(),
         &inputs, &outputs, NULL)) < 0)
         goto  end;
 
     if  ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
         goto  end;
     return  ret;
end:
     avfilter_inout_free(&inputs);
     avfilter_inout_free(&outputs);
     return  ret;
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//初始化编码器<br>int InitEncoderCodec( int iWidth, int iHeight)
{
     AVCodec *  pH264Codec = avcodec_find_encoder(AV_CODEC_ID_H264);
     if (NULL == pH264Codec)
     {
         printf ( "%s" "avcodec_find_encoder failed" );
         return   -1;
     }
     outPutEncContext = avcodec_alloc_context3(pH264Codec);
     outPutEncContext->gop_size = 30;
     outPutEncContext->has_b_frames = 0;
     outPutEncContext->max_b_frames = 0;
     outPutEncContext->codec_id = pH264Codec->id;
     outPutEncContext->time_base.num =context->streams[0]->codec->time_base.num;
     outPutEncContext->time_base.den = context->streams[0]->codec->time_base.den;
     outPutEncContext->pix_fmt            = *pH264Codec->pix_fmts;
     outPutEncContext->width              =  iWidth;
     outPutEncContext->height             = iHeight;
 
     outPutEncContext->me_subpel_quality = 0;
     outPutEncContext->refs = 1;
     outPutEncContext->scenechange_threshold = 0;
     outPutEncContext->trellis = 0;
     AVDictionary *options =  nullptr ;
     outPutEncContext->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
 
     int  ret = avcodec_open2(outPutEncContext, pH264Codec, &options);
     if  (ret < 0)
     {
         printf ( "%s" "open codec failed" );
         return   ret;
     }
     return  1;
}
<br> //初始化解码器
int  InitDecodeCodec(AVCodecID codecId)
{
     auto  codec = avcodec_find_decoder(codecId);
     if (!codec)
     {
         return  -1;
     }
     decoderContext = context->streams[0]->codec;
     if  (!decoderContext) {
         fprintf (stderr,  "Could not allocate video codec context\n" );
         exit (1);
     }
 
     if  (codec->capabilities & AV_CODEC_CAP_TRUNCATED)
         decoderContext->flags |= AV_CODEC_FLAG_TRUNCATED;
     int  ret = avcodec_open2(decoderContext, codec, NULL);
     return  ret;
 
}

 源码地址:http://pan.baidu.com/s/1o8Lkozw

视频地址:http://pan.baidu.com/s/1jH4dYN8

转载于:https://www.cnblogs.com/boonya/p/8492532.html


http://www.niftyadmin.cn/n/1088430.html

相关文章

【计算机网络综合实验】手把手带你模拟规划与组建一个学校网络实验案例:虚拟局域网VLAN划分

目录 1、设计内容及需求分析 1.1 设计内容 1.2 需求分析 2、基本思路及所涉及的

c语言从键盘输入结构体,C语言结构体问题

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼/*有十个学生&#xff0c;每个学生的数据包括学号、姓名、三门课程的成绩从键盘输入10个学生的数据&#xff0c;要求输出&#xff1a;(1)&#xff1a;每门课的平均成绩(2)&#xff1a;每个学生的总分及平均成绩*/#include#define N…

python tkinter-菜单栏

菜单栏 Menu f tkinter.Menu(root) root[menu]f f.add_command(label菜单)# f.add_command(label关于) import tkinter import tkinter.messagebox def but():tkinter.Toplevel() roottkinter.Tk() #root.title(GUI)#标题 root.geometry(800x600)#窗体大小 root.resizable(Fal…

Pycharm配置镜像源(长期有效)

以前我使用的是这种方案&#xff08;不过每次都要加-i http://pypi.douban.com/simple就很麻烦&#xff09;&#xff1a;pip install第三方包 Could not fetch URL https://pypi.tuna.tsinghua.edu.cn/simple/pipenv/:【解决方案】_蓝多多的小仓库-CSDN博客 今天在这里记录一个…

开学第一测

开学第一测 &#xff08;NOIP模拟题&#xff09; T1 &#xff08;期望得分&#xff1a;100&#xff1b;实际得分&#xff1a;100&#xff09; 循环移动 (cyclic.cpp/c/pas) (1s/256M) 问题描述 给出一个字符串S与N个操作。每个操作用三元组(L, R, K)进行描述&#xff1a…

贝叶斯邮件分类c语言,朴素贝叶斯实例——邮件分类

朴素贝叶斯在文本分类方面使用比较广&#xff0c;即使文本单词之间在现实生活中并不是独立的变量&#xff0c;但是使用朴素贝叶斯进行分类还是能达到很好的效果。朴素贝叶斯的一个实例就是分辨邮件是否是垃圾邮件&#xff0c;其过程如下。1.首先我们是存在一定的训练集供我们来…

Spring中@value以及属性注入的学习

1、简单的Java配置 配置文件&#xff08;jdbc.properties&#xff09;   jdbc.driverClassNamecom.mysql.jdbc.Driver jdbc.urljdbc:mysql://127.0.0.1:3306/test jdbc.usernameroot jdbc.password123 编写一个配置类 1 Configuration2 PropertySource("classpath:jdbc.…

修改数据库名时报错:无法用排他锁锁定该数据库,以执行该操作。

错误&#xff1a; 无法用排他锁锁定该数据库&#xff0c;以执行该操作。 解决方案&#xff1a; 右击该数据库--属性---选项—将限制访问改为单用户模式&#xff08;SINGLE_USER&#xff09;--确定 执行下列语句即可完成数据库改名操作&#xff1a; sp_renamedb 原名,修改后…