123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Threading;
- using vCloud.Server.Utilities;
- using Vinno.vCloud.Common.Vid2;
- namespace Mpeg4ConverterTool
- {
- class Program
- {
- static void Main(string[] args)
- {
- var downloadPath = args.Length>0 ? args.GetValue(0)?.ToString(): string.Empty;
- var destPath = args.Length>1 ? args.GetValue(1)?.ToString() : string.Empty;
- if (string.IsNullOrWhiteSpace(downloadPath))
- {
- downloadPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "download");
- }
- if (string.IsNullOrWhiteSpace(destPath))
- {
- destPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "dest");
- }
- //var signNode = UFileSignInstance.GetUFileSignNode(StorageNodeType.FileStorageCDN);
- //var requestUrl = signNode.GetUrl(fileName);
- //var vidFile = Path.Combine(downloadPath, fileName);
- //var success = UFileStorageSingle.Instance.DownloadFile(vidFile, requestUrl);
- //if (success)
- //{
- // try
- // {
- // var vinnoImageData = new VinnoImageData(vidFile, OperationMode.Open);
- // Directory.CreateDirectory(Path.GetDirectoryName(destPath));
- // if (vinnoImageData.ImageFormat == VidImageFormat.H264)
- // {
- // var destFile = Path.Combine(destPath, fileName.Replace(".dat", ".mp4"));
- // Console.WriteLine(destFile);
- // if (File.Exists(destFile))
- // {
- // File.Delete(destFile);
- // }
- // Mpeg4Converter.ConvertVidToMpeg4(vinnoImageData, destFile);
- // if (File.Exists(destFile))
- // {
- // Console.WriteLine($"Convert success!");
- // return;
- // }
- // }
- // else
- // {
- // var destFile = Path.Combine(destPath, fileName.Replace(".dat", ".jpeg"));
- // File.WriteAllBytes(destFile, vinnoImageData.GetImage(0)?.ImageData);
- // Console.WriteLine($"Convert success!");
- // return;
- // }
- // Console.WriteLine($"Convert fail!");
- // }
- // catch (Exception ex)
- // {
- // Console.WriteLine($"Convert fail!{ex}");
- // return;
- // }
- //}
- //else
- //{
- // Console.WriteLine("Download fail!");
- // return;
- //}
- Directory.CreateDirectory(Path.GetDirectoryName(destPath));
- Directory.CreateDirectory(Path.GetDirectoryName(downloadPath));
- CreateWatcherForLinux(downloadPath, destPath);
- }
- private static void CreateWatcherForLinux(string downloadPath, string destPath)
- {
- while (true)
- {
- try
- {
- var files = Directory.GetFiles(downloadPath,"*.dat");
- foreach (var file in files)
- {
- ConvertProcess(file, destPath);
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Convert fail!{ex}");
- }
- Thread.Sleep(1000);
- }
- }
- /// <summary>
- /// windows
- /// </summary>
- /// <param name="downloadPath"></param>
- /// <param name="destPath"></param>
- private static void CreateWatcher(string downloadPath, string destPath)
- {
- using (var watcher = new FileSystemWatcher())
- {
- watcher.Path = downloadPath;
- watcher.NotifyFilter = NotifyFilters.Attributes |
- NotifyFilters.CreationTime |
- NotifyFilters.DirectoryName |
- NotifyFilters.FileName |
- NotifyFilters.LastAccess |
- NotifyFilters.LastWrite |
- NotifyFilters.Security |
- NotifyFilters.Size;
- watcher.Filter = "*.dat";
- watcher.Created += new FileSystemEventHandler((o, e) =>
- {
- try
- {
- Thread.Sleep(50);
- ConvertProcess(e.FullPath, destPath);
- }
- catch (Exception ex)
- {
- Trace.WriteLine($"Convert fail!{ex}");
- }
- });
- watcher.InternalBufferSize = 1024000;
- //Start monitoring.
- watcher.EnableRaisingEvents = true;
- Console.ReadLine();
- }
- }
- private static void ConvertProcess(string vidFile, string destPath)
- {
- try
- {
- var destFile = Path.Combine(destPath, Path.GetFileName(vidFile).Replace(".dat", ".jpeg"));
- Directory.CreateDirectory(destPath);
- try
- {
- using (var vinnoImageData = new VinnoImageData(vidFile, OperationMode.Open))
- {
- if (vinnoImageData.ImageCount > 0)
- {
- if (!ConvertToMP4(vidFile, destPath, destFile, vinnoImageData))
- {
- ConvertToJPEG(vidFile, destFile, vinnoImageData);
- }
- }
- else
- {
- RenameConvert(vidFile, destFile);
- }
- }
- }
- catch(Exception ex)
- {
- Console.WriteLine($"{ex}");
- //转换出问题尝试改名
- RenameConvert(vidFile, destFile);
- }
- return;
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Convert fail!{ex}");
- return;
- }
- }
- /// <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)
- {
- if (vinnoImageData.ImageCount > 1)
- {
- destFile = Path.Combine(destPath, Path.GetFileName(vidFile).Replace(".dat", ".mp4"));
- if (File.Exists(destFile))
- {
- File.Delete(destFile);
- }
- Mpeg4Converter.ConvertVidToMpeg4(vinnoImageData, destFile);
- 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;
- }
- }
- }
|