Example #1
0
        public void GetVideoThumbnail(string inputFile, string outputFile, float?frameTime)
        {
            Media input = new Media {
                Filename = inputFile
            };
            Media output = new Media {
                Filename = outputFile,
                Format   = "mjpeg"
            };
            ConvertSettings settings = new ConvertSettings {
                VideoFrameCount = 1,
                Seek            = frameTime,
                MaxDuration     = 1f
            };

            this.ConvertMedia(input, output, settings);
        }
Example #2
0
        // Converts a folder of media files.
        //
        // \param path folder - The folder to be converted.
        // \param int toFormatcomboBoxIndex - The index of the format to be converted to.
        // \param int fromFormatcomboBoxIndex - The index of the format to be converted from.
        // \return bool - True if media file is converted correctly otherwise false.
        //
        public bool ConvertFolder(string folder, int toFormatcomboBoxIndex, int fromFormatcomboBoxIndex, bool deleteAll, bool subDir, ConvertSettings VideoSettings)
        {
            string[] mediaFiles = new string[1];
            bool     result     = false;

            if (subDir)
            {
                mediaFiles = Directory.GetFiles(folder, "*.*", SearchOption.AllDirectories);
            }
            else
            {
                mediaFiles = Directory.GetFiles(folder);
            }

            // For loop to go through each file.
            foreach (string file in mediaFiles)
            {
                // If the file is a media file and it is not already the correct format.
                if (((fromFormatcomboBoxIndex == 0 && IsMediaFile(file)) || (fromFormatcomboBoxIndex != 0 &&
                                                                             Path.GetExtension(file) == mediaExtensions[fromFormatcomboBoxIndex - 1])) && Path.GetExtension(file) != mediaExtensions[toFormatcomboBoxIndex])
                {
                    result = ConvertFile(file, toFormatcomboBoxIndex, fromFormatcomboBoxIndex, deleteAll, VideoSettings);
                }
            }
            return(result);
        }
Example #3
0
        // Converts a single media file.
        //
        // \param path file - The file to be converted.
        // \param int toFormatcomboBoxIndex - The index of the format to be converted to.
        // \param int fromFormatcomboBoxIndex - The index of the format to be converted from.
        // \return bool - True if media file is converted correctly otherwise false.
        //
        public bool ConvertFile(string file, int toFormatcomboBoxIndex, int fromFormatcomboBoxIndex, bool deleteAll, ConvertSettings VideoSettings)
        {
            // The array of supported file formats
            string[] formats =
            {
                Format.mp4, Format.matroska, Format.avi,
                Format.mov, Format.mpeg,     Format.ogg,
                Format.flv, Format.webm
            };

            // If the file is a media file and it is not already the correct format.
            if (((fromFormatcomboBoxIndex == 0 && IsMediaFile(file)) || (fromFormatcomboBoxIndex != 0 &&
                                                                         Path.GetExtension(file) == mediaExtensions[fromFormatcomboBoxIndex - 1])) && Path.GetExtension(file) != mediaExtensions[toFormatcomboBoxIndex])
            {
                // Determine which file is currently being processed.
                VideoConverterForm.currentFile = "Currently Processing: " + Path.GetFileName(file);

                // Create the new file name.
                string newFile   = file.Substring(0, file.Length - Path.GetExtension(file).Length) + mediaExtensions[toFormatcomboBoxIndex];
                string format    = formats[toFormatcomboBoxIndex];
                string oldFormat = Path.GetExtension(file).Substring(1);
                if (oldFormat == "mkv")
                {
                    oldFormat = Format.matroska;
                }

                try
                {
                    // Converts the file to the selected format.
                    ffMpeg.ConvertMedia(file, oldFormat, newFile, format, VideoSettings);

                    // Deletes the files if the checkbox is checked.
                    if (deleteAll)
                    {
                        File.Delete(file);
                    }

                    return(true);
                }
                catch
                {
                    MessageBox.Show("Error Detected in File " + Path.GetFileName(file) + " Skipping");
                    if (File.Exists(newFile))
                    {
                        File.Delete(newFile);
                    }
                    return(false);
                }
            }
            return(false);
        }
Example #4
0
        protected string ComposeFFMpegCommandLineArgs(string inputFile, string inputFormat, string outputFile, string outputFormat, ConvertSettings settings)
        {
            StringBuilder builder = new StringBuilder();

            if (settings.AppendSilentAudioStream)
            {
                builder.Append(" -f lavfi -i aevalsrc=0 ");
            }
            if (settings.Seek.HasValue)
            {
                builder.AppendFormat(CultureInfo.InvariantCulture, " -ss {0}", new object[] { settings.Seek });
            }
            if (inputFormat != null)
            {
                builder.Append(" -f " + inputFormat);
            }
            if (settings.CustomInputArgs != null)
            {
                builder.AppendFormat(" {0} ", settings.CustomInputArgs);
            }
            StringBuilder outputArgs = new StringBuilder();

            this.ComposeFFMpegOutputArgs(outputArgs, outputFormat, settings);
            if (settings.AppendSilentAudioStream)
            {
                outputArgs.Append(" -shortest ");
            }
            return(string.Format(" -loglevel {4} {0} -i {1} {2} -y {3}", new object[] { builder.ToString(), this.CommandArgParameter(inputFile), outputArgs.ToString(), this.CommandArgParameter(outputFile), this.LogLevel }));
        }
Example #5
0
        private ConvertLiveMediaTask CreateLiveMediaTask(string toolArgs, Stream inputStream, Stream outputStream, ConvertSettings settings)
        {
            FFMpegProgress progress = new FFMpegProgress(new Action <ConvertProgressEventArgs>(this.OnConvertProgress), this.ConvertProgress != null);

            if (settings != null)
            {
                progress.Seek        = settings.Seek;
                progress.MaxDuration = settings.MaxDuration;
            }
            return(new ConvertLiveMediaTask(this, toolArgs, inputStream, outputStream, progress));
        }
Example #6
0
        public void ConvertMedia(string inputFile, string inputFormat, string outputFile, string outputFormat, ConvertSettings settings)
        {
            if (inputFile == null)
            {
                throw new ArgumentNullException("inputFile");
            }
            if (outputFile == null)
            {
                throw new ArgumentNullException("outputFile");
            }
            if ((File.Exists(inputFile) && string.IsNullOrEmpty(Path.GetExtension(inputFile))) && (inputFormat == null))
            {
                throw new Exception("Input format is required for file without extension");
            }
            if (string.IsNullOrEmpty(Path.GetExtension(outputFile)) && (outputFormat == null))
            {
                throw new Exception("Output format is required for file without extension");
            }
            Media input = new Media {
                Filename = inputFile,
                Format   = inputFormat
            };
            Media output = new Media {
                Filename = outputFile,
                Format   = outputFormat
            };

            this.ConvertMedia(input, output, settings ?? new ConvertSettings());
        }
Example #7
0
        internal void ConvertMedia(Media input, Media output, ConvertSettings settings)
        {
            this.EnsureFFMpegLibs();
            string filename = input.Filename;

            if (filename == null)
            {
                filename = Path.GetTempFileName();
                using (FileStream stream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    this.CopyStream(input.DataStream, stream, 0x40000);
                }
            }
            string path = output.Filename;

            if (path == null)
            {
                path = Path.GetTempFileName();
            }
            if (((output.Format == "flv") || (Path.GetExtension(path).ToLower() == ".flv")) && !settings.AudioSampleRate.HasValue)
            {
                settings.AudioSampleRate = 0xac44;
            }
            try
            {
                string fFMpegExePath = this.GetFFMpegExePath();
                if (!File.Exists(fFMpegExePath))
                {
                    throw new FileNotFoundException("Cannot find ffmpeg tool: " + fFMpegExePath);
                }
                string           arguments = this.ComposeFFMpegCommandLineArgs(filename, input.Format, path, output.Format, settings);
                ProcessStartInfo startInfo = new ProcessStartInfo(fFMpegExePath, arguments)
                {
                    WindowStyle            = ProcessWindowStyle.Hidden,
                    CreateNoWindow         = true,
                    UseShellExecute        = false,
                    WorkingDirectory       = Path.GetDirectoryName(this.FFMpegToolPath),
                    RedirectStandardInput  = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true
                };
                this.InitStartInfo(startInfo);
                if (this.FFMpegProcess != null)
                {
                    throw new InvalidOperationException("FFMpeg process is already started");
                }
                this.FFMpegProcess = Process.Start(startInfo);
                if (this.FFMpegProcessPriority != ProcessPriorityClass.Normal)
                {
                    this.FFMpegProcess.PriorityClass = this.FFMpegProcessPriority;
                }
                string         lastErrorLine  = string.Empty;
                FFMpegProgress ffmpegProgress = new FFMpegProgress(new Action <ConvertProgressEventArgs>(this.OnConvertProgress), this.ConvertProgress != null);
                if (settings != null)
                {
                    ffmpegProgress.Seek        = settings.Seek;
                    ffmpegProgress.MaxDuration = settings.MaxDuration;
                }
                this.FFMpegProcess.ErrorDataReceived += delegate(object o, DataReceivedEventArgs args) {
                    if (args.Data != null)
                    {
                        lastErrorLine = args.Data;
                        ffmpegProgress.ParseLine(args.Data);
                        this.FFMpegLogHandler(args.Data);
                    }
                };
                this.FFMpegProcess.OutputDataReceived += delegate(object o, DataReceivedEventArgs args) {
                };
                this.FFMpegProcess.BeginOutputReadLine();
                this.FFMpegProcess.BeginErrorReadLine();
                this.WaitFFMpegProcessForExit();
                if (this.FFMpegProcess.ExitCode != 0)
                {
                    throw new FFMpegException(this.FFMpegProcess.ExitCode, lastErrorLine);
                }
                this.FFMpegProcess.Close();
                this.FFMpegProcess = null;
                ffmpegProgress.Complete();
                if (output.Filename == null)
                {
                    using (FileStream stream2 = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None))
                    {
                        this.CopyStream(stream2, output.DataStream, 0x40000);
                    }
                }
            }
            catch (Exception)
            {
                this.EnsureFFMpegProcessStopped();
                throw;
            }
            finally
            {
                if (((filename != null) && (input.Filename == null)) && File.Exists(filename))
                {
                    File.Delete(filename);
                }
                if (((path != null) && (output.Filename == null)) && File.Exists(path))
                {
                    File.Delete(path);
                }
            }
        }
Example #8
0
        public ConvertLiveMediaTask ConvertLiveMedia(string inputSource, string inputFormat, Stream outputStream, string outputFormat, ConvertSettings settings)
        {
            this.EnsureFFMpegLibs();
            string toolArgs = this.ComposeFFMpegCommandLineArgs(inputSource, inputFormat, "-", outputFormat, settings);

            return(this.CreateLiveMediaTask(toolArgs, null, outputStream, settings));
        }
Example #9
0
 public ConvertLiveMediaTask ConvertLiveMedia(string inputFormat, Stream outputStream, string outputFormat, ConvertSettings settings)
 {
     return(this.ConvertLiveMedia((Stream)null, inputFormat, outputStream, outputFormat, settings));
 }