Mpeg4Converter.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using SkiaSharp;
  2. using System;
  3. using System.Drawing;
  4. using System.IO;
  5. using Vinno.IUS.Common.Media.FFmpeg;
  6. using Vinno.IUS.Common.Media.FFmpeg.Settings;
  7. using Vinno.IUS.Common.Media.FFmpeg.Video;
  8. using Vinno.IUS.Common.Media.FFmpeg.Video.Codecs;
  9. using Vinno.vCloud.Common.Vid2;
  10. namespace vCloud.Server.Utilities
  11. {
  12. class Mpeg4Converter
  13. {
  14. static string RootPath;
  15. static Mpeg4Converter()
  16. {
  17. }
  18. private static SKBitmap GetBitmap(VinnoImage image)
  19. {
  20. using (MemoryStream ms = new MemoryStream(image.ImageData))
  21. {
  22. var bitmap = SKBitmap.Decode(ms);
  23. return bitmap;
  24. }
  25. }
  26. public static void ConvertVidToMpeg4(VinnoImageData sourceVid, string destFile,string rootPath)
  27. {
  28. try
  29. {
  30. if (string.IsNullOrWhiteSpace(RootPath))
  31. {
  32. RootPath = rootPath;
  33. Console.WriteLine("Mpeg4Converter:" + Path.Combine(RootPath, "Utilities", "ffmpeg"));
  34. FFmpegService.SetFFmpegPath(Path.Combine(RootPath, "Utilities", "ffmpeg"));
  35. }
  36. var firstFrame = sourceVid.GetImage(0);
  37. if (firstFrame != null)
  38. {
  39. var width = firstFrame.Width;
  40. var height = firstFrame.Height;
  41. if (width % 2 != 0)
  42. {
  43. width--;
  44. }
  45. if (height % 2 != 0)
  46. {
  47. height--;
  48. }
  49. var imageRect = new Rectangle(0, 0, width, height);
  50. var frameSize = width * height * 4;
  51. var frameData = new byte[frameSize];
  52. var frameRate = (int)sourceVid.Probe.FrameRate;
  53. frameRate = frameRate == 0 ? 10 : frameRate; //FrameRate can not 0,if 0 give default value 10
  54. var setting = new FFmpegSettings();
  55. var writeArgs = new FFmpegVideoWriterArgs(destFile, firstFrame.Width, firstFrame.Height)
  56. {
  57. FrameRate = frameRate,
  58. VideoCodec = FFmpegVideoCodec.X264
  59. };
  60. using (var videoWriter = new FFmpegVideoWriter(writeArgs, setting))
  61. {
  62. for (var i = 0; i < sourceVid.ImageCount; i++)
  63. {
  64. var vinnoImage = sourceVid.GetImage(i);
  65. if (vinnoImage.Height > 0 && vinnoImage.Width > 0 && vinnoImage.ImageData != null && vinnoImage.ImageData.Length > 0)
  66. {
  67. SKBitmap bitmap = GetBitmap(vinnoImage);
  68. if (bitmap.Width != width || bitmap.Height != height)
  69. {
  70. bitmap = bitmap.Resize(new SKImageInfo(width, height), SKFilterQuality.None);
  71. }
  72. try
  73. {
  74. if (bitmap.Bytes != null && bitmap.Bytes.Length > 0 && bitmap.Width > 0 && bitmap.Height > 0)
  75. {
  76. var frame = new BitmapFrame(bitmap.Width, bitmap.Height, bitmap.Bytes);
  77. videoWriter.WriteFrame(frame);
  78. }
  79. }
  80. catch (Exception exception)
  81. {
  82. throw new Exception($"encoder add image index {i} error:{exception}");
  83. }
  84. finally
  85. {
  86. bitmap.Dispose();
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }
  93. catch (Exception exception)
  94. {
  95. throw new Exception($"ConvertVidToMpeg4 error:{exception}");
  96. }
  97. }
  98. }
  99. }