视频转码h264格式,并将元数据放入第一帧

c# 利用ffmpeg进行开发。主要为了方便转后的视频在网页中通过ckpaly播放,并压缩视频文件节省存储空间。将元数据放入视频第一帧可以 不用缓冲太多就可以秒播。

核心代码

string name = f.Name;
                string tmp_name = Guid.NewGuid() + ".mp4";
                string[] spr = name.Split('.');//然后根据.分割文件名称
                string minzhm = spr[spr.Length - 1];//获取最后一个,这样就获取到了文件的后缀名
                string[] allowExtension = { "avi", "mp4", "mov", "f4v", "mpg", "mpeg", "flv" };//视频的常用格式
                int index = Array.IndexOf(allowExtension, minzhm.ToLower());
                if (Array.IndexOf(allowExtension, minzhm.ToLower()) == -1)//然后把后缀名统一改为小写,比较一下是否存在,存在的话,表示是视频
                    continue;
                fmain.SetDataRichTextBox_rtb0(f.Name + " 开始转换mp4 H264格式...\n");
                String Command = " -i " + '"' + insrc + "\\" + f.Name + '"' + "  -y " + watermark + " -vcodec h264 -b 500000  " + '"' + outsrc + "\\" + f.Name + '"';
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.FileName = ffmpeg_src;
                p.StartInfo.Arguments = Command;
                p.StartInfo.UseShellExecute = false;  ////不使用系统外壳程序启动进程  
                p.StartInfo.CreateNoWindow = true;  //不显示dos程序窗口  
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中  
                p.ErrorDataReceived += new DataReceivedEventHandler(Error_Output);
                p.OutputDataReceived += new DataReceivedEventHandler(Output);
                p.Start();
                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                //p.BeginOutputReadLine();
                p.BeginErrorReadLine();//开始异步读取  
                p.WaitForExit();//阻塞等待进程结束  
                p.Close();//关闭进程  
                p.Dispose();//释放资源
                //SetDataRichTextBox_rtb0(f.Name + " 格式转换完成,开始提取元数据...\n");
                fmain.SetDataRichTextBox_rtb0(f.Name + " 格式转换完成,开始提取元数据...\n");
                Command = insrc + "\\" + f.Name + " " +outsrc + "\\" + f.Name;
                p = new System.Diagnostics.Process();
                p.StartInfo.FileName = qtfast_src;
                p.StartInfo.Arguments = Command;
                p.StartInfo.UseShellExecute = false;  ////不使用系统外壳程序启动进程  
                p.StartInfo.CreateNoWindow = true;  //不显示dos程序窗口  
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中  
                p.ErrorDataReceived += new DataReceivedEventHandler(Error_Output);
                p.OutputDataReceived += new DataReceivedEventHandler(Output);
                p.Start();
                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                p.BeginErrorReadLine();//开始异步读取  
                p.WaitForExit();//阻塞等待进程结束  
                p.Close();//关闭进程  
                p.Dispose();//释放资源

                fmain.SetDataRichTextBox_rtb0(f.Name + " 提取元数据成功!end\n");


程序界面



链接:https://pan.baidu.com/s/1_odHa3_2OZA0Ut0JRWONSw 密码:g3di

csdn下载:https://download.csdn.net/download/tbzhang30/10408687

猜你喜欢

转载自blog.csdn.net/tbzhang30/article/details/80283869