.Net C# ffmpeg 视频处理添加logo删除logo 获取视频长度

//删除logo

        string para = string.Format("-i {0} -y -filter_complex \"delogo = x = 5:y = 10:w = 300:h = 110:show=0\"   -b 1024k -acodec copy -f mp4 {1}", videoUrl, targetUrl);
        //para = string.Format("-i {0} -y -filter_complex \"delogo = x =10:y = 1125:w = 709:h = 150:show=1\"   -b 1024k -acodec copy -f mp4 {1}", videoUrl, targetUrl2);
        RunMyProcess(para);
        para = string.Format("-i {0} -y -filter_complex \"delogo = x =410:y = 1155:w = 300:h = 110:show=0\"   -b 1024k -acodec copy -f mp4 {1}", targetUrl, targetUrl2);
        RunMyProcess(para);

        //加图片方法
        //加logo左上角
        //string para = string.Format("-i  {0} -y -i {1} -filter_complex \"overlay=-1:10\" -b 1024k {2}", videoUrl, img_left, targetUrl);

        ////para = string.Format("-i  {0} -y -i {1} -filter_complex \"overlay=main_w-overlay_w-10:10\" -b 1024k {2}", videoUrl, img_left, targetUrl);
        //RunMyProcess(para);
        ////加logo右下角
        //para = string.Format("-i  {0} -y -i {1} -filter_complex \"overlay=main_w-overlay_w-10:main_h-overlay_h-10\" -b 1024k {2}", targetUrl, img_right, targetUrl2);
        //RunMyProcess(para);

        //截取图片
        //string para = string.Format(" -i  {0} -r 1 -y -frames 1 -f image2 -ss 16 -t 513 -s 720x1280 {1}", videoUrl, img);
        //RunMyProcess(para);

        int Duration = GetVideoDuration(FFmpegPath, videoUrl);

static void RunMyProcess(string Parameters)
{
var p = new Process();
p.StartInfo.FileName = FFmpegPath;
p.StartInfo.Arguments = Parameters;
//是否使用操作系统shell启动
p.StartInfo.UseShellExecute = false;
//不显示程序窗口
p.StartInfo.CreateNoWindow = true;
p.Start();
Console.WriteLine("\n开始转码…\n");
p.WaitForExit();
p.Close();
}
///
///
///
/// 设置ffmpeg.exe的路径
/// 视频文件
///
private static int GetVideoDuration(string ffmpegfile, string sourceFile)
{
try
{
using (System.Diagnostics.Process ffmpeg = new System.Diagnostics.Process())
{
String duration; // soon will hold our video’s duration in the form “HH:MM:SS.UU”
String result; // temp variable holding a string representation of our video’s duration
StreamReader errorreader; // StringWriter to hold output from ffmpeg

                // we want to execute the process without opening a shell  
                ffmpeg.StartInfo.UseShellExecute = false;
                //ffmpeg.StartInfo.ErrorDialog = false;  
                ffmpeg.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                // redirect StandardError so we can parse it  
                // for some reason the output comes through over StandardError  
                ffmpeg.StartInfo.RedirectStandardError = true;
                // set the file name of our process, including the full path  
                // (as well as quotes, as if you were calling it from the command-line)  
                ffmpeg.StartInfo.FileName = ffmpegfile;

                // set the command-line arguments of our process, including full paths of any files  
                // (as well as quotes, as if you were passing these arguments on the command-line)  
                ffmpeg.StartInfo.Arguments = "-i " + sourceFile;

                // start the process  
                ffmpeg.Start();

                // now that the process is started, we can redirect output to the StreamReader we defined  
                errorreader = ffmpeg.StandardError;

                // wait until ffmpeg comes back  
                ffmpeg.WaitForExit();

                // read the output from ffmpeg, which for some reason is found in Process.StandardError  
                result = errorreader.ReadToEnd();

                // a little convoluded, this string manipulation...  
                // working from the inside out, it:  
                // takes a substring of result, starting from the end of the "Duration: " label contained within,  
                // (execute "ffmpeg.exe -i somevideofile" on the command-line to verify for yourself that it is there)  
                // and going the full length of the timestamp  

                duration = result.Substring(result.IndexOf("Duration: ") + ("Duration: ").Length, ("00:00:00").Length);

                string[] ss = duration.Split(':');
                int h = int.Parse(ss[0]);
                int m = int.Parse(ss[1]);
                int s = int.Parse(ss[2]);
                return h * 3600 + m * 60 + s;
            }
        }
        catch (System.Exception ex)
        {
            return 60;
        }
    }
发布了55 篇原创文章 · 获赞 2 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/wqs15192095633/article/details/100011868