ffmpeg学习十二:滤镜(实现视频缩放,裁剪,水印等)

本文介绍了如何使用ffmpeg的滤镜功能进行视频处理,包括滤镜的基本概念、滤镜图的构成,以及通过示例展示了如何进行视频缩放、裁剪和添加文字水印的操作。
摘要由CSDN通过智能技术生成

这篇文章对使用滤镜进行视频缩放,裁剪水印等做简单介绍。

一.滤镜

滤镜可以实现多路视频的叠加,水印,缩放,裁剪等功能,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。

使用libavfilter为视频添加滤镜

ffmpeg官网给出的filtering_video.c介绍了滤镜的用法,这个例子将一个视频文件解码成原始的一帧数据,然后再将这一帧数据使用滤镜惊醒缩放,缩小到78x24大小后,将像素中的点转换成字符,然后显示在终端中,效果如下:
这里写图片描述
这个程序结构非常清晰的介绍了滤镜的用法,但这种将视频中的像素转换为字符,然后显示在终端的做法并不能很直观的感受滤镜的作用,因此,我对这个程序做了简单的修改,将滤镜处理后的视频保存在文件中而不是显示在终端。下面为我修改过后,完整的程序,只有一个.c文件:


#define _XOPEN_SOURCE 600 /* for usleep */
#include <unistd.h>

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavfilter/avfiltergraph.h>
#include <libavfilter/avcodec.h>
#include <libavfilter/buffersink.h>
#include <libavfilter/buffersrc.h>
#include <libavutil/opt.h>

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值