123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- using System;
- using System.IO;
- using System.Threading;
- using vCloud.Server.Utilities;
- using Vinno.vCloud.Common.Vid2;
- namespace ConvertTool
- {
- public class ConvertService
- {
- public bool ConvertProcess(string vidFile, string destPath,string rootPath)
- {
- try
- {
- Console.WriteLine($"vidFile:{vidFile},destPath:{destPath}");
- var destFile = Path.Combine(destPath, Path.GetFileName(vidFile).Replace(".vid", ".jpeg"));
- Directory.CreateDirectory(destPath);
- try
- {
- using (var vinnoImageData = new VinnoImageData(vidFile, OperationMode.Open))
- {
- if (vinnoImageData.ImageCount > 0)
- {
- if (!ConvertToMP4(vidFile, destPath, destFile, vinnoImageData, rootPath))
- {
- ConvertToJPEG(vidFile, destFile, vinnoImageData);
- }
- }
- else
- {
- RenameConvert(vidFile, destFile);
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine($"{ex}");
- //转换出问题尝试改名
- RenameConvert(vidFile, destFile);
- }
- return true;
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Convert fail!{ex}");
- return false;
- }
- }
- public bool SayHi()
- {
- Console.WriteLine("Hi");
- return true;
- }
- /// <summary>
- /// 直接改后缀
- /// </summary>
- /// <param name="vidFile"></param>
- /// <param name="destFile"></param>
- /// <returns></returns>
- private static bool RenameConvert(string vidFile, string destFile)
- {
- File.Copy(vidFile, destFile, true);
- if (File.Exists(destFile))
- {
- File.Delete(vidFile);
- Console.WriteLine($"Convert success!");
- return true;
- }
- Console.WriteLine($"Convert fail!");
- return false;
- }
- /// <summary>
- /// 转换为JPEG
- /// </summary>
- /// <param name="vidFile"></param>
- /// <param name="destFile"></param>
- /// <param name="vinnoImageData"></param>
- private static bool ConvertToJPEG(string vidFile, string destFile, VinnoImageData vinnoImageData)
- {
- if (File.Exists(destFile))
- {
- File.Delete(destFile);
- }
- File.WriteAllBytes(destFile, vinnoImageData.GetImage(0)?.ImageData);
- Thread.Sleep(10);
- if (File.Exists(destFile))
- {
- vinnoImageData.Close();
- vinnoImageData.Dispose();
- File.Delete(vidFile);
- Console.WriteLine($"Convert to jpeg success!");
- return true;
- }
- Console.WriteLine($"Convert to jpeg fail!");
- return false;
- }
- /// <summary>
- /// 转换为mp4
- /// </summary>
- /// <param name="vidFile"></param>
- /// <param name="destPath"></param>
- /// <param name="destFile"></param>
- /// <param name="vinnoImageData"></param>
- /// <returns></returns>
- private static bool ConvertToMP4(string vidFile, string destPath, string destFile, VinnoImageData vinnoImageData,string rootPath)
- {
- Console.WriteLine($"ConvertToMP4:{vidFile},destPath:{destPath}");
- if (vinnoImageData.ImageCount > 1)
- {
- destFile = Path.Combine(destPath, Path.GetFileName(vidFile).Replace(".vid", ".mp4"));
- if (File.Exists(destFile))
- {
- File.Delete(destFile);
- }
- Console.WriteLine("before ConvertVidToMpeg4");
- Mpeg4Converter.ConvertVidToMpeg4(vinnoImageData, destFile, rootPath);
- Console.WriteLine("after ConvertVidToMpeg4");
- Thread.Sleep(10);
- if (File.Exists(destFile))
- {
- vinnoImageData.Close();
- vinnoImageData.Dispose();
- File.Delete(vidFile);
- Console.WriteLine($"Convert to mpeg4 success!");
- return true;
- }
- Console.WriteLine($"Convert to mpeg4 fail!");
- }
- return false;
- }
- }
- }
|