public string ExtractAudioSegment(long ticksToExtract, long ticksTimeLapse, string type, string fileDestination)
        {
            //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 = fileDestination,
            };

            string output = FFMpegService.Execute(parameters);

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

            return(fileDestination);
        }
        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);
        }
        public string ExtractVideoSegment(string FileDestination, long ticksToExtract, long ticksTimeLapse, VideoFormat type, Size dimensions)
        {
            string tempFile = FileDestination;
            // 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("-ss {0} -t {1} -acodec copy -vcodec copy ", 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")),
                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");
            }

            return(tempFile);
        }
        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);
        }
        public void GetVideoInfo()
        {
            string output = FFMpegService.Execute(FilePath);

            Duration     = InfoProcessor.GetDuration(output);
            AudioBitRate = InfoProcessor.GetAudioBitRate(output);
            AudioFormat  = InfoProcessor.GetAudioFormat(output);
            VideoFormat  = InfoProcessor.GetVideoFormat(output);
            Fps          = InfoProcessor.GetVideoFps(VideoFormat);
            Dimensions   = InfoProcessor.GetVideoDimensions(output);
            Created      = InfoProcessor.GetCreationTime(output);
        }
        public static string MergeVideoWithSplits(List <string> listVideoSegments, string outputFilePath)
        {
            try
            {
                string OutputFinal = FormatString(outputFilePath);

                string Instruction   = "-f concat -i ";
                int    indexFilePath = listVideoSegments[0].LastIndexOf(@"\") + 1;
                string filePath      = listVideoSegments[0].Substring(0, indexFilePath);
                string fileName      = "file_ " + Guid.NewGuid() + ".txt";
                string fileFullPath  = FormatString(filePath + fileName);

                List <string> lines = new List <string>();
                foreach (string line in listVideoSegments)
                {
                    lines.Add(@"file " + FormatString(line).Replace("\\", "\\\\"));
                }

                if (!File.Exists(fileFullPath))
                {
                    File.Create(fileFullPath).Close();
                    File.WriteAllLines(fileFullPath, lines);
                }

                Instruction = Instruction + '"' + fileFullPath + '"' + " -c copy ";

                FFMPEGParameters parameters = new FFMPEGParameters()
                {
                    DisableAudio   = false,
                    OutputOptions  = Instruction,
                    OutputFilePath = OutputFinal
                };

                string output = FFMpegService.Execute(parameters);

                return(outputFilePath);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        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);
        }
        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);
        }
        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);
        }
        public static string MergeVideoSegment(List <string> listVideoSegments, string outputFilePath)
        {
            string Instruction = "-i concat:";

            foreach (string item in listVideoSegments)
            {
                string ins = String.Format(@"""{0}""", item);
                Instruction += ins + "|";
            }

            Instruction = Instruction.TrimEnd('|');
            Instruction = Instruction + " -c copy ";

            FFMPEGParameters parameters = new FFMPEGParameters()
            {
                DisableAudio   = false,
                OutputOptions  = Instruction,
                OutputFilePath = outputFilePath
            };

            string output = FFMpegService.Execute(parameters);

            return(outputFilePath);
        }
        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);
        }