Program.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Threading;
  5. using vCloud.Server.Utilities;
  6. using Vinno.vCloud.Common.Vid2;
  7. namespace Mpeg4ConverterTool
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. var downloadPath = args.Length>0 ? args.GetValue(0)?.ToString(): string.Empty;
  14. var destPath = args.Length>1 ? args.GetValue(1)?.ToString() : string.Empty;
  15. if (string.IsNullOrWhiteSpace(downloadPath))
  16. {
  17. downloadPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "download");
  18. }
  19. if (string.IsNullOrWhiteSpace(destPath))
  20. {
  21. destPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "dest");
  22. }
  23. //var signNode = UFileSignInstance.GetUFileSignNode(StorageNodeType.FileStorageCDN);
  24. //var requestUrl = signNode.GetUrl(fileName);
  25. //var vidFile = Path.Combine(downloadPath, fileName);
  26. //var success = UFileStorageSingle.Instance.DownloadFile(vidFile, requestUrl);
  27. //if (success)
  28. //{
  29. // try
  30. // {
  31. // var vinnoImageData = new VinnoImageData(vidFile, OperationMode.Open);
  32. // Directory.CreateDirectory(Path.GetDirectoryName(destPath));
  33. // if (vinnoImageData.ImageFormat == VidImageFormat.H264)
  34. // {
  35. // var destFile = Path.Combine(destPath, fileName.Replace(".dat", ".mp4"));
  36. // Console.WriteLine(destFile);
  37. // if (File.Exists(destFile))
  38. // {
  39. // File.Delete(destFile);
  40. // }
  41. // Mpeg4Converter.ConvertVidToMpeg4(vinnoImageData, destFile);
  42. // if (File.Exists(destFile))
  43. // {
  44. // Console.WriteLine($"Convert success!");
  45. // return;
  46. // }
  47. // }
  48. // else
  49. // {
  50. // var destFile = Path.Combine(destPath, fileName.Replace(".dat", ".jpeg"));
  51. // File.WriteAllBytes(destFile, vinnoImageData.GetImage(0)?.ImageData);
  52. // Console.WriteLine($"Convert success!");
  53. // return;
  54. // }
  55. // Console.WriteLine($"Convert fail!");
  56. // }
  57. // catch (Exception ex)
  58. // {
  59. // Console.WriteLine($"Convert fail!{ex}");
  60. // return;
  61. // }
  62. //}
  63. //else
  64. //{
  65. // Console.WriteLine("Download fail!");
  66. // return;
  67. //}
  68. Directory.CreateDirectory(Path.GetDirectoryName(destPath));
  69. Directory.CreateDirectory(Path.GetDirectoryName(downloadPath));
  70. CreateWatcherForLinux(downloadPath, destPath);
  71. }
  72. private static void CreateWatcherForLinux(string downloadPath, string destPath)
  73. {
  74. while (true)
  75. {
  76. try
  77. {
  78. var files = Directory.GetFiles(downloadPath,"*.dat");
  79. foreach (var file in files)
  80. {
  81. ConvertProcess(file, destPath);
  82. }
  83. }
  84. catch (Exception ex)
  85. {
  86. Console.WriteLine($"Convert fail!{ex}");
  87. }
  88. Thread.Sleep(1000);
  89. }
  90. }
  91. /// <summary>
  92. /// windows
  93. /// </summary>
  94. /// <param name="downloadPath"></param>
  95. /// <param name="destPath"></param>
  96. private static void CreateWatcher(string downloadPath, string destPath)
  97. {
  98. using (var watcher = new FileSystemWatcher())
  99. {
  100. watcher.Path = downloadPath;
  101. watcher.NotifyFilter = NotifyFilters.Attributes |
  102. NotifyFilters.CreationTime |
  103. NotifyFilters.DirectoryName |
  104. NotifyFilters.FileName |
  105. NotifyFilters.LastAccess |
  106. NotifyFilters.LastWrite |
  107. NotifyFilters.Security |
  108. NotifyFilters.Size;
  109. watcher.Filter = "*.dat";
  110. watcher.Created += new FileSystemEventHandler((o, e) =>
  111. {
  112. try
  113. {
  114. Thread.Sleep(50);
  115. ConvertProcess(e.FullPath, destPath);
  116. }
  117. catch (Exception ex)
  118. {
  119. Trace.WriteLine($"Convert fail!{ex}");
  120. }
  121. });
  122. watcher.InternalBufferSize = 1024000;
  123. //Start monitoring.
  124. watcher.EnableRaisingEvents = true;
  125. Console.ReadLine();
  126. }
  127. }
  128. private static void ConvertProcess(string vidFile, string destPath)
  129. {
  130. try
  131. {
  132. var destFile = Path.Combine(destPath, Path.GetFileName(vidFile).Replace(".dat", ".jpeg"));
  133. Directory.CreateDirectory(destPath);
  134. try
  135. {
  136. using (var vinnoImageData = new VinnoImageData(vidFile, OperationMode.Open))
  137. {
  138. if (vinnoImageData.ImageCount > 0)
  139. {
  140. if (!ConvertToMP4(vidFile, destPath, destFile, vinnoImageData))
  141. {
  142. ConvertToJPEG(vidFile, destFile, vinnoImageData);
  143. }
  144. }
  145. else
  146. {
  147. RenameConvert(vidFile, destFile);
  148. }
  149. }
  150. }
  151. catch(Exception ex)
  152. {
  153. Console.WriteLine($"{ex}");
  154. //转换出问题尝试改名
  155. RenameConvert(vidFile, destFile);
  156. }
  157. return;
  158. }
  159. catch (Exception ex)
  160. {
  161. Console.WriteLine($"Convert fail!{ex}");
  162. return;
  163. }
  164. }
  165. /// <summary>
  166. /// 直接改后缀
  167. /// </summary>
  168. /// <param name="vidFile"></param>
  169. /// <param name="destFile"></param>
  170. /// <returns></returns>
  171. private static bool RenameConvert(string vidFile, string destFile)
  172. {
  173. File.Copy(vidFile, destFile, true);
  174. if (File.Exists(destFile))
  175. {
  176. File.Delete(vidFile);
  177. Console.WriteLine($"Convert success!");
  178. return true;
  179. }
  180. Console.WriteLine($"Convert fail!");
  181. return false;
  182. }
  183. /// <summary>
  184. /// 转换为JPEG
  185. /// </summary>
  186. /// <param name="vidFile"></param>
  187. /// <param name="destFile"></param>
  188. /// <param name="vinnoImageData"></param>
  189. private static bool ConvertToJPEG(string vidFile, string destFile, VinnoImageData vinnoImageData)
  190. {
  191. if (File.Exists(destFile))
  192. {
  193. File.Delete(destFile);
  194. }
  195. File.WriteAllBytes(destFile, vinnoImageData.GetImage(0)?.ImageData);
  196. Thread.Sleep(10);
  197. if (File.Exists(destFile))
  198. {
  199. vinnoImageData.Close();
  200. vinnoImageData.Dispose();
  201. File.Delete(vidFile);
  202. Console.WriteLine($"Convert to jpeg success!");
  203. return true;
  204. }
  205. Console.WriteLine($"Convert to jpeg fail!");
  206. return false;
  207. }
  208. /// <summary>
  209. /// 转换为mp4
  210. /// </summary>
  211. /// <param name="vidFile"></param>
  212. /// <param name="destPath"></param>
  213. /// <param name="destFile"></param>
  214. /// <param name="vinnoImageData"></param>
  215. /// <returns></returns>
  216. private static bool ConvertToMP4(string vidFile, string destPath, string destFile, VinnoImageData vinnoImageData)
  217. {
  218. if (vinnoImageData.ImageCount > 1)
  219. {
  220. destFile = Path.Combine(destPath, Path.GetFileName(vidFile).Replace(".dat", ".mp4"));
  221. if (File.Exists(destFile))
  222. {
  223. File.Delete(destFile);
  224. }
  225. Mpeg4Converter.ConvertVidToMpeg4(vinnoImageData, destFile);
  226. Thread.Sleep(10);
  227. if (File.Exists(destFile))
  228. {
  229. vinnoImageData.Close();
  230. vinnoImageData.Dispose();
  231. File.Delete(vidFile);
  232. Console.WriteLine($"Convert to mpeg4 success!");
  233. return true;
  234. }
  235. Console.WriteLine($"Convert to mpeg4 fail!");
  236. }
  237. return false;
  238. }
  239. }
  240. }