/// <summary> /// PostUploadImageOrVideo /// </summary> /// <param name="type">image or video</param> /// <param name="str">stream</param> /// <returns></returns> public async Task <UploadPhoto> Upload(string type, Stream str) { var t = Task.Factory.StartNew(() => { var x = System.Data.SqlTypes.SqlDateTime.MaxValue; bool flag = false; string message = string.Empty; try { if (!string.IsNullOrEmpty(type)) { if (str != null) { var recivedFile = StreamToImageorVideoWithPath(str, type, CodeLibrary.SavePath()); if (recivedFile != null) { flag = true; message = CodeLibrary.SavePath() + recivedFile; } } else { message = "No file uploaded."; } } else { message = "provide missing parameter type."; } } catch (Exception) { flag = false; message = "Some error occurred."; } return(new UploadPhoto { flag = flag, Message = message, }); }); return(await t.ConfigureAwait(false)); }
private static void GetThumbnailApp(string video, string thumbnail, string smallThumbUrl, string smallThumbUrljpg, string OsType) { try { #region Scale & FrameRate var frameRate = 15; var scale = "420:-1"; var scaleSmall = "70x70"; var scaleSmallJPG = ""; string height = ""; string width = ""; int Heightreduce = 3; //1920 int widthreduce = 3; //1080 try { var ffProbe = new NReco.VideoInfo.FFProbe(); var videoInfo = ffProbe.GetMediaInfo(video); foreach (var stream in videoInfo.Streams) { frameRate = Convert.ToInt16(stream.FrameRate); if (stream.Width > 420 && stream.Height < 560) { scale = "420x560"; if (OsType == "android") { frameRate = 25; height = (stream.Height / (stream.Height * Heightreduce)).ToString(); width = (stream.Width - (stream.Width / widthreduce)).ToString(); scale = stream.Height + "x" + stream.Width; scaleSmallJPG = stream.Height + "x" + stream.Width; } else { //scaleSmallJPG = stream.Width + "x" + stream.Height; scale = stream.Width + "x" + stream.Height; scaleSmallJPG = scale; } } else { if (stream.Height > 0 && stream.Width > 0) { scale = stream.Width + "x" + stream.Height; if (OsType == "android") { //height = (stream.Height - (stream.Height * Heightreduce / 100)).ToString(); //width = (stream.Width - (stream.Width * widthreduce / 100)).ToString(); //scale = stream.Height + "x" + stream.Width; //scaleSmallJPG = stream.Height + "x" + stream.Width; frameRate = 25; height = (stream.Height / Heightreduce).ToString(); width = (stream.Width / widthreduce).ToString(); scale = height + "x" + width; scaleSmallJPG = height + "x" + width; } else { //scaleSmallJPG = stream.Width + "x" + stream.Height; scale = stream.Width + "x" + stream.Height; scaleSmallJPG = scale; } //scale = "1280x720"; break; } } } } catch (Exception) { frameRate = 15; scale = "420:-1"; scaleSmallJPG = "420:-1"; } #endregion if (frameRate < 0) { frameRate = 25; } var ffmpeg = CodeLibrary.SavePath() + @"\ffmpeg.exe"; string pngimage = CodeLibrary.SavePath() + @"\Images\Palette\palette_" + Guid.NewGuid() + ".png"; string pngSmallimage = CodeLibrary.SavePath() + @"\Images\iconPalette\palette_" + Guid.NewGuid() + ".png"; var processPalette = new Process(); var processGif = new Process(); var processSmallPalette = new Process(); var processSmallGif = new Process(); var processSmallJPG = new Process(); try { #region Large Thumbnail var infoPalette = new ProcessStartInfo(ffmpeg, " -y -t 3 -i " + video + " -vf fps=" + frameRate + ",scale=" + scale + ":flags=lanczos,palettegen " + pngimage) { CreateNoWindow = false, UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true }; processPalette.StartInfo = infoPalette; processPalette.EnableRaisingEvents = true; processPalette.Start(); processPalette.BeginOutputReadLine(); processPalette.BeginErrorReadLine(); processPalette.WaitForExit(); processPalette.Close(); var infoGif = new ProcessStartInfo(ffmpeg, " -t 3 -i " + video + " -i " + pngimage + " -filter_complex \"fps=" + frameRate + ",scale=" + scale + ":flags=lanczos[x];[x][1:v]paletteuse\" " + thumbnail) { CreateNoWindow = false, UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true }; processGif.StartInfo = infoGif; processGif.EnableRaisingEvents = true; processGif.Start(); processGif.BeginOutputReadLine(); processGif.BeginErrorReadLine(); processGif.WaitForExit(); processGif.Close(); #endregion #region Small Thumbnail var ext = Path.GetExtension(video); var startTime = ""; if (ext != ".jpg") { startTime = "-ss 2"; } var infoSmallPalette = new ProcessStartInfo(ffmpeg, " -y " + startTime + " -t 3 -i " + video + " -vf fps=" + frameRate + ",scale=" + scaleSmall + ":flags=lanczos,palettegen " + pngSmallimage) { CreateNoWindow = false, UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true }; processSmallPalette.StartInfo = infoSmallPalette; processSmallPalette.EnableRaisingEvents = true; processSmallPalette.Start(); processSmallPalette.BeginOutputReadLine(); processSmallPalette.BeginErrorReadLine(); processSmallPalette.WaitForExit(); processSmallPalette.Close(); var infoSmallGif = new ProcessStartInfo(ffmpeg, " " + startTime + " -t 3 -i " + video + " -i " + pngSmallimage + " -filter_complex \"fps=" + frameRate + ",scale=" + scaleSmall + ":flags=lanczos[x];[x][1:v]paletteuse\" " + smallThumbUrl) { CreateNoWindow = false, UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true }; processSmallGif.StartInfo = infoSmallGif; processSmallGif.EnableRaisingEvents = true; processSmallGif.Start(); processSmallGif.BeginOutputReadLine(); processSmallGif.BeginErrorReadLine(); processSmallGif.WaitForExit(); processSmallGif.Close(); #endregion #region Small Thumbnail JPG var infoSmallJPG = new ProcessStartInfo(ffmpeg, " -t 3 -i " + video + " -i " + pngimage + " -filter_complex \"fps=" + frameRate + ",scale=" + scaleSmallJPG + ":flags=lanczos[x];[x][1:v]paletteuse\" " + smallThumbUrljpg) { CreateNoWindow = false, UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true }; processSmallJPG.StartInfo = infoSmallJPG; processSmallJPG.EnableRaisingEvents = true; processSmallJPG.Start(); processSmallJPG.BeginOutputReadLine(); processSmallJPG.BeginErrorReadLine(); processSmallJPG.WaitForExit(); processSmallJPG.Close(); #endregion #region Remove Image/Video //if ((File.Exists(video))) // File.Delete(video); #endregion } catch (Exception ex) { processPalette.Close(); processGif.Close(); processSmallPalette.Close(); processSmallGif.Close(); } } catch (Exception ex) { } }