飞道的博客

C#调用Process执行ffmpeg视频转码七分钟后就假死了

544人阅读  评论(0)

问题描述

ffmpeg  -i  /wzfgs_img_storage/tuku.img_linux/upload/20210421_164313_803_a2a2.mov  -vcodec h264 /video_list/MOV_002.mp4

单独在终端中执行上面的命令转码是可以处理完成视频的,但是用C#的Process调用ffmpeg进行转码,测试多次发现ffmpeg转码大概7分钟程序都会假死,实际查看进程ffmpeg又没有完全退出,但是视频就是中断转码了,后面将代码调整,用错误消息事件输出了ffmpeg的输出消息,这下就可以转换几个GB的视频了,没有出现转码中断情况;

解决关键代码:

主要是要把ffmpeg的输出消息接收到,这样就可以保证转码视频纵然转码耗时三个小时,也是稳定输出正常视频的

//程序异常输出消息
process.ErrorDataReceived += (o, e) =>
{
   
	if (string.IsNullOrWhiteSpace(e?.Data))
	{
   
		return;
	}
	Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "程序异常消息:" + e.Data);
};
startInfo.RedirectStandardError = true;
process.BeginErrorReadLine();

本程序在CentOS8中执行

终端命令


#192.168.1.117部署控制台程序路径:
cd /consoleAppTest

#控制台发布的程序压缩包publish.zip上传到
/consoleAppTest

#解压文件
unzip publish.zip

#给程序授予执行权限
chmod +x  ./publish/ConsoleAppNET5.TestVideoZhuanMa

#直接执行
./publish/ConsoleAppNET5.TestVideoZhuanMa

#后台静默执行,会自动生成输出文件nohup.out
nohup  ./publish/ConsoleAppNET5.TestVideoZhuanMa   &

完整有效参考代码

using Microsoft.AspNetCore.SignalR.Client;
using System;
using System.Diagnostics;
using System.Threading.Tasks;

namespace ConsoleAppNET5.TestVideoZhuanMa
{
   
 
    class Program
    {
   

        static async Task Main(string[] args)
        {
   
            HubConnection connection = new HubConnectionBuilder()
                .WithUrl("http://192.168.1.239:8050/ChatHub")
                .WithAutomaticReconnect()
                .Build();
            connection.Reconnecting += error =>
            {
   
                Console.WriteLine("正在重新连接");
                return Task.CompletedTask;
            };
            connection.Closed += async (error) =>
            {
   
                Console.WriteLine("10秒后重新连接");
                await Task.Delay(10 * 1000);
                try
                {
   
                    //连接
                    await connection.StartAsync();
                    Console.WriteLine("连接成功");
                }
                catch (Exception ex)
                {
   
                    Console.WriteLine("连接signlr异常:" + ex.ToString());
                }
            };
            try
            {
   
                //连接
                await connection.StartAsync();
                Console.WriteLine("连接成功");
            }
            catch (Exception ex)
            {
   
                Console.WriteLine("连接signlr异常:" + ex.ToString());
            }
            //try
            //{
   
            //    //发送消息
            //    await connection.InvokeAsync("SendMessage",
            //        "wusong001", "控制台消息1122");
            //}
            //catch (Exception ex)
            //{
   
            //    Console.WriteLine("发送消息异常" + ex.ToString());
            //}    

            try
            {
   
                Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "开始转码...");
                var process = new System.Diagnostics.Process();
                //进程优先级设置
                //No process is associated with this object
                //process.PriorityClass = ProcessPriorityClass.High; 
                //process.PriorityClass = ProcessPriorityClass.AboveNormal; 

                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();

                //应该触发退出事件
                process.EnableRaisingEvents = true;

                //程序退出事件
                process.Exited += (s, e) =>
                {
   
                    stopwatch.Stop();
                    Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "程序1退出,总耗时(毫秒)" + stopwatch.ElapsedMilliseconds);
                };

                //程序输出消息事件
                process.OutputDataReceived += (o, e) =>
                {
   
                    if (string.IsNullOrWhiteSpace(e?.Data))
                    {
   
                        return;
                    }
                    Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "程序1执行消息:" + e.Data);
                };
                //程序异常输出消息
                process.ErrorDataReceived += async(o, e) =>
                {
   
                    if (string.IsNullOrWhiteSpace(e?.Data))
                    {
   
                        return;
                    }
                    Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "程序异常消息:" + e.Data);
                    try
                    {
   
                        //发送消息
                        await connection.InvokeAsync("SendMessage",
                            "transcoding", e.Data);
                    }
                    catch (Exception ex)
                    {
   
                        Console.WriteLine("singnalr发送消息异常:" + ex.ToString());
                    }
                };

                //var startInfo = new System.Diagnostics.ProcessStartInfo("ping", "www.qq.com -t");
                // -i /video_list/IMG_0837.MOV -vcodec h264 /video_list/IMG_0837_10A.mp4
                //var startInfo = new System.Diagnostics.ProcessStartInfo("ffmpeg", " -i /video_list/IMG_0837.MOV -vcodec h264 /video_list/IMG_0837_19A.mp4");

                //31秒
                //var startInfo = new System.Diagnostics.ProcessStartInfo("ffmpeg", " -i  /wzfgs_img_storage/tuku.img_linux/upload/20210421_164313_803_a2a2.mov  -vcodec h264 /video_list/MOV_001.mp4");

                //视频大小3GB,将视频转码为MP4格式,编码为h264,支持web浏览器video播放
                //var startInfo = new System.Diagnostics.ProcessStartInfo("ffmpeg", " -i  /video_list/扎克·施奈德版正义联盟.rmvb  -vcodec h264 /video_list/zake003.mp4");
                var startInfo = new System.Diagnostics.ProcessStartInfo("ffmpeg", " -i  /wzfgs_img_storage/tuku.img_linux/upload/20210421_164313_803_a2a2.mov  -vcodec h264  /video_list/MOV_004.mp4");

                startInfo.RedirectStandardError = true;
                startInfo.RedirectStandardOutput = true;
                startInfo.RedirectStandardInput = true;

                process.StartInfo = startInfo;
                process.Start();
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
                process.WaitForExit();
                process.Close();
                process.Dispose();
                //process.Kill();
                Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "转码完成");
            }
            catch (Exception ex)
            {
   
                Console.WriteLine("转码异常" + ex.ToString());
            }
        }
    }
}


终端输出消息

2021-04-25 20:01:05程序异常消息:frame=219292 fps= 18 q=28.0 size= 2665984kB time=02:32:24.88 bitrate=2388.2kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:05程序异常消息:frame=219304 fps= 18 q=28.0 size= 2665984kB time=02:32:24.88 bitrate=2388.2kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:06程序异常消息:frame=219315 fps= 18 q=28.0 size= 2666240kB time=02:32:26.74 bitrate=2387.9kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:06程序异常消息:frame=219327 fps= 18 q=28.0 size= 2666240kB time=02:32:26.74 bitrate=2387.9kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:07程序异常消息:frame=219339 fps= 18 q=28.0 size= 2666240kB time=02:32:26.74 bitrate=2387.9kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:07程序异常消息:frame=219351 fps= 18 q=28.0 size= 2666496kB time=02:32:26.74 bitrate=2388.2kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:08程序异常消息:frame=219363 fps= 18 q=28.0 size= 2666496kB time=02:32:28.59 bitrate=2387.7kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:08程序异常消息:frame=219379 fps= 18 q=28.0 size= 2666496kB time=02:32:28.59 bitrate=2387.7kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:09程序异常消息:frame=219395 fps= 18 q=28.0 size= 2666752kB time=02:32:28.62 bitrate=2387.9kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:09程序异常消息:frame=219409 fps= 18 q=28.0 size= 2666752kB time=02:32:30.45 bitrate=2387.4kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:10程序异常消息:frame=219427 fps= 18 q=28.0 size= 2666752kB time=02:32:30.45 bitrate=2387.4kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:10程序异常消息:frame=219443 fps= 18 q=28.0 size= 2667008kB time=02:32:32.31 bitrate=2387.2kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:11程序异常消息:frame=219459 fps= 18 q=28.0 size= 2667008kB time=02:32:32.31 bitrate=2387.2kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:11程序异常消息:frame=219475 fps= 18 q=28.0 size= 2667008kB time=02:32:32.31 bitrate=2387.2kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:12程序异常消息:frame=219489 fps= 18 q=28.0 size= 2667264kB time=02:32:34.17 bitrate=2386.9kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:12程序异常消息:frame=219505 fps= 18 q=28.0 size= 2667264kB time=02:32:34.17 bitrate=2386.9kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:13程序异常消息:frame=219520 fps= 18 q=28.0 size= 2667264kB time=02:32:34.17 bitrate=2386.9kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:14程序异常消息:frame=219535 fps= 18 q=28.0 size= 2667520kB time=02:32:36.02 bitrate=2386.7kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:14程序异常消息:frame=219549 fps= 18 q=28.0 size= 2667520kB time=02:32:36.02 bitrate=2386.7kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:15程序异常消息:frame=219562 fps= 18 q=28.0 size= 2667520kB time=02:32:36.02 bitrate=2386.7kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:15程序异常消息:frame=219575 fps= 18 q=28.0 size= 2667776kB time=02:32:36.07 bitrate=2386.9kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:16程序异常消息:frame=219588 fps= 18 q=28.0 size= 2667776kB time=02:32:37.88 bitrate=2386.4kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:16程序异常消息:frame=219600 fps= 18 q=28.0 size= 2668032kB time=02:32:37.88 bitrate=2386.6kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:17程序异常消息:frame=219614 fps= 18 q=28.0 size= 2668032kB time=02:32:37.88 bitrate=2386.6kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:17程序异常消息:frame=219629 fps= 18 q=28.0 size= 2668288kB time=02:32:39.74 bitrate=2386.4kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:19程序异常消息:frame=219640 fps= 18 q=-1.0 Lsize= 2677244kB time=02:32:41.64 bitrate=2393.9kbits/s dup=1361 drop=0 speed=0.768x    
2021-04-25 20:01:19程序异常消息:video:2524975kB audio:143589kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.325273%
2021-04-25 20:01:19程序异常消息:[libx264 @ 0x66216c0] frame I:2574  Avg QP:15.81  size: 48340
2021-04-25 20:01:19程序异常消息:[libx264 @ 0x66216c0] frame P:76790 Avg QP:20.95  size: 20647
2021-04-25 20:01:19程序异常消息:[libx264 @ 0x66216c0] frame B:140276 Avg QP:24.04  size:  6243
2021-04-25 20:01:19程序异常消息:[libx264 @ 0x66216c0] consecutive B-frames:  4.8% 28.9%  3.4% 62.9%
2021-04-25 20:01:19程序异常消息:[libx264 @ 0x66216c0] mb I  I16..4: 28.7% 60.2% 11.1%
2021-04-25 20:01:19程序异常消息:[libx264 @ 0x66216c0] mb P  I16..4:  7.2% 13.2%  1.4%  P16..4: 24.0%  6.1%  2.2%  0.0%  0.0%    skip:45.9%
2021-04-25 20:01:19程序异常消息:[libx264 @ 0x66216c0] mb B  I16..4:  0.7%  1.0%  0.2%  B16..8: 26.3%  1.6%  0.1%  direct: 1.0%  skip:69.0%  L0:43.0% L1:52.0% BI: 5.0%
2021-04-25 20:01:19程序异常消息:[libx264 @ 0x66216c0] 8x8 transform intra:59.6% inter:77.5%
2021-04-25 20:01:19程序异常消息:[libx264 @ 0x66216c0] coded y,uvDC,uvAC intra: 30.5% 29.6% 4.6% inter: 5.4% 5.4% 0.1%
2021-04-25 20:01:19程序异常消息:[libx264 @ 0x66216c0] i16 v,h,dc,p: 29% 24% 10% 37%
2021-04-25 20:01:19程序异常消息:[libx264 @ 0x66216c0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 32% 21% 24%  3%  4%  4%  4%  4%  4%
2021-04-25 20:01:19程序异常消息:[libx264 @ 0x66216c0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 31% 23% 14%  4%  6%  7%  6%  5%  3%
2021-04-25 20:01:19程序异常消息:[libx264 @ 0x66216c0] i8c dc,h,v,p: 62% 17% 18%  3%
2021-04-25 20:01:19程序异常消息:[libx264 @ 0x66216c0] Weighted P-Frames: Y:0.4% UV:0.3%
2021-04-25 20:01:19程序异常消息:[libx264 @ 0x66216c0] ref P L0: 72.5%  9.7% 13.1%  4.7%  0.0%
2021-04-25 20:01:19程序异常消息:[libx264 @ 0x66216c0] ref B L0: 90.9%  7.9%  1.2%
2021-04-25 20:01:19程序异常消息:[libx264 @ 0x66216c0] ref B L1: 96.5%  3.5%
2021-04-25 20:01:19程序异常消息:[libx264 @ 0x66216c0] kb/s:2258.03
2021-04-25 20:01:19程序异常消息:[aac @ 0x6680ac0] Qavg: 272.739
2021-04-25 20:01:19程序1退出,总耗时(毫秒)11926342
2021-04-25 20:01:19转码完成
2021-04-25 20:01:33转换task状态:RanToCompletion
2021-04-25 20:01:33转换完成2
2021-04-25 20:01:33退出while循环主线程
[root@localhost video_list]# 


转载:https://blog.csdn.net/u011511086/article/details/116143100
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场