这篇文章对使用滤镜进行视频缩放,裁剪水印等做简单介绍。
滤镜可以实现多路视频的叠加,水印,缩放,裁剪等功能,ffmpeg提供了丰富的滤镜,可以使用ffmpeg -filters来查看:
Filters:
T.. = Timeline support
.S. = Slice threading
..C = Command support
A = Audio input/output
V = Video input/output
N = Dynamic number and/or type of input/output
| = Source or sink filter
T.. adelay A->A Delay one or more audio channels.
… aecho A->A Add echoing to the audio.
… aeval A->A Filter audio signal according to a specified expression.
T.. afade A->A Fade in/out input audio.
… aformat A->A Convert the input audio to one of the specified formats.
… ainterleave N->A Temporally interleave audio inputs.
… allpass A->A Apply a two-pole all-pass filter.
… amerge N->A Merge two or more audio streams into a single multi-channel stream.
… amix N->A Audio mixing.
… anull A->A Pass the source unchanged to the output.
T.. apad A->A Pad audio with silence.
… aperms A->A Set permissions for the output audio frame.
… aphaser A->A Add a phasing effect to the audio.
… aresample A->A Resample audio data.
… aselect A->N Select audio frames to pass in output.
… asendcmd A->A Send commands to filters.
… asetnsamples A->A Set the number of samples for each output audio frames.
… asetpts A->A Set PTS for the output audio frame.
… asetrate A->A Change the sample rate without altering the data.
… asettb A->A Set timebase for the audio output link.
… ashowinfo A->A Show textual information for each audio frame.
… asplit A->N Pass on the audio input to N audio outputs.
…..
这里只是列出其中一小部分,可见ffmpeg提供了非常丰富的滤镜。
Filter:代表单个filter
FilterPad:代表一个filter的输入或输出端口,每个filter都可以有多个输入和多个输出,只有输出pad的filter称为source,只有输入pad的filter称为sink
FilterLink:若一个filter的输出pad和另一个filter的输入pad名字相同,即认为两个filter之间建立了link
FilterChain:代表一串相互连接的filters,除了source和sink外,要求每个filter的输入输出pad都有对应的输出和输入pad
**FilterGraph:**FilterChain的集合
经典示例:
图中每一个节点就是一个Filter,每一个方括号所代表的就是FilterPad,可以看到split的输出pad中有一个叫tmp的,而crop的输入pad中也有一个tmp,由此在二者之间建立了link,当然input和output代表的就是source和sink,此外,图中有三条FilterChain,第一条由input和split组成,第二条由crop和vflip组成,第三条由overlay和output组成,整张图即是一个拥有三个FilterChain的FilterGraph。
ffmpeg官网给出的filtering_video.c介绍了滤镜的用法,这个例子将一个视频文件解码成原始的一帧数据,然后再将这一帧数据使用滤镜惊醒缩放,缩小到78x24大小后,将像素中的点转换成字符,然后显示在终端中,效果如下:
这个程序结构非常清晰的介绍了滤镜的用法,但这种将视频中的像素转换为字符,然后显示在终端的做法并不能很直观的感受滤镜的作用,因此,我对这个程序做了简单的修改,将滤镜处理后的视频保存在文件中而不是显示在终端。下面为我修改过后,完整的程序,只有一个.c文件:
#define _XOPEN_SOURCE 600 /* for usleep */
#include
#include
#include
#include
#include
#include
#include
#include
/*缩放滤镜,以下命令将视频缩小一般,iw,ih指的是输入的视频宽和高,
*此外也可以直接使用数字知名缩放的大小,比如:scale=200:100
*/
//const char *filter_descr = "scale=iw/2:ih/2";
/*裁剪滤镜,一下命令将视频的左上角的四分之一裁剪下来*/
//const char *filter_descr = "crop=iw/2:ih/2:0:0";
/*添加字符串水印*/
const char *filter_descr = "drawtext=fontfile=FreeSans.ttf:fontcolor=green:fontsize=30:text='Hello'";
static AVFormatContext *fmt_ctx;
static AVCodecContext *dec_ctx;
AVFilterContext *buffersink_ctx;
AVFilterContext *buffersrc_ctx;
AVFilterGraph *filter_graph;
static int video_stream_index = -1;
static int64_t last_pts = AV_NOPTS_VALUE;
static int open_input_file(const char *filename)
{
int ret;
AVCodec *dec;
if ((ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
return ret;
}
if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
return ret;
}
/* select the video stream */
ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot find a video stream in the input file\n");
return ret;
}
video_stream_index = ret;
dec_ctx = fmt_ctx->streams[video_stream_index]->codec;
av_opt_set_int(dec_ctx, "refcounted_frames", 1, 0);
/* init the video decoder */
if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open video decoder\n");
return ret;
}
return 0;
}
static int init_filters(const char *filters_descr)
{
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();
AVRational time_base = fmt_ctx->streams[video_stream_index]->time_base;
enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
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. */
snprintf(args, sizeof(args),
"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
time_base.num, time_base.den,
dec_ctx->sample_aspect_ratio.num, dec_ctx->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_NONE, AV_OPT_SEARCH_CHILDREN);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");
goto end;
}
/*
* Set the endpoints for the filter graph. The filter_graph will
* be linked to the graph described by filters_descr.
*/
/*
* The buffer source output must be connected to the input pad of
* the first filter described by filters_descr; since the first
* filter input label is not specified, it is set to "in" by
* default.
*/
outputs->name = av_strdup("in");
outputs->filter_ctx = buffersrc_ctx;
outputs->pad_idx = 0;
outputs->next = NULL;
/*
* The buffer sink input must be connected to the output pad of
* the last filter described by filters_descr; since the last
* filter output label is not specified, it is set to "out" by
* default.
*/
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,
&inputs, &outputs, NULL)) < 0)
goto end;
if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
goto end;
end:
avfilter_inout_free(&inputs);
avfilter_inout_free(&outputs);
return ret;
}
FILE * file_fd;
static void write_frame(const AVFrame *frame)
{
if(frame->format==AV_PIX_FMT_YUV420P){
printf("format is yuv420p\n");
}else{
printf("format is not yuv420p\n");
}
printf("frame widht=%d,frame height=%d\n",\
frame->width,frame->height);
fwrite(frame->data[0],1,frame->width*frame->height,file_fd);
fwrite(frame->data[1],1,frame->width/2*frame->height/2,file_fd);
fwrite(frame->data[2],1,frame->width/2*frame->height/2,file_fd);
}
int main(int argc, char **argv)
{
int ret;
AVPacket packet;
AVFrame *frame = av_frame_alloc();
AVFrame *filt_frame = av_frame_alloc();
int got_frame;
file_fd = fopen("hello.yuv","wb+");
if (!frame || !filt_frame) {
perror("Could not allocate frame");
exit(1);
}
if (argc != 2) {
fprintf(stderr, "Usage: %s file\n", argv[0]);
exit(1);
}
av_register_all();
avfilter_register_all();
if ((ret = open_input_file(argv[1])) < 0)
goto end;
if ((ret = init_filters(filter_descr)) < 0)
goto end;
/* read all packets */
while (1) {
if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
break;
if (packet.stream_index == video_stream_index) {
got_frame = 0;
ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, &packet);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error decoding video\n");
break;
}
if (got_frame) {
frame->pts = av_frame_get_best_effort_timestamp(frame);
/* push the decoded frame into the filtergraph */
if (av_buffersrc_add_frame_flags(buffersrc_ctx, frame, AV_BUFFERSRC_FLAG_KEEP_REF) < 0) {
av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
break;
}
/* pull filtered frames from the filtergraph */
while (1) {
ret = av_buffersink_get_frame(buffersink_ctx, filt_frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
break;
if (ret < 0)
goto end;
write_frame(filt_frame);
av_frame_unref(filt_frame);
}
av_frame_unref(frame);
}
}
av_free_packet(&packet);
}
end:
avfilter_graph_free(&filter_graph);
avcodec_close(dec_ctx);
avformat_close_input(&fmt_ctx);
av_frame_free(&frame);
av_frame_free(&filt_frame);
if (ret < 0 && ret != AVERROR_EOF) {
fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
exit(1);
}
fclose(file_fd);
exit(0);
}
文件编译参考前面的文章,便宜时会报错:No such filter: ‘drawtext’。这是因为我们没有是能这个滤镜。
解决办法:程序找不到 drawtext这个filter,一般是因为使用默认编译选项是该filter并未被编译进库里面,所以重新再编译ffmpeg,并且在执行”./configure ……”时加上“–enable-libfreetype”。这样就ok了。
完整的配置如下,我们之前已经使能了aac,h.264编解码器。
./configure --enable-libx264 --enable-gpl --enable-decoder=h264 --enable-encoder=libx264 --enable-shared --enable-static --disable-yasm -enable-nonfree --enable-libfdk-aac --enable-shared --enable-libfreetype --prefix=tmp
配置结束后执行make && make install安装即可。
编译后,执行实例如下:
./out.bin rain.mp4
然后再当前文件下会生成hello.yuv的视频文件,这是原始数据格式的视频,可以使用ffplay来播放:
ffplay -s 1280x640 hello.yuv
改程序会打印出视频的长和宽,这里的1280x640请使用打印出来的长和宽来代替。
三.缩放,裁剪,添加字符串水印
滤镜的用法基本就是如上程序给出的步骤,我们可以通过指定不同的描述字符串来实现不同的滤镜功能。下面的字符串将视频缩放为200x100大小,着我在程序里的注释中已经给出了。
原视频:
缩小为一半:
const char *filter_descr = “scale=iw/2:ih/2”;
缩小后现象不明显,就不贴图了。
如下命令将视频裁剪出中间的1/4。
const char *filter_descr = “crop=iw/2:ih/2:iw/4:ih/4”;
效果如下:
下面字符串给视频添加hello的字符串水印:
const char *filter_descr = “drawtext=fontfile=FreeSans.ttf:fontcolor=green:fontsize=30:text=’Hello’”;
效果如下:
更复杂的滤镜的使用,期待和大家一起学习。