Beispiel #1
0
        public static int GetProcessPid(string processName)
        {
            string cmd = "";

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                cmd = "ps -aux |grep " + processName + "|grep -v grep|awk \'{print $2}\'";
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                cmd = "ps -A |grep " + processName + "|grep -v grep|awk \'{print $1}\'";
            }

            LinuxShell.Run(cmd, 1000, out string std, out string err);
            if (string.IsNullOrEmpty(std) && string.IsNullOrEmpty(err))
            {
                return(-1);
            }

            int pid = -1;

            if (!string.IsNullOrEmpty(std))
            {
                int.TryParse(std, out pid);
            }
            if (!string.IsNullOrEmpty(err))
            {
                int.TryParse(err, out pid);
            }
            return(pid);
        }
        private static bool ifNotMp4(string ffmpegBinPath, string videoFilePath, out string videoPath)
        {
            string ext         = Path.GetExtension(videoFilePath);
            string newFileName = videoFilePath.Replace(ext, ".mp4");
            string ffmpegCmd   = ffmpegBinPath + " -i " + videoFilePath + " -c copy -movflags faststart " +
                                 newFileName;

            videoPath = newFileName;
            if (!string.IsNullOrEmpty(ext) && !ext.Trim().ToLower().Equals(".mp4"))
            {
                if (LinuxShell.Run(ffmpegCmd, 60 * 1000 * 5, out string std, out string err))
                {
                    if (!string.IsNullOrEmpty(std) || !string.IsNullOrEmpty(err))
                    {
                        if (File.Exists(newFileName))
                        {
                            FileInfo fi = new FileInfo(newFileName);
                            if (fi.Length > 100)
                            {
                                File.Delete(videoFilePath);
                                return(true);
                            }
                            return(false);
                        }
                        return(false);
                    }

                    return(false);
                }
            }
            return(true);
        }
Beispiel #3
0
        /// <summary>
        /// 输出视频的时长(毫秒)
        /// </summary>
        /// <param name="ffmpegBinPath"></param>
        /// <param name="videoFilePath"></param>
        /// <param name="duartion"></param>
        /// <returns></returns>
        public static bool GetDuration(string ffmpegBinPath, string videoFilePath, out long duartion)
        {
            duartion = -1;
            if (File.Exists(ffmpegBinPath) && File.Exists(videoFilePath))
            {
                string cmd = ffmpegBinPath + " -i " + videoFilePath;
                if (LinuxShell.Run(cmd, 1000, out string std, out string err))
                {
                    if (!string.IsNullOrEmpty(std) || !string.IsNullOrEmpty(err))
                    {
                        string tmp = "";
                        if (!string.IsNullOrEmpty(std))
                        {
                            tmp = GetValue(std, "Duration:", ",");
                        }

                        if (string.IsNullOrEmpty(tmp))
                        {
                            tmp = GetValue(err, "Duration:", ",");
                        }

                        if (!string.IsNullOrEmpty(tmp))
                        {
                            string[] tmpArr = tmp.Split(':', StringSplitOptions.RemoveEmptyEntries);
                            if (tmpArr.Length == 3)
                            {
                                int hour = int.Parse(tmpArr[0]);
                                int min  = int.Parse(tmpArr[1]);
                                int sec  = 0;
                                int msec = 0;
                                if (tmpArr[2].Contains('.'))
                                {
                                    string[] tmpArr2 = tmpArr[2].Split('.', StringSplitOptions.RemoveEmptyEntries);
                                    sec  = int.Parse(tmpArr2[0]);
                                    msec = int.Parse(tmpArr2[1]);
                                }
                                else
                                {
                                    sec = int.Parse(tmpArr[2]);
                                }

                                hour     = hour * 3600;              //换成秒数
                                min      = min * 60;
                                sec      = sec + hour + min;         //合计秒数
                                duartion = sec * 1000 + (msec * 10); //算成毫秒
                                LogWriter.WriteLog("获取视频时长:" + duartion.ToString() + "毫秒", videoFilePath);
                                return(true);
                            }
                        }
                    }
                }
            }

            return(false);
        }
Beispiel #4
0
        public static void KillProcess(int pid)
        {
            string cmd = "kill -9 " + pid.ToString();

            LinuxShell.Run(cmd, 1000);
        }