ffmpeg用filter实现视频scale

1、概述

此例子用ffmpeg的filter实现视频scale。

2、代码

/** 
 * 最简单的基于FFmpeg的AVFilter例子(scale) 
 * 
 * 缪国凯(MK)
 * 821486004@qq.com
 *
 * http://blog.csdn.net/dancing_night
 * 
 * 本程序使用FFmpeg的AVfilter实现了视频的缩放功能。
 * 
 *
 */

#include "stdafx.h"

#ifdef __cplusplus
extern "C"
{
#endif
#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/avutil.h>
#include <libswscale/swscale.h>
#ifdef __cplusplus
};
#endif

#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "avdevice.lib")
#pragma comment(lib, "avfilter.lib")

//#pragma comment(lib, "avfilter.lib")
//#pragma comment(lib, "postproc.lib")
//#pragma comment(lib, "swresample.lib")
#pragma comment(lib, "swscale.lib")


static AVFormatContext *ifmt_ctx, *ofmt_ctx;
static AVCodecContext *pCodecCtx;
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(&ifmt_ctx, filename, NULL, NULL)) < 0) {
		printf( "Cannot open input file\n");
		return ret;
	}

	if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0) {
		printf( "Cannot find stream information\n");
		return ret;
	}

	/* select the video stream */
	ret = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);
	if (ret < 0) {
		printf( "Cannot find a video stream in the input file\n");
		return ret;
	}
	video_stream_index = ret;
	pCodecCtx = ifmt_ctx->streams[video_stream_index]->codec;

	/* init the video decoder */
	if ((ret = avcodec_open2(pCodecCtx, dec, NULL)) < 0) 
	{
		printf( "Cannot open video decoder\n");
		return ret;
	}

	return 0;
}

int openoutputfile(const char* filename, int width, int height)
{
	AVStream *out_stream;
	int ret = 0;
	if ((ret = avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, filename)) < 0)
	{
		printf("can not alloc output context");
		return ret;
	}
	for (int i = 0; i < ifmt_ctx->nb_streams; i++)
	{
		//if the stream is video stream then find the encoder default
		//and set context and open the encoder
		if (ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
		{
			out_stream = NULL;
			//new a stream
			out_stream = avformat_new_stream(ofmt_ctx, NULL);
			if (!out_stream)
			{
				printf("can not new stream for output");
				return AVERROR_UNKNOWN;
			}

			//use default video encoder
			out_stream->codec->codec = avcodec_find_encoder(ofmt_ctx->oformat->video_codec);
								
			out_stream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
			out_stream->codec->pix_fmt = PIX_FMT_YUV420P;
			out_stream->codec->width = width;  
			out_stream->codec->height = height;
			out_stream->codec->time_base.num = 1;  
			out_stream->codec->time_base.den = 25;  
			out_stream->codec->bit_rate = 400000;  
			out_stream->codec->gop_size=250;
			//H264
			out_stream->codec->qmin = 10;
			out_stream->codec->qmax = 40;
			//Optional Param
			out_stream->codec->max_b_frames=3;

			AVDictionary *param = NULL;
			if (out_stream->codec->codec->id == AV_CODEC_ID_H264)
			{
				av_dict_set(¶m, "preset", "slow", 0);
				av_dict_set(¶m, "tune", "zerolatency", 0);
				av_dict_set(¶m, "profile", "main", 0);
			}

			//open encoder
			ret = avcodec_open2(out_stream->codec, out_stream->codec->codec, ¶m);

			if (ret < 0)
			{
				printf("can not open encoder");
				return ret;
			}

			if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
				out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;

			break;
		}
	}

	//dump output info
	av_dump_format(ofmt_ctx, 0, filename, 1);

	//open the output file handle
	if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE))
	{
		ret = avio_open(&ofmt_ctx->pb, filename, AVIO_FLAG_WRITE);
		if (ret < 0)
		{
			printf("can not open the output file handle");
			return ret;
		}
	}

	//write output file header
	if ((ret = avformat_write_header(ofmt_ctx, NULL)) < 0)
	{
		printf("can not write output file header");
		return ret;
	}

	return 0;
}

static int init_filters(const char *filters_descr)
{
	char args[512];
	int ret;
	AVFilter *buffersrc  = avfilter_get_by_name("buffer");
	AVFilter *buffersink = avfilter_get_by_name("ffbuffersink");
	AVFilterInOut *outputs = avfilter_inout_alloc();
	AVFilterInOut *inputs  = avfilter_inout_alloc();
	enum PixelFormat pix_fmts[] = { PIX_FMT_YUV420P, PIX_FMT_NONE };
	AVBufferSinkParams *buffersink_params;

	filter_graph = avfilter_graph_alloc();

	/* 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",
		pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
		pCodecCtx->time_base.num, pCodecCtx->time_base.den,
		pCodecCtx->sample_aspect_ratio.num, pCodecCtx->sample_aspect_ratio.den);

	ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
		args, NULL, filter_graph);
	if (ret < 0) {
		printf("Cannot create buffer source\n");
		return ret;
	}

	/* buffer video sink: to terminate the filter chain. */
	buffersink_params = av_buffersink_params_alloc();
	buffersink_params->pixel_fmts = pix_fmts;
	ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
		NULL, buffersink_params, filter_graph);
	av_free(buffersink_params);
	if (ret < 0) {
		printf("Cannot create buffer sink\n");
		return ret;
	}

	/* 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,
		&inputs, &outputs, NULL)) < 0)
		return ret;

	if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
		return ret;
	return 0;
}

static int encode_write_video_frame(AVFrame *filt_frame, int *got_frame) 
{
	int ret;
	int got_frame_local;
	AVPacket enc_pkt;
	unsigned int stream_index = 0;
	if (!got_frame)
		got_frame = &got_frame_local;
	av_log(NULL, AV_LOG_INFO, "Encoding frame\n");
	/* encode filtered frame */
	enc_pkt.data = NULL;
	enc_pkt.size = 0;
	av_init_packet(&enc_pkt);
	ret = avcodec_encode_video2(ofmt_ctx->streams[stream_index]->codec, &enc_pkt,
		filt_frame, got_frame);
	av_frame_free(&filt_frame);
	if (ret < 0)
		return ret;
	if (!(*got_frame))
		return 0;
	/* prepare packet for muxing */
	enc_pkt.stream_index = stream_index;
	enc_pkt.dts = av_rescale_q_rnd(enc_pkt.dts,
		ofmt_ctx->streams[stream_index]->codec->time_base,
		ofmt_ctx->streams[stream_index]->time_base,
		(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
	enc_pkt.pts = av_rescale_q_rnd(enc_pkt.pts,
		ofmt_ctx->streams[stream_index]->codec->time_base,
		ofmt_ctx->streams[stream_index]->time_base,
		(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
	enc_pkt.duration = av_rescale_q(enc_pkt.duration,
		ofmt_ctx->streams[stream_index]->codec->time_base,
		ofmt_ctx->streams[stream_index]->time_base);
	av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
	/* mux encoded frame */
	ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
	return ret;
}

static int filter_encode_write_video_frame(AVFrame *frame)
{
    int ret;
    AVFrame *filt_frame;
    av_log(NULL, AV_LOG_INFO, "Pushing decoded frame to filters\n");
    /* push the decoded frame into the filtergraph */
    ret = av_buffersrc_add_frame_flags(buffersrc_ctx,
            frame, 0);
    if (ret < 0) {
        av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
        return ret;
    }
    /* pull filtered frames from the filtergraph */
    while (1) {
        filt_frame = av_frame_alloc();
        if (!filt_frame) {
            ret = AVERROR(ENOMEM);
            break;
        }
        av_log(NULL, AV_LOG_INFO, "Pulling filtered frame from filters\n");
        ret = av_buffersink_get_frame(buffersink_ctx,
                filt_frame);
        if (ret < 0) {
            /* if no more frames for output - returns AVERROR(EAGAIN)
             * if flushed and no more frames for output - returns AVERROR_EOF
             * rewrite retcode to 0 to show it as normal procedure completion
             */
            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
                ret = 0;
            av_frame_free(&filt_frame);
            break;
        }
        filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
        ret = encode_write_video_frame(filt_frame, NULL);
        if (ret < 0)
            break;
    }
    return ret;
}

int flush_encoder(AVFormatContext *fmt_ctx,unsigned int stream_index)
{
	int ret;
	int got_frame;
	if (!(ofmt_ctx->streams[stream_index]->codec->codec->capabilities &
		CODEC_CAP_DELAY))
		return 0;
	while (1) {
		av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index);
		ret = encode_write_video_frame(NULL, &got_frame);
		if (ret < 0)
			break;
		if (!got_frame)
			return 0;
	}
	return ret;
}

int _tmain(int argc, _TCHAR* argv[])
{
	char filter_descr[100]/* = "movie=my_logo.png[wm];[in][wm]overlay=5:5[out]"*/;
	AVPacket pkt_in, pkt_out;
	unsigned int stream_index;
	int ret;
	AVPacket packet;
	AVFrame *frame;
	int got_frame;
	int width, height;
	width = 400;
	height = 300;
	
	sprintf(filter_descr, "[in]scale=%d:%d[out]", width, height);

	avcodec_register_all();
	av_register_all();
	avfilter_register_all();

	if ((ret = open_input_file("test.mp4")) < 0)
		goto end;
	if ((ret = init_filters(filter_descr)) < 0)
		goto end;
	if ((ret = openoutputfile("test_scale.mp4", width, height)) < 0)
		goto end;
	
	// to be add
	while(1)
	{
		if (av_read_frame(ifmt_ctx, &pkt_in) < 0)
		{
			break;
		}
		pkt_out.data = NULL;
		pkt_out.size = 0;
		av_init_packet(&pkt_out);
		stream_index = pkt_in.stream_index;
		frame = av_frame_alloc();
		int got_frame = -1;
		int ret = -1;

		//calculate the pts and dts
		pkt_in.dts = av_rescale_q_rnd(pkt_in.dts,
			ifmt_ctx->streams[stream_index]->time_base,
			ifmt_ctx->streams[stream_index]->codec->time_base,
			(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
		pkt_in.pts = av_rescale_q_rnd(pkt_in.pts,
			ifmt_ctx->streams[stream_index]->time_base,
			ifmt_ctx->streams[stream_index]->codec->time_base,
			(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));

		
		if (ifmt_ctx->streams[stream_index]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
		{
			ret = avcodec_decode_video2(ifmt_ctx->streams[stream_index]->codec, frame, &got_frame, &pkt_in);
			if (ret < 0)
			{
				av_frame_free(&frame);
				printf("decoding video stream failed\n");
				break;
			}

			if (got_frame) 
			{
				frame->pts = av_frame_get_best_effort_timestamp(frame);
				ret = filter_encode_write_video_frame(frame);
				av_frame_free(&frame);
				if (ret < 0)
					goto end;
			}

		}
	}


	//Flush Encoder
	ret = flush_encoder(ofmt_ctx,0);
	if (ret < 0) {
		printf("Flushing encoder failed\n");
		return -1;
	}

	av_write_trailer(ofmt_ctx);
	
end:
	avfilter_graph_free(&filter_graph);
	if (pCodecCtx)
		avcodec_close(pCodecCtx);
	avformat_close_input(&ifmt_ctx);

	if (ofmt_ctx && !(ofmt_ctx->oformat->flags & AVFMT_NOFILE))
	{
		for (int i = 0; i < ofmt_ctx->nb_streams; i++)
		{
			if (ofmt_ctx->streams[i]->codec)
			{
				avcodec_close(ofmt_ctx->streams[i]->codec);
			}
		}

		avio_close(ofmt_ctx->pb);
	}
	avformat_free_context(ofmt_ctx);

	if (ret < 0 && ret != AVERROR_EOF) {
		char buf[1024];
		av_strerror(ret, buf, sizeof(buf));
		printf("Error occurred: %s\n", buf);
		return -1;
	}

	return 0;
}

3、解释

简单说下流程:open input->open output->init filter -> read packet -> decode frame -> push into filter -> pull from filter -> encode -> write into file

4、工程下载




### 回答1: Ffmpeg是一款开源的视频处理软件,可以通过Filter进行视频处理。Filter可以对视频进行裁剪、缩放、色彩调整、添加水印等各种处理。下面是使用Filter的一些示例命令: 1. 裁剪视频: ``` ffmpeg -i input.mp4 -vf "crop=720:480:20:20" output.mp4 ``` 该命令将输入视频裁剪为720x480的大小,左边和上边各留出20像素的边距。 2. 缩放视频: ``` ffmpeg -i input.mp4 -vf "scale=320:240" output.mp4 ``` 该命令将输入视频缩放为320x240的大小。 3. 调整视频色彩: ``` ffmpeg -i input.mp4 -vf "eq=brightness=0.2:saturation=1.5" output.mp4 ``` 该命令将输入视频亮度降低20%,饱和度增加50%。 4. 添加水印: ``` ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=10:10" output.mp4 ``` 该命令将输入视频和水印图片叠加在一起,水印位置为视频左上角偏移10像素。 以上仅是Filter的一些示例,Ffmpeg支持更多的Filter,可根据需要自行查阅文档使用。 ### 回答2: FFmpeg是一款开源的多媒体处理工具,可以实现视频和音频的录制、转码、编辑等功能。在使用FFmpeg进行视频处理时,Filter是一项重要的功能,可以用来对视频进行各种处理和效果的添加。 FFmpegFilter是通过-vf(video filter)参数来实现的。在命令行中加入-vf参数后,可以添加各种不同的Filter,以达到不同的视频效果。例如,可以使用Filter实现视频的裁剪、旋转、缩放等操作。 具体使用Filter的语法如下: -vf "filter_name=parameter1:value1,parameter2:value2,..." 其中,filter_name表示要使用的Filter名称,parameter表示该Filter的具体参数,value表示参数的值。不同的Filter会有不同的参数,需要根据具体需求进行设置。 例如,要对视频进行旋转的话,可以使用rotate Filter。具体使用方法如下: -vf "rotate=angle=30" 通过以上命令,即可将视频旋转30度。同样的方式,对于其他Filter,只需要将filter_name和parameter替换为对应的值即可。 另外,在Filter中还可以使用Filter链,即将多个Filter串联使用,实现复杂的效果。通过在-vf参数中添加多个Filter,并使用逗号分隔,可以将多个Filter依次执行。 总之,FFmpeg中的Filter功能强大且灵活,可以实现视频进行各种处理和效果的添加。通过适当的命令行参数设置,可以轻松实现视频进行裁剪、旋转、缩放等操作,丰富视频的视觉效果。 ### 回答3: FFmpeg 是一款功能强大的多媒体处理工具,可以用于音视频文件的转换、剪辑、处理等。FilterFFmpeg 的一个重要功能,可以用来对音视频进行各种滤镜处理。 使用 FFmpeg 进行滤镜处理一般需要以下几个步骤: 1. 首先,需要了解 FFmpeg 支持的滤镜和其参数。可以通过命令 `ffmpeg -filters` 查看所有可用滤镜列表,以及命令 `ffmpeg -h filter=<滤镜名称>` 查看特定滤镜的详细参数信息。 2. 选择合适的滤镜后,需要通过命令行指定输入文件和输出文件,以及滤镜参数。下面是一个使用 FFmpeg 添加文本水印的命令示例: ``` ffmpeg -i input.mp4 -vf "drawtext=text='Hello World':fontfile=Arial.ttf:fontsize=24:fontcolor=white:x=10:y=10" output.mp4 ``` 这个命令会将输入文件 `input.mp4` 进行滤镜处理,添加一个文本水印,输出为 `output.mp4`。其中,`drawtext` 是指定滤镜为文本水印,后面的参数是具体的水印文字内容、字体文件、字体大小、字体颜色以及位置等信息。 3. 根据需要,可以添加多个滤镜和参数。可以通过 `;` 或者 `,` 分隔多个滤镜。例如: ``` ffmpeg -i input.mp4 -vf "scale=640:480,rotate=30*PI/180" output.mp4 ``` 这个命令会先将输入文件 `input.mp4` 进行尺寸缩放,然后再旋转一定角度后输出为 `output.mp4`。 4. 最后,运行命令进行滤镜处理。运行命令后,FFmpeg 会按照指定参数进行滤镜处理,并生成输出文件。 需要注意的是,在使用 FFmpeg 进行滤镜处理时,可以根据实际需求选择不同的滤镜和参数,也可以自定义滤镜,以实现特定的效果。同时,滤镜的顺序也可能会影响最终的效果,所以需要根据具体情况进行调整。 总之,FFmpeg 的滤镜功能强大且灵活,可以应用于音视频的各种处理场景,帮助我们实现更多的创意和效果。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dancing_night

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值