Esempio n. 1
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);
        }
Esempio n. 2
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);
        }
Esempio n. 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);
        }
Esempio n. 4
0
        protected 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);
        }
Esempio n. 5
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);
        }
Esempio n. 6
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);
        }
Esempio n. 7
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);
        }
Esempio n. 8
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);
        }