예제 #1
0
        public string MergeAudioSegment(string audioFile, VideoFormat type)
        {
            //ffmpeg -i tmpVideo.mpg -i tmpAudioRB.wav -vcodec copy finalVideow_6.mpg
            string        tempFile = Path.GetTempPath() + Guid.NewGuid() + "." + type.ToString();
            List <string> files    = new List <string>();

            files.Add(audioFile);

            FFMPEGParameters parameters = new FFMPEGParameters()
            {
                InputFilePath        = FilePath,
                DisableAudio         = false,
                AdditionalFileInputs = files,
                OutputOptions        = String.Format("-map 0:0 -map 1:0 -vcodec copy -acodec copy"),
                OutputFilePath       = tempFile,
            };

            string output = FFMpegService.Execute(parameters);

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

            return(tempFile);
        }
예제 #2
0
        public string ExtractAudioSegment(long ticksToExtract, long ticksTimeLapse, AudioFormat type)
        {
            string tempFile = Path.GetTempPath() + Guid.NewGuid() + "." + type.ToString();
            var    span     = TimeSpan.FromTicks(ticksToExtract);
            var    spanTo   = TimeSpan.FromTicks(ticksTimeLapse - ticksToExtract);

            if (span > Duration)
            {
                throw new Exception("Time is larger than actual video");
            }

            FFMPEGParameters parameters = new FFMPEGParameters()
            {
                InputFilePath  = FilePath,
                DisableAudio   = false,
                OutputOptions  = String.Format("-vn -ss {0} -t {1}", span.Hours.ToString("D2") + ":" + span.Minutes.ToString("D2") + ":" + span.Seconds.ToString("D2") + "." + span.Milliseconds.ToString("D3"), spanTo.Hours.ToString("D2") + ":" + spanTo.Minutes.ToString("D2") + ":" + spanTo.Seconds.ToString("D2") + "." + spanTo.Milliseconds.ToString("D3")),
                OutputFilePath = tempFile,
            };

            string output = FFMpegService.Execute(parameters);

            if (!File.Exists(tempFile))
            {
                throw new Exception("Could not extract Audio From Video Clip");
            }

            return(tempFile);
        }
예제 #3
0
        private string SingleOverlayPass(List <Overlay> overlays, string inputFile)
        {
            string VideoFilterInputs   = string.Empty;
            string VideoFilterCommands = string.Empty;

            for (int i = 0; i < overlays.Count; i++)
            {
                if (overlays[i].Size.Width > 0 && overlays[i].Size.Height > 0)
                {
                    VideoFilterInputs += overlays[i].InitializeOutputString;
                }
            }

            for (int i = 0; i < overlays.Count; i++)
            {
                if (overlays[i].Size.Width > 0 && overlays[i].Size.Height > 0)
                {
                    VideoFilterCommands += overlays[i].OutputProcessString;

                    if (i != (overlays.Count - 1))
                    {
                        VideoFilterCommands += ",";
                    }
                }
            }

            string extension = Path.GetExtension(inputFile);

            string tempOutputFile = Path.GetTempPath() + Guid.NewGuid() + "." + extension;

            FFMPEGParameters parameters = new FFMPEGParameters
            {
                InputFilePath              = inputFile,
                OutputFilePath             = tempOutputFile,
                ComplexVideoFilterInputs   = VideoFilterInputs,
                ComplexVideoFilterCommands = VideoFilterCommands
            };

            string output = FFMpegService.Execute(parameters);

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

            FileInfo watermarkedVideoFileInfo = new FileInfo(tempOutputFile);

            if (watermarkedVideoFileInfo.Length == 0)
            {
                throw new ApplicationException(String.Format("Failed to overlay video {0}{1}{2}", inputFile, Environment.NewLine, output));
            }
            return(tempOutputFile);
        }
예제 #4
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));
        }
예제 #5
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);
        }
예제 #6
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;
            }

        }
예제 #7
0
        public string ExtractAudioSegment(AudioFormat type)
        {
            string tempFile = Path.GetTempPath() + Guid.NewGuid() + "." + type.ToString();

            FFMPEGParameters parameters = new FFMPEGParameters()
            {
                InputFilePath  = FilePath,
                DisableAudio   = false,
                OutputOptions  = String.Format("-vn"),
                OutputFilePath = tempFile,
            };

            string output = FFMpegService.Execute(parameters);

            if (!File.Exists(tempFile))
            {
                throw new Exception("Could not extract Audio From Video Clip");
            }

            return(tempFile);
        }
예제 #8
0
        public Image ExtractSingleFrame(long ticksToExtract, ImageFormat type, Size dimensions)
        {
            string tempFile = Path.GetTempPath() + Guid.NewGuid() + "." + type.ToString();
            var    span     = TimeSpan.FromTicks(ticksToExtract);

            if (span > Duration)
            {
                throw new Exception("Time is larger than actual video");
            }

            FFMPEGParameters parameters = new FFMPEGParameters()
            {
                InputFilePath  = FilePath,
                DisableAudio   = true,
                OutputOptions  = String.Format("-f image2 -ss {0} -vframes 1", span.Hours.ToString("D2") + ":" + span.Minutes.ToString("D2") + ":" + span.Seconds.ToString("D2") + "." + span.Milliseconds.ToString("D3")),
                Size           = dimensions,
                OutputFilePath = tempFile,
            };

            string output = FFMpegService.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);
        }
예제 #9
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);
            }
        }
예제 #10
0
        public string MergeAudioSegments(List <string> audioFiles, VideoFormat type)
        {
            string        tempFile = Path.GetTempPath() + Guid.NewGuid() + "." + type.ToString();
            List <string> files    = audioFiles;

            string outputOptions = string.Empty;

            string copyAll = " -c:v copy -c:a copy";

            for (int i = 0; i < files.Count; i++)
            {
                outputOptions += "-map " + (i + 1) + ":0";
                if (i + files.Count < files.Count)
                {
                    outputOptions += " ";
                }
            }

            FFMPEGParameters parameters = new FFMPEGParameters()
            {
                InputFilePath        = FilePath,
                DisableAudio         = false,
                AdditionalFileInputs = files,
                OutputOptions        = outputOptions + copyAll,
                OutputFilePath       = tempFile,
            };

            string output = FFMpegService.Execute(parameters);

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

            return(tempFile);
        }
예제 #11
0
        public Image ExtractSingleFrame(long ticksToExtract, ImageFormat type, Size dimensions)
        {

            string tempFile = Path.GetTempPath() + Guid.NewGuid() + "." + type.ToString();
            var span = TimeSpan.FromTicks(ticksToExtract);

            if (span > Duration)
                throw new Exception("Time is larger than actual video");

            FFMPEGParameters parameters = new FFMPEGParameters()
            {
                InputFilePath = FilePath,
                DisableAudio = true,
                OutputOptions = String.Format("-f image2 -ss {0} -vframes 1", span.Hours.ToString("D2") + ":" + span.Minutes.ToString("D2") + ":" + span.Seconds.ToString("D2") + "." + span.Milliseconds.ToString("D3")),
                Size = dimensions,
                OutputFilePath = tempFile,
            };

            string output = FFMpegService.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;
        }
예제 #12
0
        public string WatermarkVideo(string watermarkImageFilePath, bool overwrite, WatermarkPosition position, Point offset)
        {
            string extension = Path.GetExtension(FilePath);

            string tempOutputFile = Path.GetTempPath() + Guid.NewGuid() + "." + 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,
                QScale         = false,
                Overwrite      = true,
                VideoFilter    = String.Format("\"movie=\\'{0}\\' [logo]; [in][logo] overlay={1} [out]\"", watermarkImageFilePath.Replace("\\", "\\\\"), overlayPostion)
            };


            string output = FFMpegService.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);
        }
예제 #13
0
        public string MergeAudioSegments(List<string> audioFiles, VideoFormat type)
        {
            string tempFile = Path.GetTempPath() + Guid.NewGuid() + "." + type.ToString();
            List<string> files = audioFiles;

            string outputOptions = string.Empty;

            string copyAll = " -c:v copy -c:a copy";

            for (int i = 0; i < files.Count; i++)
            {
                outputOptions += "-map " + (i + 1) + ":0";
                if (i + files.Count < files.Count)
                    outputOptions += " ";
            }

            FFMPEGParameters parameters = new FFMPEGParameters()
            {
                InputFilePath = FilePath,
                DisableAudio = false,
                AdditionalFileInputs = files,
                OutputOptions = outputOptions + copyAll,
                OutputFilePath = tempFile,
            };

            string output = FFMpegService.Execute(parameters);

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

            return tempFile;
        }
예제 #14
0
        public string ExtractAudioSegment(AudioFormat type)
        {
            string tempFile = Path.GetTempPath() + Guid.NewGuid() + "." + type.ToString();

            FFMPEGParameters parameters = new FFMPEGParameters()
            {
                InputFilePath = FilePath,
                DisableAudio = false,
                OutputOptions = String.Format("-vn"),
                OutputFilePath = tempFile,

            };

            string output = FFMpegService.Execute(parameters);

            if (!File.Exists(tempFile))
            {
                throw new Exception("Could not extract Audio From Video Clip");
            }

            return tempFile;
        }
예제 #15
0
        public string MergeAudioSegment(string audioFile, VideoFormat type)
        {
            //ffmpeg -i tmpVideo.mpg -i tmpAudioRB.wav -vcodec copy finalVideow_6.mpg
            string tempFile = Path.GetTempPath() + Guid.NewGuid() + "." + type.ToString();
            List<string> files = new List<string>();
            files.Add(audioFile);

            FFMPEGParameters parameters = new FFMPEGParameters()
            {
                InputFilePath = FilePath,
                DisableAudio = false,
                AdditionalFileInputs = files,
                OutputOptions = String.Format("-map 0:0 -map 1:0 -vcodec copy -acodec copy"),
                OutputFilePath = tempFile,
            };

            string output = FFMpegService.Execute(parameters);

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

            return tempFile;
        }
예제 #16
0
        private string SingleOverlayPass(List<Overlay> overlays, string inputFile)
        {
            string VideoFilterInputs = string.Empty;
            string VideoFilterCommands = string.Empty;

            for (int i = 0; i < overlays.Count; i++)
            {
                if (overlays[i].Size.Width > 0 && overlays[i].Size.Height > 0)
                {
                    VideoFilterInputs += overlays[i].InitializeOutputString;
                }
            }

            for (int i = 0; i < overlays.Count; i++)
            {
                if (overlays[i].Size.Width > 0 && overlays[i].Size.Height > 0)
                {
                    VideoFilterCommands += overlays[i].OutputProcessString;

                    if (i != (overlays.Count - 1))
                        VideoFilterCommands += ",";
                }
            }

            string extension = Path.GetExtension(inputFile);
            
            string tempOutputFile = Path.GetTempPath() + Guid.NewGuid() + "." + extension;
            
            FFMPEGParameters parameters = new FFMPEGParameters
            {
                InputFilePath = inputFile,
                OutputFilePath = tempOutputFile,
                ComplexVideoFilterInputs = VideoFilterInputs,
                ComplexVideoFilterCommands = VideoFilterCommands
            };

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

            FileInfo watermarkedVideoFileInfo = new FileInfo(tempOutputFile);
            if (watermarkedVideoFileInfo.Length == 0)
            {
                throw new ApplicationException(String.Format("Failed to overlay video {0}{1}{2}", inputFile, Environment.NewLine, output));
            }
            return tempOutputFile;
        }
예제 #17
0
        public string WatermarkVideo(string watermarkImageFilePath, bool overwrite, WatermarkPosition position, Point offset)
        {
            string extension = Path.GetExtension(FilePath);

            string tempOutputFile = Path.GetTempPath() + Guid.NewGuid() + "." + 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,
                QScale = false,
                Overwrite = true,
                VideoFilter = String.Format("\"movie=\\'{0}\\' [logo]; [in][logo] overlay={1} [out]\"", watermarkImageFilePath.Replace("\\", "\\\\"), overlayPostion)
            };


            string output = FFMpegService.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;
        }
예제 #18
0
        public string ExtractAudioSegment(long ticksToExtract, long ticksTimeLapse, AudioFormat type)
        {
            string tempFile = Path.GetTempPath() + Guid.NewGuid() + "." + type.ToString();
            var span = TimeSpan.FromTicks(ticksToExtract);
            var spanTo = TimeSpan.FromTicks(ticksTimeLapse - ticksToExtract);

            if (span > Duration)
                throw new Exception("Time is larger than actual video");

            FFMPEGParameters parameters = new FFMPEGParameters()
            {
                InputFilePath = FilePath,
                DisableAudio = false,
                OutputOptions = String.Format("-vn -ss {0} -t {1}", span.Hours.ToString("D2") + ":" + span.Minutes.ToString("D2") + ":" + span.Seconds.ToString("D2") + "." + span.Milliseconds.ToString("D3"), spanTo.Hours.ToString("D2") + ":" + spanTo.Minutes.ToString("D2") + ":" + spanTo.Seconds.ToString("D2") + "." + spanTo.Milliseconds.ToString("D3")),
                OutputFilePath = tempFile,

            };

            string output = FFMpegService.Execute(parameters);

            if (!File.Exists(tempFile))
            {
                throw new Exception("Could not extract Audio From Video Clip");
            }

            return tempFile;
        }