public byte[] GetVideoThumbnail(string inputFile, float frameTime, int width = -1, int height = -1) { InputFile = inputFile; var settings = new FFmpegSettings { Seek = frameTime, OutputFormat = "mjpeg", VideoFrameSize = $"-vf scale={width}:{height}", }; return(RunFFmpeg(inputFile, settings)); }
internal byte[] RunFFmpeg(string input, FFmpegSettings settings) { byte[] data; try { var arguments = $" -hide_banner -loglevel panic -y -ss {settings.Seek.ToString(CultureInfo.InvariantCulture)} -i \"{input}\" -t 1 -f {settings.OutputFormat} -vframes 1 {settings.VideoFrameSize} \"-\""; var processStartInfo = new ProcessStartInfo(Utils.FfmpegPath, arguments) { WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = true, UseShellExecute = false, WorkingDirectory = Path.GetDirectoryName(Utils.FfmpegPath) ?? string.Empty, RedirectStandardInput = true, //required to avoid ffmpeg printing to console RedirectStandardOutput = true, }; if (FFMpegProcess != null) { throw new InvalidOperationException(); } FFMpegProcess = Process.Start(processStartInfo); if (FFMpegProcess == null) { throw new FFMpegException(-1, "FFMpeg process was aborted"); } var ms = new MemoryStream(); //start reading here, otherwise the streams fill up and ffmpeg will block forever var imgDataTask = FFMpegProcess.StandardOutput.BaseStream.CopyToAsync(ms); WaitFFMpegProcessForExit(); imgDataTask.Wait(1000); data = ms.ToArray(); FFMpegProcess?.Close(); FFMpegProcess = null; } catch (Exception) { EnsureFFMpegProcessStopped(); throw; } return(data); }