日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

c++ ffmpeg内存推流_最简单的基于FFmpeg的AVfilter的例子

發(fā)布時間:2025/3/15 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++ ffmpeg内存推流_最简单的基于FFmpeg的AVfilter的例子 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

此前l(fā)ibavfilter一直是結(jié)合著libavcodec等類庫的接口函數(shù)使用的,因此我一直以為libavfilter庫與libavcodec等類庫是高度耦合的(也就是如果想使用libavfilter的視音頻特效功能的話必須使用libavcodec等類庫的函數(shù))。這兩天空閑的時候研究了一下libavfilter的代碼后發(fā)現(xiàn)實(shí)際情況不是這樣的:libavfilter可以獨(dú)立于libavcodec等類庫的接口函數(shù)作為一個“純粹”的視音頻特效類庫進(jìn)行使用。本文記錄的“純凈版”的avfilter的例子即實(shí)現(xiàn)了一個純粹的視頻特效添加的功能。該例子輸入為一個YUV文件,輸出也是一個YUV文件,通過avfilter的功能可以處理該YUV文件實(shí)現(xiàn)去色調(diào)、模糊、水平翻轉(zhuǎn)、裁剪、加方框、疊加文字等功能。

流程圖

該程序的流程圖如下所示。AVFilter的初始化比較復(fù)雜,而使用起來比較簡單。初始化的時候需要調(diào)用avfilter_register_all()到avfilter_graph_config()一系列函數(shù)。而使用的時候只有兩個函數(shù):av_buffersrc_add_frame()用于向FilterGraph中加入一個AVFrame,而av_buffersink_get_frame()用于從FilterGraph中取出一個AVFrame。

流程中的關(guān)鍵函數(shù)如下所示:

avfilter_register_all():注冊所有AVFilter。

avfilter_graph_alloc():為FilterGraph分配內(nèi)存。

avfilter_graph_create_filter():創(chuàng)建并向FilterGraph中添加一個Filter。

avfilter_graph_parse_ptr():將一串通過字符串描述的Graph添加到FilterGraph中。

avfilter_graph_config():檢查FilterGraph的配置。

av_buffersrc_add_frame():向FilterGraph中加入一個AVFrame。

av_buffersink_get_frame():從FilterGraph中取出一個AVFrame。

代碼

/** * 最簡單的基于FFmpeg的AVFilter例子 - 純凈版 * Simplest FFmpeg AVfilter Example - Pure * * 雷霄驊 Lei Xiaohua * leixiaohua1020@126.com * 中國傳媒大學(xué)/數(shù)字電視技術(shù) * Communication University of China / Digital TV Technology * http://blog.csdn.net/leixiaohua1020 * * 本程序使用FFmpeg的AVfilter實(shí)現(xiàn)了YUV像素數(shù)據(jù)的濾鏡處理功能。 * 可以給YUV數(shù)據(jù)添加各種特效功能。 * 是最簡單的FFmpeg的AVFilter方面的教程。 * 適合FFmpeg的初學(xué)者。 * * This software uses FFmpeg's AVFilter to process YUV raw data. * It can add many excellent effect to YUV data. * It's the simplest example based on FFmpeg's AVFilter. * Suitable for beginner of FFmpeg * */#include #define __STDC_CONSTANT_MACROS #ifdef _WIN32#define snprintf _snprintf//Windowsextern "C"{#include "libavfilter/avfiltergraph.h"#include "libavfilter/buffersink.h"#include "libavfilter/buffersrc.h"#include "libavutil/avutil.h"#include "libavutil/imgutils.h"};#else//Linux...#ifdef __cplusplusextern "C"{#endif#include #include #include #include #include #ifdef __cplusplus};#endif#endif int main(int argc, char* argv[]){ int ret; AVFrame *frame_in;AVFrame *frame_out;unsigned char *frame_buffer_in;unsigned char *frame_buffer_out; AVFilterContext *buffersink_ctx;AVFilterContext *buffersrc_ctx;AVFilterGraph *filter_graph;static int video_stream_index = -1; //Input YUVFILE *fp_in=fopen("sintel_480x272_yuv420p.yuv","rb+");if(fp_in==NULL){printf("Error open input file.");return -1;}int in_width=480;int in_height=272; //Output YUVFILE *fp_out=fopen("output.yuv","wb+");if(fp_out==NULL){printf("Error open output file.");return -1;} //const char *filter_descr = "lutyuv='u=128:v=128'";const char *filter_descr = "boxblur";//const char *filter_descr = "hflip";//const char *filter_descr = "hue='h=60:s=-3'";//const char *filter_descr = "crop=2/3*in_w:2/3*in_h";//const char *filter_descr = "drawbox=x=100:y=100:w=100:h=100:color=pink@0.5";//const char *filter_descr = "drawtext=fontfile=arial.ttf:fontcolor=green:fontsize=30:text='Lei Xiaohua'";avfilter_register_all(); char args[512];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[] = { AV_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",in_width,in_height,AV_PIX_FMT_YUV420P,1, 25,1,1); ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",args, NULL, filter_graph);if (ret < 0) {printf("Cannot create buffer source");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");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, filter_descr,&inputs, &outputs, NULL)) < 0)return ret; if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)return ret; frame_in=av_frame_alloc();frame_buffer_in=(unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, in_width,in_height,1));av_image_fill_arrays(frame_in->data, frame_in->linesize,frame_buffer_in,AV_PIX_FMT_YUV420P,in_width, in_height,1); frame_out=av_frame_alloc();frame_buffer_out=(unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, in_width,in_height,1));av_image_fill_arrays(frame_out->data, frame_out->linesize,frame_buffer_out,AV_PIX_FMT_YUV420P,in_width, in_height,1); frame_in->width=in_width;frame_in->height=in_height;frame_in->format=AV_PIX_FMT_YUV420P; while (1) { if(fread(frame_buffer_in, 1, in_width*in_height*3/2, fp_in)!= in_width*in_height*3/2){break;}//input Y,U,Vframe_in->data[0]=frame_buffer_in;frame_in->data[1]=frame_buffer_in+in_width*in_height;frame_in->data[2]=frame_buffer_in+in_width*in_height*5/4; if (av_buffersrc_add_frame(buffersrc_ctx, frame_in) < 0) { printf( "Error while add frame."); break; } /* pull filtered pictures from the filtergraph */ret = av_buffersink_get_frame(buffersink_ctx, frame_out); if (ret < 0) break; //output Y,U,Vif(frame_out->format==AV_PIX_FMT_YUV420P){for(int i=0;iheight;i++){fwrite(frame_out->data[0]+frame_out->linesize[0]*i,1,frame_out->width,fp_out);}for(int i=0;iheight/2;i++){fwrite(frame_out->data[1]+frame_out->linesize[1]*i,1,frame_out->width/2,fp_out);}for(int i=0;iheight/2;i++){fwrite(frame_out->data[2]+frame_out->linesize[2]*i,1,frame_out->width/2,fp_out);}}printf("Process 1 frame!");av_frame_unref(frame_out); } fclose(fp_in);fclose(fp_out); av_frame_free(&frame_in);av_frame_free(&frame_out); avfilter_graph_free(&filter_graph); return 0;}

結(jié)果

本程序輸入為一個名稱為“sintel_480x272_yuv420p.yuv”的YUV420P視頻數(shù)據(jù),輸出為一個名稱為“output.yuv” 的YUV420P視頻數(shù)據(jù)。輸入的視頻數(shù)據(jù)的內(nèi)容如下所示。

程序中提供了幾種特效:

lutyuv='u=128:v=128'

boxblur

hflip

hue='h=60:s=-3'

crop=2/3*in_w:2/3*in_h

drawbox=x=100:y=100:w=100:h=100:color=pink@0.5

drawtext=fontfile=arial.ttf:fontcolor=green:fontsize=30:text='Lei Xiaohua'

可以通過修改程序中的filter_descr字符串實(shí)現(xiàn)上述幾種特效。下面展示幾種特效的效果圖。

lutyuv='u=128:v=128'

boxblur

hflip

hue='h=60:s=-3'

crop=2/3*in_w:2/3*in_h

drawbox=x=100:y=100:w=100:h=100:color=pink@0.5

drawtext=fontfile=arial.ttf:fontcolor=green:fontsize=30:text='Lei Xiaohua'

————————————————

版權(quán)聲明:本文為CSDN博主「雷霄驊」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。

原文鏈接:https://blog.csdn.net/leixiaohua1020/article/details/50618190

另外還有一些關(guān)于c++ Linux后臺服務(wù)器開發(fā)的一些知識點(diǎn)分享:Linux,Nginx,MySQL,Redis,P2P,K8S,Docker,TCP/IP,協(xié)程,DPDK,webrtc,音視頻等等視頻。

需要的朋友可以后臺私信【1】獲取學(xué)習(xí)視頻

總結(jié)

以上是生活随笔為你收集整理的c++ ffmpeg内存推流_最简单的基于FFmpeg的AVfilter的例子的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。