当前位置:   article > 正文

ffmpeg filter 实现画面旋转_ffmpeg 旋转90度

ffmpeg 旋转90度

工作中发现一个问题:手机设备在拍摄视频时,是通过竖屏拍摄,文件存放时的缩略图也是竖屏

但是在做播放器时,没经过处理的播放器会发现播放时是横着的。
在这里插入图片描述
VLC是竖屏的
在这里插入图片描述在这里插入图片描述
但是其分辨率却是横屏的格式。
在这里插入图片描述

究其原因是因为存放时确实是按照横着来存放的,但是在mp4中会有一个参数标志着播放时需要旋转的角度。rotate。

1.如何找到标志着需要旋转的角度:

在文件格式描述符AVFormatContext的AVStream中有个源数据metadata,用来描述文件的响应信息,将其打印出来发现会有相对应操作的参数,其中就有rotate参数。

AVStream *stream = m_pVideoAVSt;
AVDictionaryEntry *m = NULL;
while ((m = av_dict_get(stream->metadata, "", m, AV_DICT_IGNORE_SUFFIX)) != NULL)
{
	printf("Metadata: Key:%s , value:%s\n", m->key, m->value);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在这里插入图片描述
可以看到旋转的角度是90度。那既然信息已经得到,就寻找方法开搞。

2.用什么方法进行旋转

查找了一下ffmpeg对于播放时旋转画面的操作:

ffmpeg -i fan.jpg -vf transpose=2 -y transpose2.png
  • 1

ok,既然ffmpeg有相对应的指令去操作,那就有相对应的方法去操作。

查找了一下,发现-vf是添加滤镜的意思,发现ffmpeg库中有个叫avfilter的东西,其实就是过滤器。

3.使用ffmpeg过滤器旋转

在这里插入图片描述
可以通过多个过滤器/滤镜对视频或音频进行处理,导出一个或者多个视频或音频。
滤镜的功能很强大,可以加字幕、长度剪切、缩放、画面剪裁、加水印、拼接视频或音频、对画面进行旋转或者镜像处理、加黑边、调音量。

而这里对于视频的旋转,因为画面旋转成竖屏,所以需要两边加个黑边,只需要两个滤镜就够了。

(1)在播放开始前,判断是否旋转,并且旋转多少度,创建过滤器:
头文件:

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavfilter/avfilter.h>
#include <libavfilter/buffersink.h>
#include <libavfilter/buffersrc.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
			AVStream *stream = m_pVideoAVSt;
			AVDictionaryEntry *m = NULL;
			while ((m = av_dict_get(stream->metadata, "", m, AV_DICT_IGNORE_SUFFIX)) != NULL)
			{
				printf("Metadata: Key:%s , value:%s\n", m->key, m->value);
				if (strcmp(m->key, "rotate") == 0)
				{
					m_bRotate = true;
					//strcpy(m_pRotateAngle, m->value);
					if (strcmp(m->value, "90") == 0)
					{
						//顺时针旋转90度,并在视频左右两边填充相对应像素的黑边
						int difference = abs(m_pVideoCodecCtx->height - m_pVideoCodecCtx->width);
						char args[512];
						_snprintf(args, sizeof(args),"transpose=clock,pad=iw+%d:ih:%d", difference, difference/2);
						FilterInit(args);
					}
					else if (strcmp(m->value, "-90") == 0)
					{
						//逆时针旋转90度,并在视频左右两边填充相对应像素的黑边
						int difference = abs(m_pVideoCodecCtx->height - m_pVideoCodecCtx->width);
						char args[512];
						_snprintf(args, sizeof(args), "transpose=cclock,pad=iw+%d:ih:%d", difference, difference / 2);
						FilterInit(args);
					}
				}
			}
  • 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

其中transpose取值:
0 = 90CounterCLockwise and Vertical Flip (default) 逆时针和垂直翻转
1 = 90Clockwise 顺时针旋转
2 = 90CounterClockwise 逆时针旋转
3 = 90Clockwise and Vertical Flip 顺时针和垂直翻转
也可以用这种方式设置。
这里填充黑边用宽度和高度差来进行填充。

int32_t FilterInit(const char *filters_descr)
{

	/**
	* 注冊全部AVFilter
	*/
	avfilter_register_all();

	char args[512];
	int ret = 0;
	const AVFilter *buffersrc = avfilter_get_by_name("buffer");
	const AVFilter *buffersink = avfilter_get_by_name("buffersink");
	AVFilterInOut *outputs = avfilter_inout_alloc();
	AVFilterInOut *inputs = avfilter_inout_alloc();
	enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };

	//为FilterGraph分配内存
	filter_graph = avfilter_graph_alloc();
	if (!outputs || !inputs || !filter_graph) {
		ret = AVERROR(ENOMEM);
		goto freefilter;
	}

	/**
	* 要填入正确的參数
	*/
	_snprintf(args, sizeof(args),
		"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
		m_pVideoCodecCtx->width, m_pVideoCodecCtx->height, m_pVideoCodecCtx->pix_fmt,
		m_pVideoCodecCtx->time_base.num, m_pVideoCodecCtx->time_base.den,
		m_pVideoCodecCtx->sample_aspect_ratio.num, m_pVideoCodecCtx->sample_aspect_ratio.den);

	//创建并向FilterGraph中加入一个Filter
	ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in", args, NULL, filter_graph);
	if (ret < 0) {
		printf("Cannot create buffer source\n");
		goto freefilter;
	}

	//创建并向FilterGraph中加入一个Filter
	ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out", NULL, NULL, filter_graph);
	if (ret < 0) {
		printf("Cannot create buffer sink\n");
		goto freefilter;
	}

	ret = av_opt_set_int_list(buffersink_ctx, "pix_fmts", pix_fmts, AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
	if (ret < 0) {
		printf("Cannot set output pixel format\n");
		goto freefilter;
	}


	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;

	//将一串通过字符串描写叙述的Graph加入到FilterGraph中
	if ((ret = avfilter_graph_parse_ptr(filter_graph, filters_descr, &inputs, &outputs, NULL)) < 0) {
		printf("parse ptr error\n");
		goto freefilter;
	}

	//检查FilterGraph的配置
	if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0) {
		printf("parse config error\n");
		goto freefilter;
	}

	//缓存frame。用来保存filter后的frame
	FilterFrame = av_frame_alloc();
	//uint8_t *out_buffer = (uint8_t *) av_malloc(av_image_get_buffer_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, 1));
	//av_image_fill_arrays(new_frame->data, new_frame->linesize, out_buffer, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, 1);

freefilter:
	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
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88

这里用goto是方便大家在一个函数里看,写上代码时还是用另一个函数来释放,尽量别用goto。

(2)创建完过滤器之后在获取每一帧时,对每一个解码出来的AVFrame进行处理

			if (m_bRotate == true)
			{
				//向FilterGraph中加入一个AVFrame
				ret = av_buffersrc_add_frame(buffersrc_ctx, m_pVideoFrame);
				if (ret >= 0) {
					//从FilterGraph中取出一个AVFrame
					ret = av_buffersink_get_frame(buffersink_ctx, FilterFrame);
					if (ret >= 0) {
						printf("get AVFrame success");
					}
					else {
						printf("Error while getting the filtergraph\n");
					}
				}
				else {
					printf("Error while feeding the filtergraph\n");
				}
			}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

记得每次对该帧AVFrame播放完后对FilterFrame和videoFrame进行清空。

		av_frame_unref(m_pVideoFrame);
		if(m_bRotate == true)
			av_frame_unref(FilterFrame);
  • 1
  • 2
  • 3

就可以得到旋转后的画面已经填充黑边的效果了。
在这里插入图片描述

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号