ConvertService.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using vCloud.Server.Utilities;
  5. using Vinno.vCloud.Common.Vid2;
  6. namespace ConvertTool
  7. {
  8. public class ConvertService
  9. {
  10. public bool ConvertProcess(string vidFile, string destPath,string rootPath)
  11. {
  12. try
  13. {
  14. Console.WriteLine($"vidFile:{vidFile},destPath:{destPath}");
  15. var destFile = Path.Combine(destPath, Path.GetFileName(vidFile).Replace(".vid", ".jpeg"));
  16. Directory.CreateDirectory(destPath);
  17. try
  18. {
  19. using (var vinnoImageData = new VinnoImageData(vidFile, OperationMode.Open))
  20. {
  21. if (vinnoImageData.ImageCount > 0)
  22. {
  23. if (!ConvertToMP4(vidFile, destPath, destFile, vinnoImageData, rootPath))
  24. {
  25. ConvertToJPEG(vidFile, destFile, vinnoImageData);
  26. }
  27. }
  28. else
  29. {
  30. RenameConvert(vidFile, destFile);
  31. }
  32. }
  33. }
  34. catch (Exception ex)
  35. {
  36. Console.WriteLine($"{ex}");
  37. //转换出问题尝试改名
  38. RenameConvert(vidFile, destFile);
  39. }
  40. return true;
  41. }
  42. catch (Exception ex)
  43. {
  44. Console.WriteLine($"Convert fail!{ex}");
  45. return false;
  46. }
  47. }
  48. public bool SayHi()
  49. {
  50. Console.WriteLine("Hi");
  51. return true;
  52. }
  53. /// <summary>
  54. /// 直接改后缀
  55. /// </summary>
  56. /// <param name="vidFile"></param>
  57. /// <param name="destFile"></param>
  58. /// <returns></returns>
  59. private static bool RenameConvert(string vidFile, string destFile)
  60. {
  61. File.Copy(vidFile, destFile, true);
  62. if (File.Exists(destFile))
  63. {
  64. File.Delete(vidFile);
  65. Console.WriteLine($"Convert success!");
  66. return true;
  67. }
  68. Console.WriteLine($"Convert fail!");
  69. return false;
  70. }
  71. /// <summary>
  72. /// 转换为JPEG
  73. /// </summary>
  74. /// <param name="vidFile"></param>
  75. /// <param name="destFile"></param>
  76. /// <param name="vinnoImageData"></param>
  77. private static bool ConvertToJPEG(string vidFile, string destFile, VinnoImageData vinnoImageData)
  78. {
  79. if (File.Exists(destFile))
  80. {
  81. File.Delete(destFile);
  82. }
  83. File.WriteAllBytes(destFile, vinnoImageData.GetImage(0)?.ImageData);
  84. Thread.Sleep(10);
  85. if (File.Exists(destFile))
  86. {
  87. vinnoImageData.Close();
  88. vinnoImageData.Dispose();
  89. File.Delete(vidFile);
  90. Console.WriteLine($"Convert to jpeg success!");
  91. return true;
  92. }
  93. Console.WriteLine($"Convert to jpeg fail!");
  94. return false;
  95. }
  96. /// <summary>
  97. /// 转换为mp4
  98. /// </summary>
  99. /// <param name="vidFile"></param>
  100. /// <param name="destPath"></param>
  101. /// <param name="destFile"></param>
  102. /// <param name="vinnoImageData"></param>
  103. /// <returns></returns>
  104. private static bool ConvertToMP4(string vidFile, string destPath, string destFile, VinnoImageData vinnoImageData,string rootPath)
  105. {
  106. Console.WriteLine($"ConvertToMP4:{vidFile},destPath:{destPath}");
  107. if (vinnoImageData.ImageCount > 1)
  108. {
  109. destFile = Path.Combine(destPath, Path.GetFileName(vidFile).Replace(".vid", ".mp4"));
  110. if (File.Exists(destFile))
  111. {
  112. File.Delete(destFile);
  113. }
  114. Console.WriteLine("before ConvertVidToMpeg4");
  115. Mpeg4Converter.ConvertVidToMpeg4(vinnoImageData, destFile, rootPath);
  116. Console.WriteLine("after ConvertVidToMpeg4");
  117. Thread.Sleep(10);
  118. if (File.Exists(destFile))
  119. {
  120. vinnoImageData.Close();
  121. vinnoImageData.Dispose();
  122. File.Delete(vidFile);
  123. Console.WriteLine($"Convert to mpeg4 success!");
  124. return true;
  125. }
  126. Console.WriteLine($"Convert to mpeg4 fail!");
  127. }
  128. return false;
  129. }
  130. }
  131. }