利用ffmpeg工具检查视频格式是否符合要求:视频格式为MP4(文件扩展名),视频编码格式为H.264 音频为AAC
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 /// <summary> 2 /// 检查视频格式是否符合要求,视频格式为MP4(文件扩展名),视频编码格式为H.264 音频为AAC 3 /// </summary> 4 /// <param name="fileName">视频文件路径</param> 5 /// <returns>是否符合格式要求</returns> 6 public bool CheckVideoFormat(string fileName) 7 { 8 try 9 { 10 bool flag = false; 11 //启动一个新的进程来运行"ffmpeg" 12 using (Process ffmpeg = new Process()) 13 { 14 ffmpeg.StartInfo.UseShellExecute = false; 15 ffmpeg.StartInfo.ErrorDialog = false; 16 ffmpeg.StartInfo.CreateNoWindow = true; 17 ffmpeg.StartInfo.RedirectStandardError = true; 18 19 //设置ffmpeg的目录 20 ffmpeg.StartInfo.FileName = "ffmpeg.exe"; 21 22 //设置ffmpeg的命令行参数 23 var shortPath = new StringBuilder(1024); 24 GetShortPathName(fileName, shortPath, 1024); 25 ffmpeg.StartInfo.Arguments = "-i " + @shortPath.ToString(); 26 27 //进程操作 28 ffmpeg.Start(); 29 StreamReader standardError = ffmpeg.StandardError; 30 ffmpeg.WaitForExit(); 31 32 //读取标准输出流到结束 33 String result = standardError.ReadToEnd(); 34 35 //从结果字符串中截取编码信息 36 //音频AAC 37 String audio = result.Substring(result.IndexOf("Audio: ") + ("Audio: ").Length, 3); 38 //视频编码格式为H264 39 String video = result.Substring(result.IndexOf("Video: ") + ("Video: ").Length, 4); 40 41 if ("AAC".Equals(audio.ToUpper()) && "H264".Equals(video.ToUpper())) 42 { 43 flag = true; 44 } 45 } 46 return flag; 47 } 48 catch (Exception e) 49 { 50 throw e; 51 } 52 }