/// <summary> /// 转换一个视频文件 /// </summary> /// <param name="itemSerialNumber"></param> /// <param name="videoFilePath"></param> /// <param name="userName"></param> private static void ConvertVideo(string itemSerialNumber, string videoFilePath, string userName) { writeLog(string.Format("正在试图转换{0}", videoFilePath)); bool isCompleted = false; string errorMessage = string.Empty; try { //转flv //取文件信息 //截图 //转flv小的视频 int nowStatus = (int)VideoStatus.UnConverted; string exeFilePath = string.Empty; string fileExt = Path.GetExtension(videoFilePath).ToLower(); string flvFilePath = Path.Combine(Path.Combine(videoPreviewPath, "Flv"), userName); string imgFilePath = Path.Combine(Path.Combine(videoPreviewPath, "image"), userName); string swfFilePath = Path.Combine(Path.Combine(videoPreviewPath, "swf"), userName); string smallFlvFilePath = Path.Combine(Path.Combine(videoPreviewPath, "smallFlv"), userName); if (!Directory.Exists(flvFilePath)) { Directory.CreateDirectory(flvFilePath); } if (!Directory.Exists(imgFilePath)) { Directory.CreateDirectory(imgFilePath); } if (!Directory.Exists(swfFilePath)) { Directory.CreateDirectory(swfFilePath); } if (!Directory.Exists(smallFlvFilePath)) { Directory.CreateDirectory(smallFlvFilePath); } flvFilePath = Path.Combine(flvFilePath, itemSerialNumber + ".flv"); imgFilePath = Path.Combine(imgFilePath, itemSerialNumber + ".jpg"); swfFilePath = Path.Combine(swfFilePath, itemSerialNumber + ".swf"); smallFlvFilePath = Path.Combine(smallFlvFilePath, itemSerialNumber + ".flv"); if (specialVideoFormats.Contains(fileExt)) { exeFilePath = ffmpegRmFilePath; } else { exeFilePath = ffmpegFilePath; } writeLog(string.Format("使用这个Exe文件转换,{0}", exeFilePath + fileExt)); if (File.Exists(exeFilePath)) { VideoController vc = new VideoController(exeFilePath); if (fileExt.EndsWith("flv")) { //flv文件只拷贝过去即可 File.Copy(videoFilePath, flvFilePath, true); } else { //转换成flv vc.ConvertToFlv(videoFilePath, flvFilePath); } //没有转成flv if (!File.Exists(flvFilePath) || new FileInfo(flvFilePath).Length < 10) { File.Delete(flvFilePath); //尝试用 mencoder 来转换,这部分先留着,看看能有多少不成功的 } else { //截图,取第一帧图像 long imageSize = 0; double startSecond = 0.001; do { writeLog("截图\r\n" + vc.ConvertToThumbnails(videoFilePath, imgFilePath, startSecond)); imageSize = new FileInfo(imgFilePath).Length; startSecond = startSecond + 1; }while(imageSize < 4000 && startSecond < 11); //当图片小于4000字节时,认为图片颜色过于单一,在下一秒继续取图,共取10次, //也可以利用图片灰度来做比较 //转成小的flv vc.ConvertToSmallFlv(flvFilePath, smallFlvFilePath); } //取文件信息 VideoFile v = vc.getVideoInfo(videoFilePath); writeLog("获取文件的MetaData" + v.ClipLength); //写回服务器 WS.VideoStorageService vss = new WS.VideoStorageService(); //vss.UpdateVideoMetaData(itemSerialNumber, v.ClipLength, v.Bitrate, v.ClipSize); Dictionary <string, string> dct = new Dictionary <string, string>(); //通过反射得到所有的附加属性以及相应的值 Type t = v.GetType(); PropertyInfo[] piArray = t.GetProperties(); foreach (PropertyInfo pi in piArray) { if (pi.GetValue(v, null) != null) { dct.Add(pi.Name, pi.GetValue(v, null).ToString()); } } List <ResourceWS.DictionaryEntry> lst = new List <ResourceWS.DictionaryEntry>(); foreach (string key in dct.Keys) { ResourceWS.DictionaryEntry de = new ResourceWS.DictionaryEntry(); de.Key = key; de.Value = dct[key]; lst.Add(de); } ResourceWS.DictionaryEntry[] result = lst.ToArray(); ResourceWS.ResourceService rs = new ResourceWS.ResourceService(); rs.insertResourceAttributes(itemSerialNumber, result); if (File.Exists(flvFilePath) && File.Exists(imgFilePath)) { nowStatus = (int)VideoStatus.Converted; } else { nowStatus = (int)VideoStatus.ConvertError; } //将结果更新到数据库中 vss.UpdateVideoStatus(itemSerialNumber, nowStatus); } else { errorMessage = string.Format("在转换文件{0}时,没有找到可执行程序:{1}", videoFilePath, exeFilePath); writeLog(logFile, errorMessage); } } catch (Exception e1) { writeLog(logFile, e1.StackTrace + e1.Message); } }