123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- using SkiaSharp;
- using DICOM = Dicom;
- using Dicom.Imaging;
- using Vinno.IUS.Common.Media.FFmpeg;
- using System.Diagnostics;
- namespace VidProcessService.Utilities
- {
- public class VidConverter
- {
- private static string _ffmpegPath;
- private string _tempfolder;
- public VidConverter(string tempfolder)
- {
- if (!Directory.Exists(tempfolder))
- {
- Directory.CreateDirectory(tempfolder);
- }
- _tempfolder = tempfolder;
- _ffmpegPath = GetFFmpegPath();
- }
- public long DicomToVid(string filePath, string targetPath)
- {
- var dicomFile = DICOM.DicomFile.Open(filePath);
- var dataset = dicomFile.Dataset;
- var dicomImage = new DicomImage(dataset);
- using (var vidFile = new VidFile(targetPath))
- {
- vidFile.AddExtendedData(dataset);
- vidFile.AddProbe(dataset);
- vidFile.AddImages(dicomImage);
- }
- if (File.Exists(targetPath))
- {
- FileInfo fileInfo = new FileInfo(targetPath);
- return fileInfo.Length;
- }
- return 0;
- }
- public long ImageToVid(string filePath, string targetPath)
- {
- FileStream fileStream =
- new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
- // 读取文件的 byte[]
- byte[] bytes = new byte[fileStream.Length];
- fileStream.Read(bytes, 0, bytes.Length);
- fileStream.Close();
- using (var vidFile = new VidFile(targetPath))
- {
- using (MemoryStream ms = new MemoryStream(bytes))
- {
- var skiaBitmap = SKBitmap.Decode(ms);
- vidFile.AddProbe(1);
- vidFile.AddImages(skiaBitmap.Width, skiaBitmap.Height, bytes);
- }
- }
- if (File.Exists(targetPath))
- {
- FileInfo fileInfo = new FileInfo(targetPath);
- return fileInfo.Length;
- }
- return 0;
- }
- /// <summary>
- /// Mp4 to vid
- /// </summary>
- /// <param name="filePath"></param>
- /// <param name="targetPath"></param>
- /// <returns></returns>
- public long Mp4ToVid(string filePath, string targetPath)
- {
- var imageTempPathList = new List<string>();
- string id = Guid.NewGuid().ToString();
- var targetImageDirectory = Path.Combine(_tempfolder, id);
- if (!Directory.Exists(targetImageDirectory))
- {
- Directory.CreateDirectory(targetImageDirectory);
- }
- ConvertImages(filePath, targetImageDirectory);//Mp4 to images
- //生成Vid
- ImagesToVid(targetImageDirectory, targetPath);//images to vid
- if (File.Exists(targetPath))
- {
- FileInfo fileInfo = new FileInfo(targetPath);
- return fileInfo.Length;
- }
- return 0;
- }
- private void ImagesToVid(string targetImageDirectory, string targetPath)
- {
- var filePaths = Directory.GetFiles(targetImageDirectory, "*.jpg");
- using (var vidFile = new VidFile(targetPath))
- {
- foreach (var item in filePaths)
- {
- FileStream fileStream =
- new FileStream(item, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
- // 读取文件的 byte[]
- byte[] bytes = new byte[fileStream.Length];
- fileStream.Read(bytes, 0, bytes.Length);
- fileStream.Close();
- using (MemoryStream ms = new MemoryStream(bytes))
- {
- var skiaBitmap = SKBitmap.Decode(ms);
- vidFile.AddProbe(1);
- vidFile.AddImages(skiaBitmap.Width, skiaBitmap.Height, bytes);
- }
- }
- }
- if (Directory.Exists(targetImageDirectory))//清除images文件夹
- {
- Directory.Delete(targetImageDirectory, true);
- }
- }
- private void ConvertImages(string sourcePath, string desfolderPath)
- {
- //TODO 需要计算帧率大小
- //var argument = "-i " + sourcePath + " -y -vframes 1 -f mjpeg " + desfolderPath+"\\output%d.jpg";
- // var argument = "-i " + sourcePath + " -vf fps=15 -vsync vfr " + desfolderPath+"\\output%d.jpg";
- var argument = "-i " + sourcePath + " " + Path.Combine(desfolderPath, "output%d.jpg");
- var log = new FFmpegLog();
- log.WriteArgs(argument);
- Process process;
- StartFFmpeg(out process, argument, log);
- process.WaitForExit();
- }
- private static void StartFFmpeg(out Process process, string arguments, FFmpegLog logItem = null)
- {
- process = new Process
- {
- StartInfo =
- {
- FileName = _ffmpegPath,
- Arguments = arguments,
- UseShellExecute = false,
- Verb = "runas",
- CreateNoWindow = true,
- RedirectStandardError = true,
- RedirectStandardInput = true
- },
- EnableRaisingEvents = true
- };
- if (logItem != null)
- {
- process.ErrorDataReceived += (s, e) => { };
- }
- process.Start();
- process.BeginErrorReadLine();
- }
- /// <summary>
- /// 获取 FFmpeg exe 路径
- /// </summary>
- /// <returns></returns>
- private string GetFFmpegPath()
- {
- switch (Environment.OSVersion.Platform)
- {
- case PlatformID.Win32S:
- throw new NotSupportedException($"Not supported OS {Environment.OSVersion.Platform}");
- case PlatformID.Win32Windows:
- throw new NotSupportedException($"Not supported OS {Environment.OSVersion.Platform}");
- case PlatformID.Win32NT:
- return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Packages", "ffmpeg.exe");
- break;
- case PlatformID.WinCE:
- throw new NotSupportedException($"Not supported OS {Environment.OSVersion.Platform}");
- case PlatformID.Unix:
- return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Packages", "ffmpeg");
- break;
- case PlatformID.Xbox:
- throw new NotSupportedException($"Not supported OS {Environment.OSVersion.Platform}");
- case PlatformID.MacOSX:
- return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Packages", "ffmpeg");
- break;
- default:
- throw new NotSupportedException($"Not supported OS {Environment.OSVersion.Platform}");
- }
- }
- }
- }
|