Пример #1
0
        public Image ExtractSingleFrame(int positionToExtract, Size dimensions)
        {
            string           tempFile   = Path.ChangeExtension(Path.GetTempFileName(), "jpg");
            FFMPEGParameters parameters = new FFMPEGParameters()
            {
                InputFilePath  = FilePath,
                DisableAudio   = true,
                OutputOptions  = String.Format("-f image2 -ss {0} -vframes 1", positionToExtract),
                Size           = dimensions,
                OutputFilePath = tempFile,
            };

            ;
            string output = FFMPEG.Execute(parameters);

            if (!File.Exists(tempFile))
            {
                throw new Exception("Could not create single frame image from video clip");
            }

            Image previewImage = LoadImageFromFile(tempFile);

            try
            {
                File.Delete(tempFile);
            }

            catch (Exception ex)
            {
                throw new Exception("Failed to delete temporary file used for thumbnail " + ex.Message);
            }

            return(previewImage);
        }
Пример #2
0
        public static string Execute(string inputFilePath)
        {
            if (String.IsNullOrWhiteSpace(inputFilePath))
            {
                throw new ArgumentNullException("Input file path cannot be null");
            }

            FFMPEGParameters parameters = new FFMPEGParameters()
            {
                InputFilePath = inputFilePath
            };

            return(Execute(parameters));
        }
Пример #3
0
        public static string Execute(string inputFilePath)
        {
            if (String.IsNullOrWhiteSpace(inputFilePath))
            {
                throw new ArgumentNullException("Input file path cannot be null");
            }

            FFMPEGParameters parameters = new FFMPEGParameters()
            {
                InputFilePath = inputFilePath
            };

            return Execute(parameters);
        }
Пример #4
0
        public static string Execute(FFMPEGParameters parameters)
        {
            if (String.IsNullOrWhiteSpace(FFMPEGExecutableFilePath))
            {
                throw new ArgumentNullException("Path to FFMPEG executable cannot be null");
            }

            if (parameters == null)
            {
                throw new ArgumentNullException("FFMPEG parameters cannot be completely null");
            }

            using (Process ffmpegProcess = new Process())
            {
                ProcessStartInfo info = new ProcessStartInfo(FFMPEGExecutableFilePath)
                {
                    Arguments              = parameters.ToString(),
                    WorkingDirectory       = Path.GetDirectoryName(FFMPEGExecutableFilePath),
                    UseShellExecute        = false,
                    CreateNoWindow         = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true
                };

                ffmpegProcess.StartInfo = info;
                ffmpegProcess.Start();
                string processOutput = ffmpegProcess.StandardError.ReadToEnd();
                ffmpegProcess.WaitForExit();
                PreviousBuffers.Enqueue(processOutput);
                lock (PreviousBuffers)
                {
                    while (PreviousBuffers.Count > MaximumBuffers)
                    {
                        PreviousBuffers.Dequeue();
                    }
                }

                return(processOutput);
            }
        }
Пример #5
0
        private static void ResizeVideo()
        {
            FFMPEGParameters parameters = new FFMPEGParameters()
            {
                InputFilePath = m_inputFilePath,
                OutputFilePath = m_outputFilePath,
                VideoCodec = "libx264",
                AudioCodec = "libvo_aacenc",
                Format = "flv",
                BufferSize = 50000,
                Size = new Size(320, 240),
                MaximumRate = 400,
                Overwrite = true,
                OutputOptions = "-ar 22050 -ab 128k -ac 1",
            };

            string output = FFMPEG.Execute(parameters);
        }
Пример #6
0
        public string WatermarkVideo(string watermarkImageFilePath, bool overwrite, WatermarkPosition position, Point offset)
        {
            string extension = Path.GetExtension(FilePath);
            string tempOutputFile = Path.ChangeExtension(Path.GetTempFileName(), extension);

            string overlayFormat;
            switch (position)
            {
                case WatermarkPosition.TopLeft:
                    overlayFormat = "{0}:{1}";
                    break;
                case WatermarkPosition.TopRight:
                    overlayFormat = "main_w-overlay_w-{0}:{1}";
                    break;
                case WatermarkPosition.BottomLeft:
                    overlayFormat = "{0}:main_h-overlay_h-{1}";
                    break;
                case WatermarkPosition.BottomRight:
                    overlayFormat = "main_w-overlay_w-{0}:main_h-overlay_h-{1}";
                    break;
                case WatermarkPosition.Center:
                    overlayFormat = "(main_w-overlay_w)/2-{0}:(main_h-overlay_h)/2-{1}";
                    break;
                case WatermarkPosition.MiddleLeft:
                    overlayFormat = "{0}:(main_h-overlay_h)/2-{1}";
                    break;
                case WatermarkPosition.MiddleRight:
                    overlayFormat = "main_w-overlay_w-{0}:(main_h-overlay_h)/2-{1}";
                    break;
                case WatermarkPosition.CenterTop:
                    overlayFormat = "(main_w-overlay_w)/2-{0}:{1}";
                    break;
                case WatermarkPosition.CenterBottom:
                    overlayFormat = "(main_w-overlay_w)/2-{0}:main_h-overlay_h-{1}";
                    break;

                default:
                    throw new ArgumentException("Invalid position specified");

            }

            string overlayPostion = String.Format(overlayFormat, offset.X, offset.Y);

            FFMPEGParameters parameters = new FFMPEGParameters
            {
                InputFilePath = FilePath,
                OutputFilePath = tempOutputFile,
                SameQ = true,
                Overwrite = true,
                VideoFilter = String.Format("\"movie=\\'{0}\\' [logo]; [in][logo] overlay={1} [out]\"", watermarkImageFilePath.Replace("\\", "\\\\"), overlayPostion)
            };

            string output = FFMPEG.Execute(parameters);
            if (File.Exists(tempOutputFile) == false)
            {
                throw new ApplicationException(String.Format("Failed to watermark video {0}{1}{2}", FilePath, Environment.NewLine, output));
            }

            FileInfo watermarkedVideoFileInfo = new FileInfo(tempOutputFile);
            if (watermarkedVideoFileInfo.Length == 0)
            {
                throw new ApplicationException(String.Format("Failed to watermark video {0}{1}{2}", FilePath, Environment.NewLine, output));
            }

            if (overwrite)
            {
                File.Delete(FilePath);
                File.Move(tempOutputFile, FilePath);

                return FilePath;
            }

            return tempOutputFile;
        }
Пример #7
0
        public Image ExtractSingleFrame(int positionToExtract, Size dimensions)
        {
            string tempFile = Path.ChangeExtension(Path.GetTempFileName(), "jpg");
            FFMPEGParameters parameters = new FFMPEGParameters()
            {
                InputFilePath = FilePath,
                DisableAudio = true,
                OutputOptions = String.Format("-f image2 -ss {0} -vframes 1", positionToExtract),
                Size = dimensions,
                OutputFilePath = tempFile,
            };

            ;
            string output = FFMPEG.Execute(parameters);

            if (!File.Exists(tempFile))
            {
                throw new Exception("Could not create single frame image from video clip");
            }

            Image previewImage = LoadImageFromFile(tempFile);

            try
            {
                File.Delete(tempFile);
            }

            catch (Exception ex)
            {
                throw new Exception("Failed to delete temporary file used for thumbnail " + ex.Message);
            }

            return previewImage;
        }
Пример #8
0
        public static string Execute(FFMPEGParameters parameters)
        {
            if (String.IsNullOrWhiteSpace(FFMPEGExecutableFilePath))
            {
                throw new ArgumentNullException("Path to FFMPEG executable cannot be null");
            }

            if (parameters == null)
            {
                throw new ArgumentNullException("FFMPEG parameters cannot be completely null");
            }

            using (Process ffmpegProcess = new Process())
            {
                ProcessStartInfo info = new ProcessStartInfo(FFMPEGExecutableFilePath)
                {
                    Arguments = parameters.ToString(),
                    WorkingDirectory = Path.GetDirectoryName(FFMPEGExecutableFilePath),
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true
                };

                ffmpegProcess.StartInfo = info;
                ffmpegProcess.Start();
                string processOutput = ffmpegProcess.StandardError.ReadToEnd();
                ffmpegProcess.WaitForExit();
                PreviousBuffers.Enqueue(processOutput);
                lock (PreviousBuffers)
                {
                    while (PreviousBuffers.Count > MaximumBuffers)
                    {
                        PreviousBuffers.Dequeue();
                    }

                }

                return processOutput;
            }
        }
Пример #9
0
        public string WatermarkVideo(string watermarkImageFilePath, bool overwrite, WatermarkPosition position, Point offset)
        {
            string extension      = Path.GetExtension(FilePath);
            string tempOutputFile = Path.ChangeExtension(Path.GetTempFileName(), extension);

            string overlayFormat;

            switch (position)
            {
            case WatermarkPosition.TopLeft:
                overlayFormat = "{0}:{1}";
                break;

            case WatermarkPosition.TopRight:
                overlayFormat = "main_w-overlay_w-{0}:{1}";
                break;

            case WatermarkPosition.BottomLeft:
                overlayFormat = "{0}:main_h-overlay_h-{1}";
                break;

            case WatermarkPosition.BottomRight:
                overlayFormat = "main_w-overlay_w-{0}:main_h-overlay_h-{1}";
                break;

            case WatermarkPosition.Center:
                overlayFormat = "(main_w-overlay_w)/2-{0}:(main_h-overlay_h)/2-{1}";
                break;

            case WatermarkPosition.MiddleLeft:
                overlayFormat = "{0}:(main_h-overlay_h)/2-{1}";
                break;

            case WatermarkPosition.MiddleRight:
                overlayFormat = "main_w-overlay_w-{0}:(main_h-overlay_h)/2-{1}";
                break;

            case WatermarkPosition.CenterTop:
                overlayFormat = "(main_w-overlay_w)/2-{0}:{1}";
                break;

            case WatermarkPosition.CenterBottom:
                overlayFormat = "(main_w-overlay_w)/2-{0}:main_h-overlay_h-{1}";
                break;

            default:
                throw new ArgumentException("Invalid position specified");
            }

            string overlayPostion = String.Format(overlayFormat, offset.X, offset.Y);

            FFMPEGParameters parameters = new FFMPEGParameters
            {
                InputFilePath  = FilePath,
                OutputFilePath = tempOutputFile,
                SameQ          = true,
                Overwrite      = true,
                VideoFilter    = String.Format("\"movie=\\'{0}\\' [logo]; [in][logo] overlay={1} [out]\"", watermarkImageFilePath.Replace("\\", "\\\\"), overlayPostion)
            };


            string output = FFMPEG.Execute(parameters);

            if (File.Exists(tempOutputFile) == false)
            {
                throw new ApplicationException(String.Format("Failed to watermark video {0}{1}{2}", FilePath, Environment.NewLine, output));
            }

            FileInfo watermarkedVideoFileInfo = new FileInfo(tempOutputFile);

            if (watermarkedVideoFileInfo.Length == 0)
            {
                throw new ApplicationException(String.Format("Failed to watermark video {0}{1}{2}", FilePath, Environment.NewLine, output));
            }

            if (overwrite)
            {
                File.Delete(FilePath);
                File.Move(tempOutputFile, FilePath);

                return(FilePath);
            }

            return(tempOutputFile);
        }