FFmpeg添加滤镜调整视频对比度和亮度

原文地址:https://blog.csdn.net/wer85121430/article/details/79642951

目的:调整视频的亮度和对比度。

可参考ffmpeg的源码:

\examples\filtering_video.c,视频滤波例子

\libavfilter\vf_eq.c,亮度对比度调整的具体实现

先使用ffmpeg.exe试试效果,女神的原始视频图片:

FFmpeg添加滤镜调整视频对比度和亮度_第1张图片

ffmpeg.exe -i record.mp4 -vf eq=contrast=1:brightness=-0.2 output.mp4 处理后图片

FFmpeg添加滤镜调整视频对比度和亮度_第2张图片

ffmpeg.exe -i record.mp4 -vf eq=contrast=1:brightness=0.2 output.mp4 处理后图片

FFmpeg添加滤镜调整视频对比度和亮度_第3张图片

效果还可以.

写好了,贴代码,主要是参考这个文件\examples\filtering_video.c,C代码改C#遇到了几个坑花了大半天时间

Init函数的contrast和brightness参数分别对应对比度和亮度,取值1-9,5为原始图像。

[csharp] view plain copy
  1.   
[csharp] view plain copy
  1. public unsafe class VideoFiltering  
  2. {  
  3.     #region 类成员变量  
  4.     AVFilterGraph* m_filter_graph = null;  
  5.     AVFilterContext* m_buffersink_ctx = null;  
  6.     AVFilterContext* m_buffersrc_ctx = null;  
  7.     AVFrame* m_filt_frame = null;  
  8.     Object m_lock_record = new Object();  
  9.     #endregion  
  10.   
  11.     public int Init(int width, int height, int contrast, int brightness)  
  12.     {  
  13.         lock (m_lock_record)  
  14.         {  
  15.             // Critical code section  
  16.             if (m_filter_graph != null)  
  17.                 return -1;  
  18.   
  19.             contrast = contrast < 1 ? 1 : contrast;  
  20.             contrast = contrast > 9 ? 9 : contrast;  
  21.             brightness = brightness < 1 ? 1 : brightness;  
  22.             brightness = brightness > 9 ? 9 : brightness;  
  23.   
  24.             float contrast_f = 1 + ((float)(contrast - 5)) / 10;  
  25.             float brightness_f = 0 + ((float)(brightness - 5)) / 10;  
  26.             string filters_descr = string.Format("eq=contrast=" + contrast_f.ToString() + ":brightness=" + brightness_f.ToString());  
  27.   
  28.             return init_filters(width, height, filters_descr);  
  29.         }  
  30.    }  
  31.     public int Reset(int width, int height, int contrast, int brightness)  
  32.     {  
  33.         Deinit();  
  34.         return Init(width, height, contrast, brightness);  
  35.     }  
  36.   
  37.     public int Filter(AVFrame * frame_src, AVFrame **frame_dst)  
  38.     {  
  39.         lock (m_lock_record)  
  40.         {  
  41.             *frame_dst = frame_src;  
  42.             if (m_filter_graph == null)  
  43.             {  
  44.                 return -1;  
  45.             }  
  46.   
  47.             int ret;  
  48.   
  49.             //AV_BUFFERSRC_FLAG_KEEP_REF = 8,  
  50.             ret = ffmpeg.av_buffersrc_add_frame_flags(m_buffersrc_ctx, frame_src, 8);  
  51.             if (ret < 0)  
  52.                 return ret;  
  53.   
  54.             ret = ffmpeg.av_buffersink_get_frame(m_buffersink_ctx, m_filt_frame);  
  55.             if (ret < 0)  
  56.                 return ret;  
  57.   
  58.             *frame_dst = m_filt_frame;  
  59.   
  60.             return 0;  
  61.         }  
  62.     }  
  63.     public void UnrefFrame()  
  64.     {  
  65.         lock (m_lock_record)  
  66.         {  
  67.             if (m_filter_graph == null)  
  68.                 return ;  
  69.             ffmpeg.av_frame_unref(m_filt_frame);  
  70.         }  
  71.     }  
  72.   
  73.     public void Deinit()  
  74.     {  
  75.         if (m_filter_graph == null)  
  76.             return ;  
  77.   
  78.         if(m_filter_graph != null)  
  79.         {  
  80.             fixed(AVFilterGraph** filter_graph = &m_filter_graph)  
  81.                 ffmpeg.avfilter_graph_free(filter_graph);  
  82.         }  
  83.   
  84.         if (m_filt_frame != null)  
  85.         {  
  86.             fixed (AVFrame** filt_frame = &m_filt_frame)  
  87.                 ffmpeg.av_frame_free(filt_frame);  
  88.         }  
  89.   
  90.     }  
  91.   
  92.     private int init_filters(int width, int height, string filters_descr)  
  93.     {  
  94.         int ret = 0;  
  95.   
  96.         ffmpeg.avfilter_register_all();  
  97.   
  98.         //AVPixelFormat.AV_PIX_FMT_YUV420P = 0;  
  99.         string args = string.Format("video_size=" + width.ToString() + "x" + height.ToString() +  
  100.                                     ":pix_fmt=0:time_base=1/20");  
  101.               
  102.         AVFilter* buffersrc = ffmpeg.avfilter_get_by_name("buffer");  
  103.         AVFilter* buffersink = ffmpeg.avfilter_get_by_name("buffersink");  
  104.         AVFilterInOut* outputs = ffmpeg.avfilter_inout_alloc();  
  105.         AVFilterInOut* inputs = ffmpeg.avfilter_inout_alloc();  
  106.         //AVRational time_base;  
  107.   
  108.         int* pix_fmts = (int*)ffmpeg.av_malloc(8);  
  109.         pix_fmts[0] = (int)AVPixelFormat.AV_PIX_FMT_YUV420P;  
  110.         pix_fmts[1] = (int)AVPixelFormat.AV_PIX_FMT_NONE;  
  111.         //AVPixelFormat pix_fmts[] = { AVPixelFormat.AV_PIX_FMT_YUV420P, AVPixelFormat.AV_PIX_FMT_NONE };  
  112.   
  113.         m_filter_graph = ffmpeg.avfilter_graph_alloc();  
  114.         if(outputs == null || inputs == null || m_filter_graph == null)  
  115.         {  
  116.             ret = -1;  
  117.             goto end;  
  118.         }  
  119.   
  120.         fixed (AVFilterContext** buffersrc_ctx = &m_buffersrc_ctx)  
  121.         {  
  122.             ret = ffmpeg.avfilter_graph_create_filter(buffersrc_ctx, buffersrc, "in", args, null, m_filter_graph);  
  123.             if (ret < 0)  
  124.             {  
  125.                 goto end;  
  126.             }  
  127.         }  
  128.   
  129.         fixed (AVFilterContext** buffersink_ctx = &m_buffersink_ctx)  
  130.         {  
  131.             ret = ffmpeg.avfilter_graph_create_filter(buffersink_ctx, buffersink, "out"nullnull, m_filter_graph);  
  132.             if (ret < 0)  
  133.             {  
  134.                 goto end;  
  135.             }  
  136.         }  
  137.              
  138.   
  139.         int size = (int)ffmpeg.av_int_list_length_for_size(1, (void*)pix_fmts, unchecked((ulong)AVPixelFormat.AV_PIX_FMT_NONE));  
  140.         ret = ffmpeg.av_opt_set_bin(m_buffersink_ctx, "pix_fmts", (byte*)pix_fmts, size, ffmpeg.AV_OPT_SEARCH_CHILDREN);  
  141.         if(ret < 0)  
  142.             goto end;  
  143.   
  144.         outputs->name = ffmpeg.av_strdup("in");  
  145.         outputs->filter_ctx = m_buffersrc_ctx;  
  146.         outputs->pad_idx = 0;  
  147.         outputs->next = null;  
  148.   
  149.         inputs->name = ffmpeg.av_strdup("out");  
  150.         inputs->filter_ctx = m_buffersink_ctx;  
  151.         inputs->pad_idx = 0;  
  152.         inputs->next = null;  
  153.   
  154.         ret = ffmpeg.avfilter_graph_parse_ptr(m_filter_graph, filters_descr, &inputs, &outputs, null);  
  155.         if (ret < 0)  
  156.             goto end;  
  157.   
  158.         ret = ffmpeg.avfilter_graph_config(m_filter_graph, null);  
  159.         if (ret < 0)  
  160.             goto end;  
  161.   
  162.         m_filt_frame = ffmpeg.av_frame_alloc();  
  163.   
  164.         end:  
  165.         ffmpeg.avfilter_inout_free(&inputs);  
  166.         ffmpeg.avfilter_inout_free(&outputs);  
  167.   
  168.         ffmpeg.av_free(pix_fmts);  
  169.   
  170.         if (ret < 0)  
  171.             Deinit();  
  172.   
  173.         return ret;  
  174.     }  
  175. }  


你可能感兴趣的:(音频视频开发,C#)