c# 如何使用FFmpeg转换视频

 winfrom的一个参考demo

private void button1_Click(object sender, EventArgs e)
        {
            string FromName = "E:\\录像\\原始视频.mp4";//原始视频
            string ExportName = "E:\\录像\\转换完的视频.mp4";//转换完的视频
            VideoConvertFlvFast(FromName, ExportName);
        }
        public bool VideoConvertFlvFast(string FromName, string ExportName)
        {
            if (!System.IO.File.Exists(FromName))
            {
                return false;
            }
            string ffmpeg = "";
            try
            {
                ffmpeg = System.AppDomain.CurrentDomain.BaseDirectory + "converter\\avcodec.exe";
            }
            catch (Exception ex)
            {
                return false;

            }
            //FromName = HttpContext.Current.Server.MapPath(FromName);
            //ExportName = HttpContext.Current.Server.MapPath(ExportName);
            //string Command = " -i \"" + FromName + "\" -y -vcodec copy -acodec copy \"" + ExportName + "\""; 
            //string Command = string.Format("-i {0} -vcodec copy -an {1} -y", FromName, ExportName);//->Flv格式 (抛去音频)
            //string Command = " ffmpeg.exe -i F1_02.264 -vcodec copy -an F1_02.flv -y ";

            //string Command = string.Format("-i {0} -ab 128 -acodec libmp3lame -ac 1 -ar 22050 -r 29.97 -qscale 11 {1} -y", FromName, ExportName);//->Flv格式 (抛去音频)
           // string Command = string.Format("-i {0} -r 24 -b 400k -vcodec libx264 -preset ultrafast -qp 35 -y  {1}", FromName, ExportName);//
            string Command = string.Format("-i {0} -r 24 -b 400k -vcodec libx264 -preset ultrafast -qp 35 -y  {1}", FromName, ExportName);//
                                                                                                                                //string Command = string.Format("-i {0} -c:v libx264 -preset medium -b:v 500k -maxrate 500k -bufsize 1000k -cpu-used 5  {1}", FromName, ExportName);//

            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = ffmpeg;
            p.StartInfo.Arguments = Command;
            p.StartInfo.WorkingDirectory = System.AppDomain.CurrentDomain.BaseDirectory + "converter\\";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;//不创建进程窗口

            //开始执行
            p.Start();

            //设置CPU数  这里使用了CPU0和CPU1和CPU3和CPU4
            p.ProcessorAffinity = (IntPtr)(0x0001 | 0x0002 );
            //string str = p.StandardOutput.ReadToEnd();
            p.BeginErrorReadLine();//开始异步读取
            p.WaitForExit();//阻塞等待进程结束
            p.Close();//关闭进程
            p.Dispose();//释放资源

            return true;
        }

注意:在转换的时候很消耗CPU 导致电脑卡顿,这时候我们可以使用

p.ProcessorAffinity = (IntPtr)(0x0001 | 0x0002 );限制CPU的数,防止CPU占用率100%的情况。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C#使用FFmpeg播放视频,可以使用FFmpeg.AutoGen库。首先需要引入FFmpeg.AutoGen.dll文件,并在代码中导入FFmpeg的头文件: ``` using System.Runtime.InteropServices; namespace FFmpegTest { class Program { static void Main(string[] args) { // 初始化FFmpegffmpeg.av_register_all(); ffmpeg.avcodec_register_all(); ffmpeg.avformat_network_init(); // 打开视频文件 AVFormatContext* pFormatCtx = null; if (ffmpeg.avformat_open_input(&pFormatCtx, "test.mp4", null, null) != 0) { Console.WriteLine("无法打开视频文件"); return; } // 获取视频流信息 if (ffmpeg.avformat_find_stream_info(pFormatCtx, null) < 0) { Console.WriteLine("无法获取视频流信息"); return; } // 查找视频流 AVCodec* pCodec = null; AVCodecContext* pCodecCtx = null; int videoStreamIndex = -1; for (int i = 0; i < pFormatCtx->nb_streams; i++) { if (pFormatCtx->streams[i]->codecpar->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO) { videoStreamIndex = i; pCodec = ffmpeg.avcodec_find_decoder(pFormatCtx->streams[i]->codecpar->codec_id); pCodecCtx = ffmpeg.avcodec_alloc_context3(pCodec); ffmpeg.avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[i]->codecpar); break; } } if (videoStreamIndex == -1 || pCodec == null || pCodecCtx == null) { Console.WriteLine("找不到视频流"); return; } // 打开视频解码器 if (ffmpeg.avcodec_open2(pCodecCtx, pCodec, null) < 0) { Console.WriteLine("无法打开视频解码器"); return; } // 解码每一帧视频并渲染 AVPacket packet; AVFrame* pFrame = ffmpeg.av_frame_alloc(); AVFrame* pFrameRGB = ffmpeg.av_frame_alloc(); SwsContext* img_convert_ctx = ffmpeg.sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AVPixelFormat.AV_PIX_FMT_BGR24, 0, null, null, null); int frameFinished; byte[] out_buffer = new byte[ffmpeg.av_image_get_buffer_size(AVPixelFormat.AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height, 1)]; ffmpeg.av_image_fill_arrays(ref pFrameRGB->data[0], ref pFrameRGB->linesize[0], out_buffer, AVPixelFormat.AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height, 1); while (ffmpeg.av_read_frame(pFormatCtx, &packet) >= 0) { // 如果是视频流 if (packet.stream_index == videoStreamIndex) { // 解码一帧视频 ffmpeg.avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet); if (frameFinished != 0) { // 转换颜色空间 ffmpeg.sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize); // 将视频帧渲染到窗口中 // 这里是伪代码,具体渲染方法需要根据具体情况自行实现 RenderFrame(pFrameRGB); } } // 释放packet引用计数 ffmpeg.av_packet_unref(&packet); } // 清理内存 ffmpeg.av_frame_free(&pFrame); ffmpeg.av_frame_free(&pFrameRGB); ffmpeg.avcodec_close(pCodecCtx); ffmpeg.avformat_close_input(&pFormatCtx); ffmpeg.avformat_network_deinit(); } static void RenderFrame(AVFrame* frame) { // 实现视频帧渲染的方法 } static unsafe void* Alloc(int size) { return Marshal.AllocHGlobal(size).ToPointer(); } static unsafe void Free(void* ptr) { Marshal.FreeHGlobal(new IntPtr(ptr)); } } } ``` 上述代码会将视频渲染到窗口中,具体的窗口渲染方法需要根据具体情况自行实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值